Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Property Improved #139

Merged
merged 12 commits into from
Sep 12, 2011
Merged

Property Improved #139

merged 12 commits into from
Sep 12, 2011

Conversation

solnic
Copy link
Contributor

@solnic solnic commented Sep 11, 2011

Hey!

This is still a WIP but I'm opening a pull request early so we can be discussing changes while I'm finishing this stuff.

This branch makes two relatively small but significant changes. First it adds dump_as/load_as property options as described here: https://gist.github.com/784350; it also removes typecast logic from dm-core and replaces it with Virtus Coercion system.

Let me know what you think :)

@primitive = self.class.primitive
@field = @options[:field].freeze unless @options[:field].nil?
@default = @options[:default]
@field = @options[:field].freeze unless @options[:field].nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize the original code did this, but this seems to be freezing the object that was provided to the method, even if it is a hash value.

In general we try not to mutate any objects passed into methods, unless that is the method's primary purpose (which is rare).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The safest approach for freezing objects can be seen in veritas: https://github.com/dkubb/veritas/blob/master/lib/veritas/support/immutable.rb#L81-116

@dkubb
Copy link
Member

dkubb commented Sep 12, 2011

@solnic this is a pretty amazing reduction in code. Do the dm-types specs pass with this change?

Most of the comments I've left are really minor in nature, I really think this is a pretty awesome change. I love removing code from DM and replacing it with library code that's better tested. It's also a nice way to get virtus used more now.

solnic added a commit that referenced this pull request Sep 12, 2011
@solnic solnic merged commit a42c0ff into datamapper:master Sep 12, 2011
@@ -266,7 +266,7 @@ module DataMapper
:unique => @unique
)

if target_property.primitive == Integer
if target_property.instance_of?(Property::Integer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this purpose of this was to catch any property subclasses with Integer primitives, not necessarily only Property::Integer classes.

However, for other parts of DM to work, people would probably have to subclass Property::Integer anyway if the they needed to store an Integer, so maybe this should use #kind_of? ? What do you think @solnic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkubb yes in other gems (dm-validations for example) I used #kind_of? cause there we may deal with custom subclasses; notice that this code here deals with FKs which currently are always created as Integer properties. For this to fail somebody would have to manually create an fk using an integer sub-class. Maybe "just in case" we should use #kind_of? here too. I don't have a strong opinion to be honest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to use the strongest match that works, and then fallback to something less specific or duck typing if I have a valid use case.

In this case I think we have a valid use case though, and it's probably best handled by duck typing rather than asserting the class. I would probably change this to if target_property.respond_to?(:min) && target_property.respond_to?(:max), which will pass-through the min/max if the property type supports it.

@emmanuel
Copy link
Member

This is awesome! Nice work @solnic!

@solnic do you have any further thoughts on ripping out the Property#get and #set APIs and reworking DM::Property to has-a Virtus::Attribute?

On Sep 11, 2011, at 1:33 PM, Piotr Solnica wrote:

Hey!

This is still a WIP but I'm opening a pull request early so we can be discussing changes while I'm finishing this stuff.

This branch makes two relatively small but significant changes. First it adds dump_as/load_as property options as described here: https://gist.github.com/784350; it also removes typecast logic from dm-core and replaces it with Virtus Coercion system.

Let me know what you think :)

You can merge this Pull Request by running:

git pull https://github.com/solnic/dm-core property-improved

Or you can view, comment on it, or merge it online at:

#139

-- Commit Summary --

  • require virtus
  • Replace "primitive" with "dump_as" and add a new option "load_as" (WIP)
  • Integrate with Virtus Coercion system (WIP)

-- File Changes --

M Gemfile (1)
M lib/dm-core.rb (10)
M lib/dm-core/property.rb (64)
M lib/dm-core/property/binary.rb (2)
M lib/dm-core/property/boolean.rb (26)
M lib/dm-core/property/class.rb (19)
M lib/dm-core/property/date.rb (40)
M lib/dm-core/property/date_time.rb (39)
M lib/dm-core/property/decimal.rb (21)
M lib/dm-core/property/discriminator.rb (4)
M lib/dm-core/property/float.rb (17)
M lib/dm-core/property/integer.rb (17)
M lib/dm-core/property/lookup.rb (9)
M lib/dm-core/property/numeric.rb (8)
M lib/dm-core/property/object.rb (23)
M lib/dm-core/property/serial.rb (4)
M lib/dm-core/property/string.rb (19)
M lib/dm-core/property/text.rb (7)
M lib/dm-core/property/time.rb (41)
D lib/dm-core/property/typecast/numeric.rb (32)
D lib/dm-core/property/typecast/time.rb (33)
M lib/dm-core/spec/shared/public/property_spec.rb (14)
M lib/dm-core/spec/shared/semipublic/property_spec.rb (16)
M spec/public/property/binary_spec.rb (4)
M spec/public/property/boolean_spec.rb (4)
M spec/public/property/class_spec.rb (4)
M spec/public/property/date_spec.rb (4)
M spec/public/property/date_time_spec.rb (4)
M spec/public/property/decimal_spec.rb (4)
M spec/public/property/discriminator_spec.rb (2)
M spec/public/property/float_spec.rb (4)
M spec/public/property/integer_spec.rb (4)
M spec/public/property/object_spec.rb (2)
M spec/public/property/serial_spec.rb (4)
M spec/public/property/string_spec.rb (4)
M spec/public/property/text_spec.rb (4)
M spec/public/property/time_spec.rb (4)
M spec/semipublic/property/boolean_spec.rb (2)
M spec/semipublic/property/class_spec.rb (2)
M spec/semipublic/property/date_spec.rb (8)
M spec/semipublic/property/date_time_spec.rb (14)
M spec/semipublic/property/decimal_spec.rb (2)
M spec/semipublic/property/float_spec.rb (2)
M spec/semipublic/property/integer_spec.rb (2)
M spec/semipublic/property/time_spec.rb (14)

-- Patch Links --

https://github.com/datamapper/dm-core/pull/139.patch
https://github.com/datamapper/dm-core/pull/139.diff

Reply to this email directly or view it on GitHub:
#139

@solnic
Copy link
Contributor Author

solnic commented Sep 13, 2011

@emmanuel thanks man, it was fun :)

I'm not sure how to tackle the virtus transition to be honest. I still need to think about possible solutions. @dkubb suggested that I could turn Property into a sub-class of Virtus::Attribute to ease the transition. That seems like a good idea.

What do you mean by 'has-a Virtus::Attribute'? Effectively we want to rip out Property and have it replaced by Virtus.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants