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

fix: ensure the stored field names are always symbols #312

Merged
merged 1 commit into from
Jul 7, 2024
Merged
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
7 changes: 5 additions & 2 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def fields(*args)
end

def field(field_name, options = {})
field_name = field_name.to_sym
validate_field(field_name)

field_names << field_name

add_default_value(field_name, options[:default]) if options.key?(:default)
Expand All @@ -210,7 +212,8 @@ def field(field_name, options = {})
end

def validate_field(field_name)
if [:attributes].include?(field_name.to_sym)
field_name = field_name.to_sym
if [:attributes].include?(field_name)
raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash. Please use another name.")
end
end
Expand Down Expand Up @@ -264,7 +267,7 @@ def add_default_value field_name, default_value
end

def define_getter_method(field, default_value)
unless instance_methods.include?(field.to_sym)
unless instance_methods.include?(field)
define_method(field) do
attributes[field].nil? ? default_value : attributes[field]
end
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 @@ -106,6 +106,16 @@ class Country < ActiveHash::Base
end
end

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

it "returns an array of field names" do
expect(Country.field_names).to eq([:name, :iso_name, :size])
end
end

describe ".data=" do
before do
class Region < ActiveHash::Base
Expand Down
Loading