DataType API stubs should receive resource
Reported by Troy K | June 5th, 2008 @ 07:55 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 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 Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
