#351 invalid
Troy K

DataType API stubs should receive resource

Reported by Troy K | June 5th, 2008 @ 07:56 PM

DataTypes would be a lot more powerful if they received the resource as well, I purpose to change DataType.load/dump(value,property) to DataType.load/dump(value, property, resource).

This would give the datatype more control over setting dirty, etc. For example, I have a legacy database where names are stored in a single char field, I am currently doing the following, but was thinking how cool it would be to move this to a datatype so that I do not have to override the name accessors.

When I implemented name as a datatype, the following happens:

>> p = Person[1]
>> p.name.first = 'some other value'
>> p.dirty?
=> false

The below code works as expected.

class Person < Contact
  class Name
    [:last,:first,:middle].each do |method|
      class_eval "def #{method}; @value[:#{method}]; end;\n"
      class_eval "def #{method}=(value); @value[:#{method}] = value.to_s.gsub(',',''); dump(); end;\n"
    end
    
    def initialize(resource, value)
      @resource = resource
      case value
      when nil, Hash
        @value = value || {}
      when String
        values = value.split(',')
        @value = {:last=>values[0],:first=>values[1],:middle=>values[2]}
      end
    end
    
    def dump
      @resource.attribute_set(:name,"#{self.last},#{self.first},#{self.middle}")
    end
  end
  
  property :company_id, Integer
  property :name, String, :nullable => false
  property :title, String
  
  def name; @custom_name ||= Name.new(self, attribute_get(:name)); end;
  def name=(value)
    @custom_name = Name.new(self,value)
    @custom_name.dump
  end
end

Comments and changes to this ticket

  • Sam Smoot

    Sam Smoot June 6th, 2008 @ 12:31 AM

    • State changed from “new” to “invalid”

    This specific problem is solved by the Property tracking strategies being implemented (:load, :get, :set, :hash, false).

    Until we have a compelling use-case, I'd say we hold off on tweaking the API.

    You could sub-class the custom-type after all without much effort.

    So I'm not rejecting the idea out of hand. Just saying I'd like to see a really compelling use-case (with implementation) before changing the API, and this case is (will be) already covered.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags

Pages