Sha256: f99e0af97ed0b4fd0438b76ea5c24c0afe2ef24186173ead959a117cbb4f06ad

Contents?: true

Size: 1.89 KB

Versions: 36

Compression:

Stored size: 1.89 KB

Contents

ActiveRecord::Base.class_eval do
  class << self

    def can_wrap_with_hobo_type?(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

    # Define an attribute reader method.  Cope with nil column.
    def define_read_method(symbol, attr_name, column)

      cast_code = column.type_cast_code('v') if column
      access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"

      unless attr_name.to_s == self.primary_key.to_s
        access_code = access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
      end

      # This is the Hobo hook - add a type wrapper around the field
      # value if we have a special type defined
      if can_wrap_with_hobo_type?(symbol)
        access_code = "val = begin; #{access_code}; end; wrapper_type = self.class.attr_type(:#{attr_name}); " +
            "if HoboFields.can_wrap?(wrapper_type, val); wrapper_type.new(val); else; val; end"
      end

      if cache_attribute?(attr_name)
        access_code = "@attributes_cache['#{attr_name}'] ||= begin; #{access_code}; end;"
      end

      generated_attribute_methods.module_eval("def #{symbol}; #{access_code}; end", __FILE__, __LINE__)
    end

    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

  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
hobo_fields-1.3.3 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.2 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.1 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre8 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre7 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre6 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre5 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre4 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre3 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.4.0.pre2 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.RC4 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.RC3 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.RC2 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.RC1 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.RC lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.pre31 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.pre29 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.pre28 lib/hobo_fields/extensions/active_record/attribute_methods.rb
hobo_fields-1.3.0.pre27 lib/hobo_fields/extensions/active_record/attribute_methods.rb