Module: EasyType::Type::ClassMethods

Defined in:
lib/easy_type/type.rb

Instance Method Summary collapse

Instance Method Details

#define_os_command_method(method_or_command) ⇒ Object



294
295
296
297
298
299
# File 'lib/easy_type/type.rb', line 294

def define_os_command_method(method_or_command)
  eigenclass.send(:define_method, method_or_command) do |*args|
    command = args.dup.unshift(method_or_command)
    Puppet::Util::Execution.execute(command)
  end
end

#eigenclassObject



332
333
334
# File 'lib/easy_type/type.rb', line 332

def eigenclass
  class << self; self; end
end

#group(group_name = :default, &block) ⇒ Group

define a group of parameters. A group means a change in one of it’s members all the information in the group is added tot the command

rubocop:disable Alias

Examples:

group(:name) do # name is optional
   property :a
   property :b
end

Parameters:

  • group_name (Symbol) (defaults to: :default)

    the group name to use. A group name must be unique within a type

Returns:

  • (Group)

    the defined group



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/easy_type/type.rb', line 124

def group(group_name = :default, &block)
  include ::EasyType::FileIncluder

  @group_name = group_name # make it global
  @groups ||= ::EasyType::Group.new

  alias :orig_parameter :parameter
  alias :orig_property :property

  def parameter(parameter_name)
    process_group_entry(include_file("puppet/type/#{name}/#{parameter_name}"))
  end

  def property(property_name)
    process_group_entry(include_file("puppet/type/#{name}/#{property_name}"))
  end

  def process_group_entry(entry)
    @groups.add(name, entry)
  end

  yield if block

  alias :parameter :orig_parameter
  alias :property :orig_property
end

#groupsGroup

rubocop:enable Alias

Return the groups the type contains

Returns:

  • (Group)

    All defined groups



157
158
159
160
# File 'lib/easy_type/type.rb', line 157

def groups
  @groups ||= ::EasyType::Group.new
  @groups
end

#map_title_to_attributes(*attributes) { ... } ⇒ Object

easy way to map parts of a title to one of the attributes and properties.

map_title_to_attributes([:name,:domain, :jmsmodule, :queue_name]) do

/^((.*\/)?(.*):(.*)?)$/)

end

You can also pass a Hash as one of the entries in the array. The key mus be the field to map to and the value mus be a closure (Proc or a Lambda) to manage the value

default_name = lambda {| name| name.nil? ? ‘default’ : name} map_title_to_attributes([:name -> default_name,:domain, :jmsmodule, :queue_name]) do

/^((.*\/)?(.*):(.*)?)$/)

end

Parameters:

  • an (Array)

    array containing the symbols idetifying the parameters an properties to use

Yields:

  • yields regexp the regular expression to map parts of the title.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/easy_type/type.rb', line 201

def map_title_to_attributes(*attributes)
  if @deprecated_message.nil?
    Puppet.warning "Deprecated method easy_type::map_title_to_attributes used in custom type #{name}"
    @deprecated_message = true
  end
  attribute_array = attributes.map do |attr|
    case attr
    when Array  then attr
    when Symbol then [attr, nil]
    when String then [attr.to_sym, nil]
    when Hash   then attr.to_a.flatten
    else fail "map_title_to_attribute, doesn\'t support #{attr.class} as attribute"
    end
  end
  regexp = yield
  eigenclass.send(:define_method, :title_patterns) do
    [
      [
        regexp,
        attribute_array
      ]
    ]
  end
end

#map_titles_to_attributes(array) ⇒ Object

easy way to map parts of a titles to one of the attributes and properties.

map_titles_to_attributes([

/((.*))/      , [:name, :user],
/((.*)@(.*))/ , [:name, :user, :sid]
])

Parameters:

  • an (Array)

    Array containing pairs where the fisrt entry is a regular expression and the second contains an array of symbols depicting the attributes to be mapped to



174
175
176
177
178
179
# File 'lib/easy_type/type.rb', line 174

def map_titles_to_attributes(array)
  pattern_array = array.each_slice(2).map { |entry| [entry[0], entry[1].collect { |e| [e] }] }
  eigenclass.send(:define_method, :title_patterns) do
    pattern_array
  end
end

#on_notify(&block) ⇒ Object

retuns the string needed to start the creation of an sql type

Examples:

newtype(:oracle_user) do

  on_refresh do
    # restart the server
  end

Parameters:

  • block (Method)

    The code to be run on getting a notification



327
328
329
# File 'lib/easy_type/type.rb', line 327

def on_notify(&block)
  define_method(:refresh, &block) if block
end

#on_prefetch(&block) ⇒ Object

retuns an Array with all prefteched resources

Examples:

on_prefetch do
end

Parameters:

  • block (Method)

    The code to be run in provider for prefetching. If none is specfied the default will be run



311
312
313
# File 'lib/easy_type/type.rb', line 311

def on_prefetch(&block)
  eigenclass.send(:define_method, :execute_prefetch, &block)
end

#parameter(parameter_name) ⇒ Object Also known as: property

include’s the parameter declaration. It searches for the parameter file in the directory ‘puppet/type/type_name/parameter_name, or in the shared directory `puppet/type/shared`

Examples:

parameter(:name)

Parameters:

  • parameter_name (Symbol)

    the base name of the parameter



235
236
237
# File 'lib/easy_type/type.rb', line 235

def parameter(parameter_name)
  process_group_entry(include_file("puppet/type/#{name}/#{parameter_name}"))
end

#process_group_entry(entry) ⇒ Object



141
142
143
# File 'lib/easy_type/type.rb', line 141

def process_group_entry(entry)
  @groups.add(name, entry)
end

#set_command(methods_or_commands) ⇒ Object

set’s the command to be executed. If the specified argument translate’s to an existing class method on the type, this method will be identified as the command. When a class method doesn’t exist, the command will be translated to an os command

Examples:

newtype(:oracle_user) do

  command do
   :sql
  end

Parameters:

  • method_or_command (Symbol)

    method or os command name



284
285
286
287
288
289
290
291
# File 'lib/easy_type/type.rb', line 284

def set_command(methods_or_commands)
  @commands ||= []
  methods_or_commands = Array(methods_or_commands) # ensure Array
  methods_or_commands.each do |method_or_command|
    @commands << method_or_command
    define_os_command_method(method_or_command) unless methods.include?(method_or_command)
  end
end

#shared_file(parameter_name) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/easy_type/type.rb', line 253

def shared_file(parameter_name)
  #
  # For back-ward compatibility, we first search the module's private shared path
  # and then go to the overall shared path.
  #
  if defined?(module_name)
    module_file = get_ruby_file("puppet/type/shared/#{module_name}/#{parameter_name}")
  end
  shared_file = get_ruby_file("puppet/type/shared/#{parameter_name}")
  module_file ? module_file : shared_file
end

#shared_file?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/easy_type/type.rb', line 266

def shared_file?(parameter_name)
  !shared_file(parameter_name).nil?
end

#specific_file(parameter_name) ⇒ Object



243
244
245
# File 'lib/easy_type/type.rb', line 243

def specific_file(parameter_name)
  get_ruby_file("puppet/type/#{name}/#{parameter_name}")
end

#specific_file?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/easy_type/type.rb', line 248

def specific_file?(parameter_name)
  !specific_file(parameter_name).nil?
end

#to_get_raw_resources(&block) ⇒ Object

The code in the block is called to fetch all information of all available resources on the system. Although not strictly necessary, it is a convention the return an Array of Hashes

Examples:

newtype(:oracle_user) do

  to_get_raw_resourced do
   TODO: Fill in
  end

Parameters:

  • block (Method)

    The code to be run to fetch the raw resource information from the system.



373
374
375
# File 'lib/easy_type/type.rb', line 373

def to_get_raw_resources(&block)
  eigenclass.send(:define_method, :get_raw_resources, &block)
end