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

Create class enum accessor #135

Merged
merged 3 commits into from
Feb 19, 2023
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
5 changes: 4 additions & 1 deletion docs/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ config.active? # => false
config.status_value # => 0

config.status_values # => { :active => 0, :archived => 1 }
config.statuses # => { :active => 0, :archived => 1 }
Configuration.status_values # => { :active => 0, :archived => 1 }
Configuration.statuses # => { :active => 0, :archived => 1 }
```

Under the hood, values are stored as integers, according to the index of the element in the array:
Expand Down Expand Up @@ -57,4 +60,4 @@ review.archived_status? # => false
review.comments_active? # => false
review.comments_inactive? # => true

```
```
9 changes: 8 additions & 1 deletion lib/store_model/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def enum(name, values = nil, **kwargs)
define_reader(name, mapping)
define_writer(name, mapping)
define_method("#{name}_value") { attributes[name.to_s] }
define_method("#{name}_values") { mapping }
define_map_readers(name, mapping)
define_predicate_methods(name, mapping, options)
end
end
Expand All @@ -44,6 +44,13 @@ def define_predicate_methods(name, mapping, options)
end
end

def define_map_readers(name, mapping)
define_method("#{name}_values") { mapping }
alias_method(ActiveSupport::Inflector.pluralize(name), "#{name}_values")
singleton_class.define_method("#{name}_values") { mapping }
singleton_class.alias_method(ActiveSupport::Inflector.pluralize(name), "#{name}_values")
Copy link

@BoberMod BoberMod Mar 3, 2023

Choose a reason for hiding this comment

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

It's breaking change.
What if enum is already in plural form?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BoberMod good point, I'll open a PR shortly to fix this.

end

def cast_type(mapping)
StoreModel::Types::EnumType.new(mapping)
end
Expand Down
29 changes: 29 additions & 0 deletions spec/store_model/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@
expect(subject.status_values).to eq(active: 1, archived: 0)
end

it "has .values class method" do
expect(config_class.status_values).to eq(active: 1, archived: 0)
end

it "aliases the pluralized name to the #values method" do
expect(subject.statuses).to eq(subject.status_values)
end

it "aliases the pluralized name to the .values method" do
expect(config_class.statuses).to eq(config_class.status_values)
end

context "when multiple StoreModel classes are defined" do
let!(:another_config_class) do
Class.new do
include StoreModel::Model

enum :status, off: 0, on: 1
enum :level, low: 1, medium: 2, high: 3
end
end

it "does not share enum mapping methods between classes" do
expect(another_config_class.status_values).to eq(off: 0, on: 1)
expect(config_class.status_values).to eq(active: 1, archived: 0)
expect(config_class.respond_to?(:level_values)).to eq(false)
end
end

context "when value is not in the list" do
let(:value) { "undefined" }

Expand Down