Class: PuppetRestClient::DB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-rest/db/entities/base.rb

Direct Known Subclasses

Fact, Node, Resource

Constant Summary collapse

@@identity_map =
PuppetRestClient::IdentityMap.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = Mash.new) ⇒ PuppetRestClient::Base

Initializes a new object

Parameters:

  • attrs (Hash) (defaults to: Mash.new)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puppet-rest/db/entities/base.rb', line 51

def initialize(attrs=Mash.new)
  self.class.attr_reader *attrs.keys
  attrs.stringify_keys!
  self.update attrs
  if attrs['title']
    @@identity_map[self.class] ||= {}
    @@identity_map[self.class][attrs['title']] = self
  elsif attrs['resource']
    @@identity_map[self.class] ||= {}
    @@identity_map[self.class][attrs['resource']] = self
  else
    @@identity_map[self.class] ||= {}
    @@identity_map[self.class][Marshal.dump(attrs)] = self
  end
end

Instance Attribute Details

#attrsObject Also known as: to_hash

Returns the value of attribute attrs.



3
4
5
# File 'lib/puppet-rest/db/entities/base.rb', line 3

def attrs
  @attrs
end

Class Method Details

.self.attr_reader(attr) ⇒ Object .self.attr_reader(attrs) ⇒ Object

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

Overloads:

  • .self.attr_reader(attr) ⇒ Object

    Parameters:

    • attr (Symbol)
  • .self.attr_reader(attrs) ⇒ Object

    Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet-rest/db/entities/base.rb', line 18

def self.attr_reader(*attrs)
  attrs.each do |attribute|
    class_eval do
      define_method attribute do
        @attrs[attribute.to_s]
      end
      define_method "#{attribute}=" do |value|
        @attrs[attribute.to_s] = value
      end
    end
  end
end

.get(attrs = Mash.new) ⇒ Object

def self.attr_reader



31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet-rest/db/entities/base.rb', line 31

def self.get(attrs=Mash.new)
  @@identity_map[self] ||= {}
  if attrs['title']
    @@identity_map[self][attrs['title']] && @@identity_map[self][attrs['title']].update(attrs)
  elsif attrs['resource']
    @@identity_map[self][attrs['resource']] && @@identity_map[self][attrs['resource']].update(attrs)
  else
    @@identity_map[self][Marshal.dump(attrs)]
  end
end

.get_or_new(attrs = Mash.new) ⇒ Object

Retrieve an object from the identity map or initialize a new object



43
44
45
# File 'lib/puppet-rest/db/entities/base.rb', line 43

def self.get_or_new(attrs=Mash.new)
  self.get(attrs) || self.new(attrs)
end

.identity_mapObject



8
9
10
# File 'lib/puppet-rest/db/entities/base.rb', line 8

def self.identity_map
  @@identity_map
end

Instance Method Details

#[](method) ⇒ Object

Fetches an attribute of an object using hash notation

Parameters:

  • method (String, Symbol)

    Message to send to the object



70
71
72
73
74
# File 'lib/puppet-rest/db/entities/base.rb', line 70

def [](method)
  self.__send__(method.to_sym)
rescue NoMethodError
  nil
end

#keysObject

def name



95
96
97
# File 'lib/puppet-rest/db/entities/base.rb', line 95

def keys
  @attrs.keys
end

#resourceString

Returns:

  • (String)


86
87
88
# File 'lib/puppet-rest/db/entities/base.rb', line 86

def resource
  @attrs['resource']
end

#titleString

Returns:

  • (String)


91
92
93
# File 'lib/puppet-rest/db/entities/base.rb', line 91

def title
  @attrs['title']
end

#update(attrs) ⇒ PuppetRestClient::Base

Update the attributes of an object

Parameters:

Returns:

  • (PuppetRestClient::Base)


80
81
82
83
# File 'lib/puppet-rest/db/entities/base.rb', line 80

def update(attrs)
  @attrs = attrs
  self
end