-
Notifications
You must be signed in to change notification settings - Fork 179
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
Add column names method #311
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hatsu38 Thank you for submitting this!
If the goal here is to make sure active hash integrates with the CSV gem, can you please add a new spec that verifies this is working as expected? I'd suggest creating a new directory, spec/integration
, and a new spec file.
I would also be interested in a short "how to generate CSV" in the README.md file.
I would also like to see a short explicit test in |
Rails does not have I'd feel more comfortable with just doing the work for column_names. def column_names
field_names.map(&:name)
end Also looks like |
Using Symbol#name instead of Symbol#to_s avoids creating a new string for each invocation, improving performance and memory efficiency.
Ruby versions prior to 3.0 do not have the name method in the Symbol class.
lib/active_hash/base.rb
Outdated
field_names.map do |field| | ||
field.respond_to?(:name) ? field.name : field.to_s.freeze | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now field names are all symbols, so I think we can simplify this:
field_names.map do |field| | |
field.respond_to?(:name) ? field.name : field.to_s.freeze | |
end | |
fields.map(&:name) |
Does that work for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After merging the latest master, the code worked perfectly! 0306858
This pull request adds the column_names method to the ActiveHash gem.
The column_names method is analogous to the one found in Rails' ActiveRecord, allowing users to retrieve the names of columns defined in an ActiveHash model.
Added column_names method to ActiveHash::Base class.
The method returns an array of string column names, similar to ActiveRecord's column_names method.
close #310