class PuppetGenerator::Models::Base

Attributes

name[R]

Public Class Methods

all() click to toggle source
# File lib/puppet_generator/models/base.rb, line 75
def all
  @instances.to_a
end
all_names_as_string(connector=", ") click to toggle source

return all names as string

# File lib/puppet_generator/models/base.rb, line 80
def all_names_as_string(connector=", ")
  find_all(enabled: true).map(&:name).sort.join(connector)
end
clear() click to toggle source
# File lib/puppet_generator/models/base.rb, line 71
def clear
  @instances = Set.new
end
create( *args, &block ) click to toggle source
# File lib/puppet_generator/models/base.rb, line 51
def create( *args, &block )
  if block_given?
    register( new( *args, &block ) )
  else
    register( new( *args ) )
  end
end
delete( val ) click to toggle source
# File lib/puppet_generator/models/base.rb, line 63
def delete( val )
  element = find( val.to_s.to_sym )
  raise Exceptions::InstanceNotFound unless element
  @instances.delete element

  element
end
enable(name) click to toggle source

enables a specific instance

# File lib/puppet_generator/models/base.rb, line 85
def enable(name)
  find(name: name).enable
end
find( val ) click to toggle source
# File lib/puppet_generator/models/base.rb, line 59
def find( val )
  @instances.find { |i| i.name == val.to_s.to_sym }
end
find_all( criteria={} ) click to toggle source

finds all instances

# File lib/puppet_generator/models/base.rb, line 95
def find_all( criteria={} )
  PuppetGenerator::Models.logger.debug(self) { "Criteria for search: #{ criteria }" }
  criteria = { name: criteria.to_sym } if criteria.kind_of? Symbol or criteria.kind_of? String

  PuppetGenerator::Models.logger.debug(self) { "Instances to be searched for: #{ @instances.map { |i| "#{i.name} (#{i.class})" }.join(", ") }" }
  @instances.find_all do |i| 
    criteria.all? do |c,v|

      PuppetGenerator::Models.logger.debug(self) { "Check method for search: #{ c }" }
      i.send( "#{c}?".to_sym , v )
    end
  end

rescue NameError => e
  raise Exceptions::InvalidSearchCriteria, e.message
end
inherited(base) click to toggle source
# File lib/puppet_generator/models/base.rb, line 11
def self.inherited(base)
  base.instance_variable_set(:@instances, Set.new)
end
new(name) click to toggle source
# File lib/puppet_generator/models/base.rb, line 15
def initialize(name)
  @name = name.to_sym
  @enabled = false
end
register(element) click to toggle source

attr_accessor :instances

# File lib/puppet_generator/models/base.rb, line 45
def register(element)
  @instances << element

  element
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/puppet_generator/models/base.rb, line 34
def <=>(other)
  name <=> other.name
end
enable() click to toggle source

enable action

# File lib/puppet_generator/models/base.rb, line 21
def enable
  @enabled = true
end
enabled?(val=true) click to toggle source

check if action is enabled

# File lib/puppet_generator/models/base.rb, line 26
def enabled?(val=true)
  @enabled == val
end
eql?(other) click to toggle source
# File lib/puppet_generator/models/base.rb, line 38
def eql?(other)
  name == other.name
end
name?(name) click to toggle source
# File lib/puppet_generator/models/base.rb, line 30
def name?(name)
  @name == name
end