From 2a3ee892437b3dd99954c87429ba3e49c6edcf8b Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Wed, 3 Jul 2024 21:05:37 +0900 Subject: [PATCH 1/9] feat: Add column_names method from field_names --- lib/active_hash/base.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index 4a36b03..b166ead 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -50,6 +50,10 @@ def field_names @field_names ||= [] end + def column_names + @column_names ||= field_names.map(&:to_s) + end + def the_meta_class class << self self From 46c1fca6febc889e5aa3e3db5513350f2ac60609 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Wed, 3 Jul 2024 21:07:08 +0900 Subject: [PATCH 2/9] test: Add column_names test --- spec/active_yaml/aliases_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/active_yaml/aliases_spec.rb b/spec/active_yaml/aliases_spec.rb index d9c360f..ad4c7eb 100644 --- a/spec/active_yaml/aliases_spec.rb +++ b/spec/active_yaml/aliases_spec.rb @@ -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 @@ -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 From 0540d0301c9129ceae7fa56af1e8d2696ee44f4b Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Thu, 4 Jul 2024 23:48:28 +0900 Subject: [PATCH 3/9] refactor: Optimize field name conversion by using Symbol#name Using Symbol#name instead of Symbol#to_s avoids creating a new string for each invocation, improving performance and memory efficiency. --- lib/active_hash/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index b166ead..44fa111 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -51,7 +51,7 @@ def field_names end def column_names - @column_names ||= field_names.map(&:to_s) + @column_names ||= field_names.map(&:name) end def the_meta_class From 45adef80b122fd152b962213d6f9fb4ca234fec3 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Fri, 5 Jul 2024 00:02:25 +0900 Subject: [PATCH 4/9] docs: Add comment for rdoc --- lib/active_hash/base.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index 44fa111..f68aa80 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -50,6 +50,19 @@ def field_names @field_names ||= [] end + # + # Useful for CSV integration needing column names as strings. + # + # @return [Array] 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 @column_names ||= field_names.map(&:name) end From ac1e2207e0065138c560baa0b2c7991d9633e3b3 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Sun, 7 Jul 2024 08:06:00 +0900 Subject: [PATCH 5/9] refactor: no need cache --- lib/active_hash/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index f68aa80..29c01cf 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -64,7 +64,7 @@ def field_names # # => ["id", "name", "code"] # def column_names - @column_names ||= field_names.map(&:name) + field_names.map(&:name) end def the_meta_class From 2f6b35d4a0c5f21ec5365bb36668cb12be5b7551 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Sun, 7 Jul 2024 09:36:45 +0900 Subject: [PATCH 6/9] fix: Made it work with Ruby versions prior to 3.0 Ruby versions prior to 3.0 do not have the name method in the Symbol class. --- lib/active_hash/base.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index 29c01cf..83f56da 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -64,7 +64,9 @@ def field_names # # => ["id", "name", "code"] # def column_names - field_names.map(&:name) + field_names.map do |field| + field.respond_to?(:name) ? field.name : field.to_s.freeze + end end def the_meta_class From 030685854a7876130595b630c283fbba7265660e Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Mon, 8 Jul 2024 12:35:33 +0900 Subject: [PATCH 7/9] refactor: change to map(&:name) --- lib/active_hash/base.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index 043d140..464fa44 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -64,9 +64,7 @@ def field_names # # => ["id", "name", "code"] # def column_names - field_names.map do |field| - field.respond_to?(:name) ? field.name : field.to_s.freeze - end + field_names.map(&:name) end def the_meta_class From 034e9d27390ce3e3fbf2945c896a1796fa7568f3 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Mon, 8 Jul 2024 12:36:43 +0900 Subject: [PATCH 8/9] test: Add column_names spec --- spec/active_hash/base_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 0af1a45..c50aaa6 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -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 + end + describe ".data=" do before do class Region < ActiveHash::Base From 9679a8998128c2e11c994e76dde132135759f153 Mon Sep 17 00:00:00 2001 From: hatsu38 Date: Fri, 19 Jul 2024 23:34:28 +0900 Subject: [PATCH 9/9] test: Add skip test if RUBY_VERSION < "3.0.0" --- spec/active_hash/base_spec.rb | 1 + spec/active_yaml/aliases_spec.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index c50aaa6..c87bab0 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -122,6 +122,7 @@ class Country < ActiveHash::Base end it "returns an array of column names" do + skip "Not supported in Ruby 3.0.0" if RUBY_VERSION < "3.0.0" expect(Country.column_names).to eq(["name", "iso_name", "size"]) end end diff --git a/spec/active_yaml/aliases_spec.rb b/spec/active_yaml/aliases_spec.rb index ad4c7eb..9adf9fa 100644 --- a/spec/active_yaml/aliases_spec.rb +++ b/spec/active_yaml/aliases_spec.rb @@ -45,6 +45,7 @@ class KeyProduct < ActiveYaml::Base end it 'excludes them from column_names' do + skip "Not supported in Ruby 3.0.0" if RUBY_VERSION < "3.0.0" model.all expect(model.column_names).to match_array ["name", "flavor", "price"] end @@ -77,6 +78,7 @@ class KeyProduct < ActiveYaml::Base end it 'excludes them from column_names' do + skip "Not supported in Ruby 3.0.0" if RUBY_VERSION < "3.0.0" model.all expect(model.column_names).to match_array ["name", "flavor", "price", "slogan", "key"] end