# File lib/hobo_fields/extensions/active_record/attribute_methods.rb, line 21 def can_wrap_with_hobo_type?(attr_name) @can_wrap_cache ||= {} if @can_wrap_cache.include?(attr_name) @can_wrap_cache[attr_name] else @can_wrap_cache[attr_name] = if connected? type_wrapper = try.attr_type(attr_name) type_wrapper.is_a?(Class) && type_wrapper.not_in?(HoboFields::PLAIN_TYPES.values) else false end end end
# File lib/hobo_fields/extensions/active_record/attribute_methods.rb, line 36 def define_method_attribute=(attr_name) if can_wrap_with_hobo_type?(attr_name) src = "begin; wrapper_type = self.class.attr_type(:#{attr_name}); " + "if !new_value.is_a?(wrapper_type) && HoboFields.can_wrap?(wrapper_type, new_value); wrapper_type.new(new_value); else; new_value; end; end" generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', #{src}); end", __FILE__, __LINE__) else super end end
# File lib/hobo_fields/extensions/active_record/fields_declaration.rb, line 3 def self.fields(include_in_migration = true, &b) # Any model that calls 'fields' gets a bunch of other # functionality included automatically, but make sure we only # include it once include HoboFields::Model unless HoboFields::Model.in?(included_modules) @include_in_migration ||= include_in_migration if b dsl = HoboFields::FieldDeclarationDsl.new(self) if b.arity == 1 yield dsl else dsl.instance_eval(&b) end end end
# File lib/hobo_fields/extensions/active_record/attribute_methods.rb, line 2 def _read_attribute_with_hobo(attr_name) name = attr_name.to_s if self.class.can_wrap_with_hobo_type?(name) attr_name = attr_name.to_sym val = _read_attribute_without_hobo(name) wrapper_type = self.class.attr_type(attr_name) if HoboFields.can_wrap?(wrapper_type, val) wrapper_type.new(val) else val end else _read_attribute_without_hobo(name) end end