Problems with composed_of

Hi,

I'm having problems with composed_of

I have this two classes:

class Declaration < ActiveRecord::Base
  composed_of :amount, :class_name => "Currency", :mapping =>
%w(amount amount)
end

class Currency
  attr_accessor :round, :decimal, :amount

  def initialize(amount)
    self.amount = amount
    self.round = amount.to_i.to_s[0..-3]
    self.decimal = amount.to_i.to_s[-2..-1]
  end

end

What I want to do: I want "amount" of the declaration model to be an
object. I'm trying to execute the following;

declaration = Declaration.new

=> #<Declaration:0xb739f1b8 @attributes={"updated_at"=>nil,
"description"=>nil, "amount"=>nil, "user_id"=>nil, "created_at"=>nil},
@new_record=true>

declaration.amount = 12

NoMethodError: undefined method `amount' for 12:Fixnum
        from (eval):3:in `amount='
        from (irb):2

I would like to be able to do:
declaration.amount = 1200
declaration.amount.cents() # or something like that)

What am I doing wrong?

LeonB wrote:

class Declaration < ActiveRecord::Base
  composed_of :amount, :class_name => "Currency", :mapping =>
%w(amount amount)
end

class Currency
  attr_accessor :round, :decimal, :amount

  def initialize(amount)
    self.amount = amount
    self.round = amount.to_i.to_s[0..-3]
    self.decimal = amount.to_i.to_s[-2..-1]
  end

I would like to be able to do:
declaration.amount = 1200

declaration.amount = Currency.new(1200)

Thanks for the reply. But doing Currency.new() every time seems a bit
like a hassle.

Ah, added this tot the model:

  def amount=(amount)
    @amount = Currency.new(amount)
  end

that worked nicely.