Class: PuppetRestClient::DB::Base
- Inherits:
-
Object
- Object
- PuppetRestClient::DB::Base
- Defined in:
- lib/puppet-rest/db/entities/base.rb
Constant Summary collapse
- @@identity_map =
PuppetRestClient::IdentityMap.new
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_hash)
Returns the value of attribute attrs.
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key.
-
.get(attrs = Mash.new) ⇒ Object
def self.attr_reader.
-
.get_or_new(attrs = Mash.new) ⇒ Object
Retrieve an object from the identity map or initialize a new object.
- .identity_map ⇒ Object
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
-
#initialize(attrs = Mash.new) ⇒ PuppetRestClient::Base
constructor
Initializes a new object.
-
#keys ⇒ Object
def name.
- #resource ⇒ String
- #title ⇒ String
-
#update(attrs) ⇒ PuppetRestClient::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = Mash.new) ⇒ PuppetRestClient::Base
Initializes a new object
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
#attrs ⇒ Object 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
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_map ⇒ Object
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
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 |
#keys ⇒ Object
def name
95 96 97 |
# File 'lib/puppet-rest/db/entities/base.rb', line 95 def keys @attrs.keys end |
#resource ⇒ String
86 87 88 |
# File 'lib/puppet-rest/db/entities/base.rb', line 86 def resource @attrs['resource'] end |
#title ⇒ 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
80 81 82 83 |
# File 'lib/puppet-rest/db/entities/base.rb', line 80 def update(attrs) @attrs = attrs self end |