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

Accessing the resource inside load/dump hooks #255

Open
mmuddasir opened this issue Jun 18, 2013 · 3 comments
Open

Accessing the resource inside load/dump hooks #255

mmuddasir opened this issue Jun 18, 2013 · 3 comments

Comments

@mmuddasir
Copy link

I wrote a custom datamapper property & defined load & dump methods but the value that I return is dependent on one of the resource's property, For example the model is say

class person
  property amount, CustomType
  property currency, String
end

module DataMapper
  class Property
    class CustomType < DataMapper::Property::Decimal

      def dump(value)
        #If the user wanted to save 23.5 then I'd multiply the number with it's currency precision & store it in the database
      end

      def load(value)
        # while retrieving i'll divide the value with the precision & give it to the user
      end   
    end  
  end
end

The question is how can I get the currency attribute of the resource inside load/dump methods ?

@mbj
Copy link
Member

mbj commented Jun 18, 2013

@mmuddasir You cannot get the resource in that context. And IMHO you should not.

Why dont you handle this "domain specific" stuff at your model?

module YourModel
  def price
     YourPriceClass.create_somehow(value, currency)
  end
end

You can also write an assignment method writing the "split" values back to properties.

@mmuddasir
Copy link
Author

@mbj Basically you want me to modify the getters & setters right ? I tried that too but in that case If I do aggregates on the values I'll get a different result, is there a way we can override aggregates methods for this specific property of the model & let it return the calculated value instead of the aggregation of the values stored in the db ?

@mbj
Copy link
Member

mbj commented Jun 19, 2013

@mmuddasir Yeah, I pointed you to a workaround. Dunno aggregates where in the game! So I cannot point you to a better solution currently. I'm not deep inside dm-1, sorry.

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

No branches or pull requests

2 participants