Module: Rethinker::Document::Attributes::ClassMethods

Defined in:
lib/rethinker/document/attributes.rb

Instance Method Summary collapse

Instance Method Details

#field(name, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rethinker/document/attributes.rb', line 81

def field(name, options={})
  name = name.to_sym

  # Using a hash because:
  # - at some point, we want to associate informations with a field (like the type)
  # - it gives us a set for free
  ([self] + descendants).each do |klass|
    klass.fields[name] ||= {}
    klass.fields[name].merge!(options)
  end

  # Using a layer so the user can use super when overriding these methods
  inject_in_layer :attributes, <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}=(value)
      attributes['#{name}'] = value
    end

    def #{name}
      attributes['#{name}']
    end
  RUBY
end

#inherited(subclass) ⇒ Object



76
77
78
79
# File 'lib/rethinker/document/attributes.rb', line 76

def inherited(subclass)
  super
  subclass.fields = self.fields.dup
end

#new_from_db(attrs, options = {}) ⇒ Object



72
73
74
# File 'lib/rethinker/document/attributes.rb', line 72

def new_from_db(attrs, options={})
  klass_from_attrs(attrs).new(attrs, options.reverse_merge(:from_db => true)) if attrs
end

#remove_field(name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rethinker/document/attributes.rb', line 104

def remove_field(name)
  name = name.to_sym

  ([self] + descendants).each do |klass|
    klass.fields.delete(name)
  end

  inject_in_layer :attributes, <<-RUBY, __FILE__, __LINE__ + 1
    undef #{name}=
    undef #{name}
  RUBY
end