Module: Ripple::AttributeMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, Translation
Defined in:
lib/ripple/attribute_methods.rb,
lib/ripple/attribute_methods/read.rb,
lib/ripple/attribute_methods/dirty.rb,
lib/ripple/attribute_methods/query.rb,
lib/ripple/attribute_methods/write.rb

Overview

Makes ActiveRecord-like attribute accessors based on your Document‘s properties.

Defined Under Namespace

Modules: ClassMethods, Dirty, Query, Read, Write

Instance Method Summary collapse

Methods included from Translation

#i18n_scope

Instance Method Details

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

Mass assign the document’s attributes.

Parameters:

  • attrs (Hash)

    the attributes to assign

  • options (Hash) (defaults to: {})

    assignment options

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ripple/attribute_methods.rb', line 68

def assign_attributes(attrs, options={})
  raise ArgumentError, t('attribute_hash') unless(Hash === attrs)

  unless options[:without_protection]
    if method(:sanitize_for_mass_assignment).arity == 1 # ActiveModel 3.0
      if options[:as]
        raise ArgumentError, t('mass_assignment_roles_unsupported')
      end
      attrs = sanitize_for_mass_assignment(attrs)
    else
      mass_assignment_role = (options[:as] || :default)
      attrs = sanitize_for_mass_assignment(attrs, mass_assignment_role)
    end
  end

  attrs.each do |k,v|
    if respond_to?("#{k}=")
      __send__("#{k}=",v)
    else
      raise ArgumentError, t('undefined_property', :prop => k, :class => self.class.name)
    end
  end
end

#attributesHash

A copy of the values of all attributes in the Document. The result is not memoized, so use sparingly. This does not include associated objects, nor embedded documents.

Returns:

  • (Hash)

    all document attributes, by key



54
55
56
# File 'lib/ripple/attribute_methods.rb', line 54

def attributes
  raw_attributes.reject { |k, v| !respond_to?(k) }
end

#attributes=(attrs) ⇒ Object

Mass assign the document’s attributes.

Parameters:

  • attrs (Hash)

    the attributes to assign



94
95
96
# File 'lib/ripple/attribute_methods.rb', line 94

def attributes=(attrs)
  assign_attributes(attrs)
end

#initialize(attrs = {}, options = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



112
113
114
115
116
117
# File 'lib/ripple/attribute_methods.rb', line 112

def initialize(attrs={}, options={})
  super()
  @attributes = attributes_from_property_defaults
  assign_attributes(attrs, options)
  yield self if block_given?
end

#raw_attributesObject



58
59
60
61
62
63
# File 'lib/ripple/attribute_methods.rb', line 58

def raw_attributes
  self.class.properties.values.inject(@attributes.with_indifferent_access) do |hash, prop|
    hash[prop.key] = attribute(prop.key)
    hash
  end
end

#raw_attributes=(attrs) ⇒ Object

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ripple/attribute_methods.rb', line 99

def raw_attributes=(attrs)
  raise ArgumentError, t('attribute_hash') unless Hash === attrs
  attrs.each do |k,v|
    next if k.to_sym == :key
    if respond_to?("#{k}=")
      __send__("#{k}=",v)
    else
      __send__(:attribute=,k,v)
    end
  end
end