module Hat::Model::Attributes
Public Class Methods
new(attrs = {})
click to toggle source
# File lib/hat/model/attributes.rb, line 21 def initialize(attrs = {}) attrs = attrs.with_indifferent_access attributes.each do |attr_name, attr_options| raw_value = attrs[attr_name] == nil ? default_for(attr_options) : attrs[attr_name] set_raw_value(attr_name, raw_value, true) unless raw_value == nil end self.errors = attrs[:errors] || {} end
Private Class Methods
included(base)
click to toggle source
# File lib/hat/model/attributes.rb, line 102 def self.included(base) base.class_attribute :_attributes base._attributes = {} base.extend(ClassMethods) base.send(:attr_accessor, :errors) end
Public Instance Methods
attributes()
click to toggle source
# File lib/hat/model/attributes.rb, line 34 def attributes self.class._attributes end
has_many_changed(has_many_name)
click to toggle source
# File lib/hat/model/attributes.rb, line 38 def has_many_changed(has_many_name) send("#{has_many_name.to_s.singularize}_ids=", send(has_many_name).map(&:id).compact) end
Private Instance Methods
assert_default_type_valid(options)
click to toggle source
# File lib/hat/model/attributes.rb, line 81 def assert_default_type_valid(options) if options[:default] default_class = options[:default].class unless [Array, Hash, Integer, Float, String].include? default_class raise "Default values of type #{default_class.name} are not supported." end end end
default_for(options)
click to toggle source
make sure we don't pass references to the same default object to each instance. Copy/dup where appropriate
# File lib/hat/model/attributes.rb, line 69 def default_for(options) assert_default_type_valid(options) default = options[:default] if default.kind_of? Array [].concat(default) elsif default.kind_of? Hash default.dup else default end end
set_belongs_to_value(attr_name, value)
click to toggle source
# File lib/hat/model/attributes.rb, line 90 def set_belongs_to_value(attr_name, value) instance_variable_set("@#{attr_name}", value) send("#{attr_name}_id=", value.try(:id)) end
set_has_many_value(attr_name, value)
click to toggle source
# File lib/hat/model/attributes.rb, line 95 def set_has_many_value(attr_name, value) instance_variable_set("@#{attr_name}", HasManyArray.new(value, self, attr_name)) has_many_changed(attr_name) end
set_raw_value(attr_name, raw_value, apply_if_read_only = false)
click to toggle source
# File lib/hat/model/attributes.rb, line 44 def set_raw_value(attr_name, raw_value, apply_if_read_only = false) attr_def = attributes[attr_name] value = transformed_value(attr_def[:type], raw_value) if attr_def[:read_only] && apply_if_read_only instance_variable_set("@#{attr_name}", value) elsif self.send("#{attr_name}=", value) end end
transformed_value(type, raw_value)
click to toggle source
# File lib/hat/model/attributes.rb, line 56 def transformed_value(type, raw_value) if type transformer_registry.from_raw(type, raw_value) else raw_value end end
transformer_registry()
click to toggle source
# File lib/hat/model/attributes.rb, line 64 def transformer_registry Transformers::Registry.instance end