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
-
#assign_attributes(attrs, options = {}) ⇒ Object
Mass assign the document’s attributes.
-
#attributes ⇒ Hash
A copy of the values of all attributes in the Document.
-
#attributes=(attrs) ⇒ Object
Mass assign the document’s attributes.
- #initialize(attrs = {}, options = {}) {|_self| ... } ⇒ Object
- #raw_attributes ⇒ Object
- #raw_attributes=(attrs) ⇒ Object
Methods included from Translation
Instance Method Details
#assign_attributes(attrs, options = {}) ⇒ Object
Mass assign the document’s attributes.
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, ={}) raise ArgumentError, t('attribute_hash') unless(Hash === attrs) unless [:without_protection] if method(:sanitize_for_mass_assignment).arity == 1 # ActiveModel 3.0 if [:as] raise ArgumentError, t('mass_assignment_roles_unsupported') end attrs = sanitize_for_mass_assignment(attrs) else mass_assignment_role = ([: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 |
#attributes ⇒ Hash
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.
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.
94 95 96 |
# File 'lib/ripple/attribute_methods.rb', line 94 def attributes=(attrs) assign_attributes(attrs) end |
#initialize(attrs = {}, options = {}) {|_self| ... } ⇒ Object
112 113 114 115 116 117 |
# File 'lib/ripple/attribute_methods.rb', line 112 def initialize(attrs={}, ={}) super() @attributes = attributes_from_property_defaults assign_attributes(attrs, ) yield self if block_given? end |
#raw_attributes ⇒ Object
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
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 |