Class: Spice::Base
- Inherits:
-
Object
- Object
- Spice::Base
- Defined in:
- lib/spice/base.rb
Direct Known Subclasses
Client, Cookbook, CookbookVersion, DataBag, DataBagItem, Environment, Node, Role
Constant Summary collapse
- @@identity_map =
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.
- #id ⇒ Integer
-
#initialize(attrs = Mash.new) ⇒ Spice::Base
constructor
Initializes a new object.
-
#keys ⇒ Object
def name.
- #name ⇒ String
-
#update(attrs) ⇒ Spice::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = Mash.new) ⇒ Spice::Base
Initializes a new object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/spice/base.rb', line 53 def initialize(attrs=Mash.new) self.class.attr_reader *attrs.keys attrs.stringify_keys! if attrs['name'] self.update(attrs) @@identity_map[self.class] ||= {} @@identity_map[self.class][attrs['name']] = self elsif attrs['id'] self.update(attrs) @@identity_map[self.class] ||= {} @@identity_map[self.class][attrs['id']] = self else self.update(attrs) @@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.
5 6 7 |
# File 'lib/spice/base.rb', line 5 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
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/spice/base.rb', line 20 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
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/spice/base.rb', line 33 def self.get(attrs=Mash.new) @@identity_map[self] ||= {} if attrs['name'] @@identity_map[self][attrs['name']] && @@identity_map[self][attrs['name']].update(attrs) elsif attrs['id'] @@identity_map[self][attrs['id']] && @@identity_map[self][attrs['id']].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
45 46 47 |
# File 'lib/spice/base.rb', line 45 def self.get_or_new(attrs=Mash.new) self.get(attrs) || self.new(attrs) end |
.identity_map ⇒ Object
10 11 12 |
# File 'lib/spice/base.rb', line 10 def self.identity_map @@identity_map end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
74 75 76 77 78 |
# File 'lib/spice/base.rb', line 74 def [](method) self.__send__(method.to_sym) rescue NoMethodError nil end |
#id ⇒ Integer
90 91 92 |
# File 'lib/spice/base.rb', line 90 def id @attrs['id'] end |
#keys ⇒ Object
def name
99 100 101 |
# File 'lib/spice/base.rb', line 99 def keys @attrs.keys end |
#name ⇒ String
95 96 97 |
# File 'lib/spice/base.rb', line 95 def name @attrs['name'] end |
#update(attrs) ⇒ Spice::Base
Update the attributes of an object
84 85 86 87 |
# File 'lib/spice/base.rb', line 84 def update(attrs) @attrs = attrs self end |