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

Add column names method #311

Merged
merged 10 commits into from
Aug 21, 2024
17 changes: 17 additions & 0 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def field_names
@field_names ||= []
end

#
# Useful for CSV integration needing column names as strings.
#
# @return [Array<String>] An array of column names as strings.
#
# @example Usage
# class Country < ActiveHash::Base
# fields :name, :code
# end
#
# Country.column_names
# # => ["id", "name", "code"]
#
def column_names
flavorjones marked this conversation as resolved.
Show resolved Hide resolved
field_names.map(&:name)
end

def the_meta_class
class << self
self
Expand Down
10 changes: 10 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ class Country < ActiveHash::Base
end
end

describe ".column_names" do
before do
Country.fields :name, :iso_name, "size"
end

it "returns an array of column names" do
expect(Country.column_names).to eq(["name", "iso_name", "size"])
end
Copy link
Collaborator

@kbrock kbrock Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flavorjones do we want to just conditionally do this?

Suggested change
it "returns an array of column names" do
expect(Country.column_names).to eq(["name", "iso_name", "size"])
end
it "returns an array of column names" do
skip unless RUBY_VERSION >= "3.0"
expect(Country.column_names).to eq(["name", "iso_name", "size"])
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commited! 9679a89

end

describe ".data=" do
before do
class Region < ActiveHash::Base
Expand Down
10 changes: 10 additions & 0 deletions spec/active_yaml/aliases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class KeyProduct < ActiveYaml::Base
model.all
expect(model.field_names).to match_array [:name, :flavor, :price]
end

it 'excludes them from column_names' do
model.all
expect(model.column_names).to match_array ["name", "flavor", "price"]
end
end
end

Expand Down Expand Up @@ -70,6 +75,11 @@ class KeyProduct < ActiveYaml::Base
model.all
expect(model.field_names).to match_array [:name, :flavor, :price, :slogan, :key]
end

it 'excludes them from column_names' do
model.all
expect(model.column_names).to match_array ["name", "flavor", "price", "slogan", "key"]
end
end
end

Expand Down
Loading