Module: Opium::Model::Attributable

Extended by:
ActiveSupport::Concern
Defined in:
lib/opium/model/attributable.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



14
15
16
# File 'lib/opium/model/attributable.rb', line 14

def attributes
  @attributes ||= {}.with_indifferent_access
end

#attributes=(values) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/opium/model/attributable.rb', line 18

def attributes=(values)
  sanitize_for_mass_assignment( rubyize_field_names( values ) ).each do |k, v|
    field_info, setter = self.class.fields[k], :"#{k}="
    if field_info.present? || self.respond_to?( setter )
      send( setter, v )
    else
      attributes[k] = v
    end
  end
end

#attributes_to_parse(options = {}) ⇒ Object



29
30
31
32
# File 'lib/opium/model/attributable.rb', line 29

def attributes_to_parse( options = {} )
  options[:except] ||= self.class.fields.values.select {|f| f.readonly? || f.virtual? }.map {|f| f.name} if options[:not_readonly]
  Hash[*self.as_json( options ).flat_map {|k, v| [self.class.fields[k].name_to_parse, self.class.fields[k].type.to_parse(v)]}]
end

#initialize(attributes = {}) ⇒ Object



10
11
12
# File 'lib/opium/model/attributable.rb', line 10

def initialize( attributes = {} )
  super( self.class.default_attributes( self ).merge attributes )
end