Module: Recliner::AttributeMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Defined in:
lib/recliner/attribute_methods.rb,
lib/recliner/attribute_methods/read.rb,
lib/recliner/attribute_methods/dirty.rb,
lib/recliner/attribute_methods/query.rb,
lib/recliner/attribute_methods/write.rb,
lib/recliner/attribute_methods/defaults.rb,
lib/recliner/attribute_methods/protected.rb,
lib/recliner/attribute_methods/before_type_cast.rb

Defined Under Namespace

Modules: BeforeTypeCast, ClassMethods, Defaults, Dirty, Protected, Query, Read, Write

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/recliner/attribute_methods.rb', line 79

def method_missing(method_id, *args, &block)
  # If we haven't generated any methods yet, generate them, then
  # see if we've created the method we're looking for.
  unless self.class.attribute_methods_generated?
    self.class.define_attribute_methods
    method_name = method_id.to_s
    
    guard_private_attribute_method!(method_name, args)
    
    if self.class.generated_attribute_methods.method_defined?(method_name)
      return self.send(method_id, *args, &block)
    end
  end
  
  super
end

Instance Method Details

#attributesObject

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.



33
34
35
# File 'lib/recliner/attribute_methods.rb', line 33

def attributes
  @attributes ||= {}
end

#attributes=(attrs) ⇒ Object

Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names).

Sensitive attributes can be protected from this form of mass-assignment by using the attr_protected macro. Or you can alternatively specify which attributes can be accessed with the attr_accessible macro. Then all the attributes not included in that won’t be allowed to be mass-assigned.

class User < Recliner::Document
  property :is_admin, Boolean
  attr_protected :is_admin
end

user = User.new
user.attributes = { :username => 'Sam', :is_admin => true }
user.username   # => "Sam"
user.is_admin?  # => false


54
55
56
57
58
# File 'lib/recliner/attribute_methods.rb', line 54

def attributes=(attrs)
  attrs.each do |key, value|
    self.send("#{key}=", value)
  end
end

#clone_attribute_value(attribute_name) ⇒ Object

def clone_attributes(reader_method = :read_attribute, attributes = {})

self.attribute_names.inject(attributes) do |attrs, name|
  attrs[name] = clone_attribute_value(reader_method, name)
  attrs
end

end



72
73
74
75
76
77
# File 'lib/recliner/attribute_methods.rb', line 72

def clone_attribute_value(attribute_name)
  value = read_attribute(attribute_name)
  value.duplicable? ? value.clone : value
rescue TypeError, NoMethodError
  value
end

#respond_to?(*args) ⇒ Boolean

Returns:



96
97
98
99
# File 'lib/recliner/attribute_methods.rb', line 96

def respond_to?(*args)
  self.class.define_attribute_methods
  super
end

#to_couchObject



61
62
63
# File 'lib/recliner/attribute_methods.rb', line 61

def to_couch
  attributes_with_class.to_couch
end