module Opium::Model::Attributable
Public Class Methods
new( attributes = {} )
click to toggle source
Calls superclass method
# File lib/opium/model/attributable.rb, line 10 def initialize( attributes = {} ) super( self.class.default_attributes( self ).merge attributes ) end
Public Instance Methods
attributes()
click to toggle source
# File lib/opium/model/attributable.rb, line 14 def attributes @attributes ||= {}.with_indifferent_access end
attributes=(values)
click to toggle source
# 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}=" unless field_info.present? || self.respond_to?( setter ) self.class.send(:attr_accessor, k) end send( setter, v ) end end
attributes_to_parse( options = {} )
click to toggle source
# File lib/opium/model/attributable.rb, line 28 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
Private Instance Methods
rubyize_field_names( hash )
click to toggle source
# File lib/opium/model/attributable.rb, line 35 def rubyize_field_names( hash ) hash.transform_keys {|k| self.class.ruby_canonical_field_names[k] || k} end