Class: Spice::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spice/base.rb

Constant Summary collapse

@@identity_map =
IdentityMap.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a new object

Parameters:

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

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

#attrsObject 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

Overloads:

  • .self.attr_reader(attr) ⇒ Object

    Parameters:

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

    Parameters:


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_mapObject


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

Parameters:

  • method (String, Symbol)

    Message to send to the object


74
75
76
77
78
# File 'lib/spice/base.rb', line 74

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

#idInteger

Returns:

  • (Integer)

90
91
92
# File 'lib/spice/base.rb', line 90

def id
  @attrs['id']
end

#keysObject

def name


99
100
101
# File 'lib/spice/base.rb', line 99

def keys
  @attrs.keys
end

#nameString

Returns:

  • (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

Parameters:

Returns:


84
85
86
87
# File 'lib/spice/base.rb', line 84

def update(attrs)
  @attrs = attrs
  self
end