Skip to content
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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ Gp/UnsafeYamlMarshal:
Enabled: true
Exclude:
- spec/**/*.rb

Gp/OptArgParameters:
Enabled: false
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ GEM
zeitwerk (~> 2.6)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.8.2)
json (2.9.0)
language_server-protocol (3.17.0.3)
logger (1.6.1)
method_source (1.1.0)
Expand All @@ -89,7 +89,7 @@ GEM
rack (3.1.8)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
regexp_parser (2.9.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
Expand All @@ -103,17 +103,17 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.69.0)
rubocop (1.69.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.36.1, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.36.1)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
Expand Down
2 changes: 1 addition & 1 deletion lib/enum_machine/build_enum_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.call(enum_values:, i18n_scope:, value_class:, machine: nil)
end

if i18n_scope
def self.values_for_form(specific_values = nil) # rubocop:disable Gp/OptArgParameters
def self.values_for_form(specific_values = nil)
(specific_values || values).map { |v| [human_name_for(v), v] }
end

Expand Down
20 changes: 19 additions & 1 deletion lib/enum_machine/build_value_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def self.call(enum_values:, i18n_scope:, value_decorator:, machine: nil)
Class.new(String) do
include(value_decorator) if value_decorator

define_method(:machine) { machine } if machine
define_method(:machine) { machine }
define_method(:enum_values) { enum_values }
private :enum_values, :machine

def inspect
"#<EnumMachine \"#{self}\">"
Expand Down Expand Up @@ -73,6 +75,22 @@ def human_name
end
RUBY
end

def respond_to_missing?(method_name, _include_private = false)
method_name = method_name.name if method_name.is_a?(Symbol)

method_name.end_with?("?") &&
method_name.include?("__") &&
(method_name.delete_suffix("?").split("__") - enum_values).empty?
end

def method_missing(method_name)
return super unless respond_to_missing?(method_name)

m_enums = method_name.name.delete_suffix("?").split("__")
self.class.define_method(method_name) { m_enums.include?(self) }
send(method_name)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/enum_machine/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module EnumMachine
class Machine
attr_reader :enum_values, :base_klass, :enum_const_name, :attr_name

def initialize(enum_values, base_klass = nil, enum_const_name = nil, attr_name = nil) # rubocop:disable Gp/OptArgParameters
def initialize(enum_values, base_klass = nil, enum_const_name = nil, attr_name = nil)
@enum_values = enum_values
@base_klass = base_klass
@enum_const_name = enum_const_name
Expand Down
17 changes: 11 additions & 6 deletions spec/enum_machine/driver_simple_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(state)
@state = state
end

include EnumMachine[state: { enum: %w[choice in_delivery] }]
include EnumMachine[state: { enum: %w[choice in_delivery lost] }]
end

module ValueDecorator
Expand All @@ -23,14 +23,17 @@ def initialize(state)
@state = state
end

include EnumMachine[state: { enum: %w[choice in_delivery], value_decorator: ValueDecorator }]
include EnumMachine[state: { enum: %w[choice in_delivery lost], value_decorator: ValueDecorator }]
end

RSpec.describe "DriverSimpleClass" do
subject(:item) { TestClass.new("choice") }

it { expect(item.state).to be_choice }
it { expect(item.state).not_to be_in_delivery }
it { expect(item.state.choice?).to be true }
it { expect(item.state.in_delivery?).to be false }
it { expect(item.state.choice__in_delivery?).to be true }
it { expect(item.state.lost__in_delivery?).to be false }
it { expect { item.state.last__in_delivery? }.to raise_error(NoMethodError) }
it { expect(item.state).to eq "choice" }
it { expect(item.state.frozen?).to be true }

Expand Down Expand Up @@ -68,13 +71,15 @@ def initialize(state)

describe "TestClass::STATE const" do
it "#values" do
expect(TestClass::STATE.values).to eq(%w[choice in_delivery])
expect(TestClass::STATE.values).to eq(%w[choice in_delivery lost])
end

it "#[]" do
expect(TestClass::STATE["in_delivery"]).to eq "in_delivery"
expect(TestClass::STATE["in_delivery"].in_delivery?).to be(true)
expect(TestClass::STATE["in_delivery"].choice?).to be(false)
expect(TestClass::STATE["in_delivery"].in_delivery__choice?).to be(true)
expect(TestClass::STATE["in_delivery"].lost__choice?).to be(false)
expect(TestClass::STATE["wrong"]).to be_nil
end

Expand Down Expand Up @@ -118,7 +123,7 @@ def initialize(state)
end

it "decorates enum values in enum const" do
expect(TestClassWithDecorator::STATE.values.map(&:am_i_choice?)).to eq([true, false])
expect(TestClassWithDecorator::STATE.values.map(&:am_i_choice?)).to eq([true, false, false])
expect((TestClassWithDecorator::STATE.values & ["in_delivery"]).map(&:am_i_choice?)).to eq([false])
end

Expand Down
Loading