Skip to content

Commit b478985

Browse files
authored
Improve value constants (#46)
* improve value constants with value methods * add specs & docs
1 parent 7481411 commit b478985

4 files changed

Lines changed: 7 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ end
7474

7575
Product::COLOR.values # => ["red", "green"]
7676
Product::COLOR::RED # => "red"
77+
Product::COLOR::RED.red? # => true
78+
Product::COLOR::RED.human_name # => "Красный"
7779
Product::COLOR::RED__GREEN # => ["red", "green"]
7880

7981
Product::COLOR["red"].red? # => true

lib/enum_machine/build_enum_class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def self.human_name_for(name)
3636
end
3737

3838
enum_values.each do |enum_value|
39-
const_set enum_value.underscore.upcase, enum_value.to_s.freeze
39+
const_set enum_value.underscore.upcase, value_class.new(enum_value).freeze
4040
end
4141

4242
aliases.each_key do |key|

spec/enum_machine/active_record_enum_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
m = model.new(color: "red")
4242
expect(m.color.human_name).to eq "Красный"
4343
expect(model::COLOR.human_name_for("red")).to eq "Красный"
44+
expect(model::COLOR::RED.human_name).to eq "Красный"
4445
end
4546

4647
context "when enum in CamelCase" do

spec/enum_machine/driver_simple_class_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def initialize(state)
3636
it { expect { item.state.last__in_delivery? }.to raise_error(NoMethodError) }
3737
it { expect(item.state).to eq "choice" }
3838
it { expect(item.state.frozen?).to be true }
39+
it { expect(TestClass::STATE::IN_DELIVERY.in_delivery?).to be true }
40+
it { expect(TestClass::STATE::IN_DELIVERY.choice?).to be false }
41+
it { expect(TestClass::STATE::IN_DELIVERY.frozen?).to be true }
3942

4043
describe "module" do
4144
it "returns state string" do

0 commit comments

Comments
 (0)