module Rethinker::Document::Attributes::ClassMethods

Public Instance Methods

field(name, options={}) click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/rethinker/document/attributes.rb, line 76
def inherited(subclass)
  super
  subclass.fields = self.fields.dup
end
new_from_db(attrs, options={}) click to toggle source
# 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) click to toggle source
# 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