Can I invoke an arbitrary setter method during #make?

25 views
Skip to first unread message

ch...@flippa.com

unread,
May 24, 2011, 12:06:54 PM5/24/11
to Machinist Users
Hi,

I'm using DataMapper, so I'm working with Machinist 1.0.

I have a model like:

class User
include DataMapper::Resource

# ... properties etc ...

property :password_hash, String
property :password_salt, String

def password=(password)
self.password_salt = random_salt
self.password_hash = hashed_password(password)
end
end

Obviously here there's no `#password` attribute, since you can't
recover the actual password once it's hashed.

In my blueprint I can do:

User.blueprint do
username Faker::Internet.user_name
password "password"
end

And when I call User.make I can verify that this user has the password
"password". But what I can't seem to do is override that password:

@user = User.make(:password => "another")

It produces an error:

NoMethodError:
undefined method `password' for #<User:0xa72e980>
# ./spec/support/blueprints.rb:30:in `block in <top (required)>'
# ./spec/models/user_spec.rb:6:in `block (2 levels) in <top
(required)>'

I'm not really sure why it wants `#password` to be defined when all it
needs to do is invoke the setter. Is this a bug, or a limitation?
Looking through the DataMapperAdapter class, it doesn't seem like the
problem arises there. It happens somewhere inside Lathe, I think.
Still poking around though. I tried defining `attr_writer :password`,
but it still produces the same error. I can silence the error by
defining a password method, but that's not the solution. Obviously
I'm just doing this in two steps now, but is this a feature or a
bug? ;)

Cheers,

Chris

PS: If nobody is working on DataMapper support on Machinist 2, I'm
happy to have a go. It doesn't look like there's much code involved.
Reply all
Reply to author
Forward
0 new messages