Get DataMapper Model Properties
Is there a way to get the properties of a model with DataMapper? For example:
require 'rubygems'
require 'datamapper'
class User
include DataMapper::Resource
p开发者_运维技巧roperty :id, Serial
property :name, String
end
Could I get the properties of User
in an array or a hash?
Yes, you can get them with
User.properties
it will return an instance of PropertySet which you can cast into an Array if you want.
>> u = User.new
=> #<User @id=nil @name=nil>
>> u.id = 1
=> 1
>> u.name = "hello"
=> "hello"
>> u.attributes
=> {:name=>"hello", :id=>1}
>> u.attributes.class
=> Hash
精彩评论