fernferret
fernferret

Reputation: 856

Is 'valid' a reserved name in DataMapper?

I have the following model in datamapper:

class Student
  include DataMapper::Resource
  property :id,          Serial
  # <snip>
  property :permissions, String, :accessor => :protected, :required => true, :default => 'standard'
  property :valid,       Boolean, :default => false, :required => true
  # <snip>
end

After requiring 'dm-validations' (version 1.1.0), and starting my Sinatra app, I recieve the following message:

/Library/Ruby/Gems/1.8/gems/dm-validations-1.1.0/lib/dm-validations.rb:81:in `valid?': wrong number of arguments (1 for 0) (ArgumentError)
    from /Library/Ruby/Gems/1.8/gems/dm-validations-1.1.0/lib/dm-validations.rb:81:in `save_self'
    from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource.rb:1007:in `_save'
    from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource.rb:1223:in `run_once'
    from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource.rb:1006:in `_save'
    from /Library/Ruby/Gems/1.8/gems/dm-core-1.1.0/lib/dm-core/resource.rb:406:in `save'
    from /Library/Ruby/Gems/1.8/gems/dm-validations-1.1.0/lib/dm-validations.rb:69:in `save'
    from /Library/Ruby/Gems/1.8/gems/dm-validations-1.1.0/lib/dm-validations/support/context.rb:30:in `validation_context'
    from /Library/Ruby/Gems/1.8/gems/dm-validations-1.1.0/lib/dm-validations.rb:69:in `save'
<snip>

Is the 'valid' name I'm using for my model a reserved word? If it is, where can I find these words. I'm to the point of going on to trying to name it something like: 'student_valid' but now i'm just really curious about this.

Thanks

Upvotes: 2

Views: 326

Answers (2)

Michael Papile
Michael Papile

Reputation: 6856

Well the way datamapper works, is that it uses method_missing at the end of the method call chain and finds your property. If there is a method with this same name then that is called rather than your property. Datamapper mixes in Validatable which has the method valid? Most of the time you learn what is reserved (Like all Object methods etc.) But if you want a full list you can do:

  `myinstance.methods`

Anything that appears there will get called first.

Upvotes: 2

solnic
solnic

Reputation: 5743

#valid? is a method that dm-validations adds. You cannot use "valid" as a property name because it automatically defines "valid?" method for a boolean property type which overrides dm-validations' valid?. Hence the error.

That's a tricky situation, I guess we need to improve the way we validate property names. Thanks for reporting this.

Upvotes: 5

Related Questions