solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Small question: Is it possible to retrieve the list of the attibutes with their types using the class name and not the instance that includes Virtus.model? #232

Closed bogdanRada closed 10 years ago

bogdanRada commented 10 years ago

First of all, i want to say this is a great gem and i simply love it . I have only one question:

Is it possible that having a class like this

class User
  include Virtus.model

  attribute :name, String
  attribute :age, Integer
end

I could somehow retrieve the list of the attributes with their types doing something like this : User.attributes ? I need to use the class and i can not instantiate it because i need it for a project i am working on to determine the attributes for some dynamic classes.

I know that if this would have been an ActiveRecord class i could do User.columns_hash and i would get that list.

I am just asking if this functionality exists here too or maybe is something you would consider in the future? Or maybe there is an alternative? I would really appreciate any suggestion. Thank you very much

solnic commented 10 years ago

You can do this:

User.attribute_set.map(&:name)
bogdanRada commented 10 years ago

Perfect. Thanks . That's exactly what i needed :)