Module: ShadyDB::Attributes

Included in:
Document
Defined in:
lib/shadydb/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Used for allowing accessor methods for dynamic attributes. Pulled from Mongoid



32
33
34
35
36
37
38
39
40
41
# File 'lib/shadydb/attributes.rb', line 32

def method_missing(name, *args)
  attr = name.to_s
  return super unless @attributes.has_key?(attr.reader)
  if attr.writer?
    # "args.size > 1" allows to simulate 1.8 behavior of "*args"
    @attributes[attr.reader] = (args.size > 1) ? args : args.first
  else
    @attributes[attr.reader]
  end
end

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/shadydb/attributes.rb', line 5

def self.included(base)
  base.class_eval do
    extend ClassMethods
    
    cattr_accessor :fields
    self.fields = {}
    
    field :id
    
    attr_accessor :attributes, :raw
  end
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/shadydb/attributes.rb', line 22

def [](key)
  attributes[key.to_s]
end

#[]=(key, value) ⇒ Object



26
27
28
# File 'lib/shadydb/attributes.rb', line 26

def []=(key, value)
  attributes[key.to_s] = value
end

#idObject



18
19
20
# File 'lib/shadydb/attributes.rb', line 18

def id
  self[:id]
end