module Rethinker::Document::Attributes

Attributes

fields[RW]
attributes[RW]

Public Class Methods

new(attrs={}, options={}) click to toggle source
Calls superclass method
# File lib/rethinker/document/attributes.rb, line 16
def initialize(attrs={}, options={})
  super
  assign_attributes(attrs, options.reverse_merge(:pristine => true))
end

Public Instance Methods

[](name) click to toggle source
# File lib/rethinker/document/attributes.rb, line 21
def [](name)
  __send__("#{name}")
end
[]=(name, value) click to toggle source
# File lib/rethinker/document/attributes.rb, line 25
def []=(name, value)
  __send__("#{name}=", value)
end
assign_attributes(attrs, options={}) click to toggle source
# File lib/rethinker/document/attributes.rb, line 42
def assign_attributes(attrs, options={})
  reset_attributes if options[:pristine]

  # TODO Should we reject undeclared fields ?
  if options[:from_db]
    attributes.merge! attrs
  else
    if Rethinker.rails3?
      unless options[:without_protection]
        attrs = sanitize_for_mass_assignment(attrs, options[:as])
      end
    end
    attrs.each do |k,v| 
      if respond_to?("#{k}=")
        __send__("#{k}=", v)
      else 
        attributes[k.to_s] = v
      end
    end
  end
end
Also aliased as: attributes=
attributes=(attrs, options={})
Alias for: assign_attributes
inspect() click to toggle source

TODO test that thing

# File lib/rethinker/document/attributes.rb, line 66
def inspect
  attrs = self.class.fields.keys.map { |f| "#{f}: #{attributes[f.to_s].inspect}" }
  "#<#{self.class} #{attrs.join(', ')}>"
end
reset_attributes() click to toggle source
# File lib/rethinker/document/attributes.rb, line 29
def reset_attributes
  # XXX Performance optimization: we don't save field that are not
  # explicitly set. The row will therefore not contain nil for
  # unset attributes. This has some implication when using where()
  # see lib/rethinker/selection/where.rb
  self.attributes = {}

  # assign default attributes based on the field definitions
  self.class.fields.each do |name, options|
    self.__send__("#{name}=", options[:default]) if options.has_key?(:default)
  end
end