Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  property :id, Serial
  property :name, String
end

Could I get the properties of User in an array or a hash?

like image 269
Ethan Turkeltaub Avatar asked Feb 09 '26 16:02

Ethan Turkeltaub


2 Answers

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.

like image 110
solnic Avatar answered Feb 12 '26 14:02

solnic


>> 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
like image 34
stef Avatar answered Feb 12 '26 15:02

stef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!