Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method attribute_names #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/virtus/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def attributes
attribute_set
end

# Returns array of all attribute names defined on a Class
#
# @example
# class User
# include Virtus
#
# attribute :name, String
# attribute :age, Integer
# end
#
# User.attribute_names # => [:name, :age]
#
# @return [Array]
# @api public
def attribute_names
attribute_set.map(&:name)
end

private

# Setup descendants' own Attribute-accessor-method-hosting modules
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/virtus/class_methods/finalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ class Address
it 'automatically resolves constant when it is already available' do
expect(Examples::Article.attribute_set[:person].type.primitive).to be(Examples::Person)
end

describe 'attribute_names' do

it 'returns array of attribute names' do
expect(Examples::Person.attribute_names).to eq([:name, :articles, :address])
end

end

end