Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally combining multiple attributes #45

Open
brewpoo opened this issue May 21, 2012 · 0 comments
Open

Conditionally combining multiple attributes #45

brewpoo opened this issue May 21, 2012 · 0 comments

Comments

@brewpoo
Copy link

brewpoo commented May 21, 2012

Using ROXML with dm-core here. I am dealing with a poorly designed XML schema so I need to conditionally set a DM property from one of two xml attributes.

class Sample < Model
  include ROXML
  include DatamMapper::Resource

  xml_name "SAMPLE"
  xml_convention :upcase

  property :name, String
  xml_reader :name

  property :amount, String

  xml_reader :display_amount
  xml_reader :uncast_amount, :from => "AMOUNT"

  def amount
    display_amount || "#{uncast_amount} liters"
  end

end

I was trying to do this via DataMappers after :create hook but this does not get fired until I explicitly save the object. I know I can use a block to set a value but cannot get this to work. In my case AMOUNT is "always" present so I've tried something like:

xml_reader :display_amount
xml_reader(:amount) { |x| x = display_amount || x}

This obviously doesn't work as the block only receives the value returned from xml_reader.

Any suggestions?

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

No branches or pull requests

1 participant