module Opium::Model::Fieldable::ClassMethods
Public Instance Methods
default_attributes( context = nil )
click to toggle source
# File lib/opium/model/fieldable.rb, line 40 def default_attributes( context = nil ) fields.transform_values {|field| field.contextual_default_value( context ) }.with_indifferent_access end
field( name, options = {} )
click to toggle source
# File lib/opium/model/fieldable.rb, line 15 def field( name, options = {} ) create_field_from( name.to_sym, options ).tap do |field| create_field_getter_for( field ) create_field_setter_for( field ) end end
fields()
click to toggle source
# File lib/opium/model/fieldable.rb, line 28 def fields @fields ||= {}.with_indifferent_access end
has_field?( field_name )
click to toggle source
# File lib/opium/model/fieldable.rb, line 22 def has_field?( field_name ) fields.key? field_name end
Also aliased as: field?
parse_canonical_field_names()
click to toggle source
# File lib/opium/model/fieldable.rb, line 36 def parse_canonical_field_names @parse_canonical_field_names ||= {}.with_indifferent_access end
ruby_canonical_field_names()
click to toggle source
# File lib/opium/model/fieldable.rb, line 32 def ruby_canonical_field_names @ruby_canonical_field_names ||= {}.with_indifferent_access end
Private Instance Methods
create_field_from( name, options )
click to toggle source
# File lib/opium/model/fieldable.rb, line 46 def create_field_from( name, options ) field = Field.new( name, options[:type] || Object, options[:default], options[:readonly] || false, options[:as] ) ruby_canonical_field_names[name] = ruby_canonical_field_names[field.name_to_parse] = name.to_s parse_canonical_field_names[name] = parse_canonical_field_names[field.name_to_parse] = field.name_to_parse.to_s fields[name] = field end
create_field_getter_for( field )
click to toggle source
# File lib/opium/model/fieldable.rb, line 53 def create_field_getter_for( field ) class_eval do define_attribute_methods [field.name] define_method(field.name) do self.attributes[field.name] end end end
create_field_setter_for( field )
click to toggle source
# File lib/opium/model/fieldable.rb, line 62 def create_field_setter_for( field ) class_eval do define_method("#{ field.name }=") do |value| converted = field.type.to_ruby(value) send( "#{ field.name }_will_change!" ) unless self.attributes[field.name] == converted if field.relation? converted = field.contextual_default_value( self ) unless converted converted.owner ||= self converted.metadata ||= self.class.relations[field.name] end self.attributes[field.name] = converted end send(:private, "#{ field.name }=") if field.readonly? end end