Get DataMapper Model Properties

Is there a way to get model properties using DataMapper? For instance:

require 'rubygems'
require 'datamapper'

class User
  include DataMapper::Resource

  property :id, Serial
  property :name, String
end

Can I get properties Userin an array or hash?

+3
source share
2 answers

Yes, you can get them using

User.properties

it will return an instance of the PropertySet, which you can use in an array if you want.

+10
source
>> 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
+3
source

Source: https://habr.com/ru/post/1785068/


All Articles