From a33c3183cbd9ad42b20cf403a85ee849318fdad0 Mon Sep 17 00:00:00 2001 From: Rob Date: Wed, 16 Dec 2020 20:09:28 -0700 Subject: [PATCH 1/4] Implements == on Avram Enums --- spec/type_extensions/enum_spec.cr | 4 ++++ src/avram/charms/enum_extensions.cr | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/spec/type_extensions/enum_spec.cr b/spec/type_extensions/enum_spec.cr index b0e46703a..c50b16976 100644 --- a/spec/type_extensions/enum_spec.cr +++ b/spec/type_extensions/enum_spec.cr @@ -32,4 +32,8 @@ describe "Enum" do issue.status.to_s.should eq("Opened") issue.status.to_i.should eq(0) end + + it "provides a working ==" do + Issue::Status.new(:closed).should eq(Issue::Status.new(:closed)) + end end diff --git a/src/avram/charms/enum_extensions.cr b/src/avram/charms/enum_extensions.cr index 396d3681c..f19d586c2 100644 --- a/src/avram/charms/enum_extensions.cr +++ b/src/avram/charms/enum_extensions.cr @@ -24,6 +24,10 @@ macro avram_enum(enum_name, &block) @enum = Avram{{ enum_name }}.from_value(enum_value.to_i) end + def ==(other : {{ enum_name }}) : Bool + self.enum == other.enum + end + delegate to_s, to_i, to: @enum forward_missing_to @enum From 97319fba5e022d6cfbf210f193cab96a5f0d9f9f Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 17 Dec 2020 12:06:13 -0700 Subject: [PATCH 2/4] implements case equality operator on avram_enum Also provides named constants which reflect the underlying enum values --- spec/type_extensions/enum_spec.cr | 24 ++++++++++++++++++++++++ src/avram/charms/enum_extensions.cr | 17 +++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/spec/type_extensions/enum_spec.cr b/spec/type_extensions/enum_spec.cr index c50b16976..06c1903ea 100644 --- a/spec/type_extensions/enum_spec.cr +++ b/spec/type_extensions/enum_spec.cr @@ -36,4 +36,28 @@ describe "Enum" do it "provides a working ==" do Issue::Status.new(:closed).should eq(Issue::Status.new(:closed)) end + + it "provides enum-like getters" do + Issue::Status::Closed.should eq(Issue::Status.new(:closed).enum) + end + + it "implements case equality" do + symbol_lookup = case Issue::Status.new(:closed).value + when Issue::Status.new(:closed) + true + else + false + end + + symbol_lookup.should be_true + + constant_lookup = case Issue::Status.new(:opened).value + when Issue::Status::Opened + true + else + false + end + + constant_lookup.should be_true + end end diff --git a/src/avram/charms/enum_extensions.cr b/src/avram/charms/enum_extensions.cr index f19d586c2..9132ce077 100644 --- a/src/avram/charms/enum_extensions.cr +++ b/src/avram/charms/enum_extensions.cr @@ -1,6 +1,17 @@ +macro redeclare_avram_enum(name) + {% for member in name.resolve.constants %} + {{ member }} = {{ name }}::{{ member }} + {% end %} +end + macro avram_enum(enum_name, &block) - enum Avram{{ enum_name }} + {% avram_enum = ("Avram" + enum_name.names.join("::")).id %} + enum {{ avram_enum }} {{ block.body }} + + def ===(other : Int32) + value == other + end end class {{ enum_name }} @@ -8,6 +19,8 @@ macro avram_enum(enum_name, &block) Lucky end + redeclare_avram_enum({{ avram_enum }}) + getter :enum # You may need to prefix with {{ @type }} @@ -28,7 +41,7 @@ macro avram_enum(enum_name, &block) self.enum == other.enum end - delegate to_s, to_i, to: @enum + delegate :===, to_s, to_i, to: @enum forward_missing_to @enum From 972859d38bd77439507e742988f7fde360a52f8b Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 21 Dec 2020 10:15:22 -0700 Subject: [PATCH 3/4] convert enum charm to struct --- src/avram/charms/enum_extensions.cr | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/avram/charms/enum_extensions.cr b/src/avram/charms/enum_extensions.cr index 9132ce077..ff7310d73 100644 --- a/src/avram/charms/enum_extensions.cr +++ b/src/avram/charms/enum_extensions.cr @@ -14,7 +14,7 @@ macro avram_enum(enum_name, &block) end end - class {{ enum_name }} + struct {{ enum_name }} def self.adapter Lucky end @@ -37,10 +37,6 @@ macro avram_enum(enum_name, &block) @enum = Avram{{ enum_name }}.from_value(enum_value.to_i) end - def ==(other : {{ enum_name }}) : Bool - self.enum == other.enum - end - delegate :===, to_s, to_i, to: @enum forward_missing_to @enum From 4dfabef56fddfadd338a549ee5085145e62cddca Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 21 Dec 2020 10:26:34 -0700 Subject: [PATCH 4/4] add sad-path specs for avram enum --- spec/type_extensions/enum_spec.cr | 48 +++++++++++++++++++------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/spec/type_extensions/enum_spec.cr b/spec/type_extensions/enum_spec.cr index 06c1903ea..2c394fb37 100644 --- a/spec/type_extensions/enum_spec.cr +++ b/spec/type_extensions/enum_spec.cr @@ -35,29 +35,41 @@ describe "Enum" do it "provides a working ==" do Issue::Status.new(:closed).should eq(Issue::Status.new(:closed)) + Issue::Status.new(:opened).should_not eq(Issue::Status.new(:closed)) end - it "provides enum-like getters" do + it "provides enum-like constant values" do Issue::Status::Closed.should eq(Issue::Status.new(:closed).enum) + Issue::Status.new(:closed).enum.should eq(Issue::Status::Closed) end it "implements case equality" do - symbol_lookup = case Issue::Status.new(:closed).value - when Issue::Status.new(:closed) - true - else - false - end - - symbol_lookup.should be_true - - constant_lookup = case Issue::Status.new(:opened).value - when Issue::Status::Opened - true - else - false - end - - constant_lookup.should be_true + case Issue::Status.new(:closed).value + when Issue::Status.new(:closed) + true + else + false + end.should be_true + + case Issue::Status::Closed.value + when Issue::Status.new(:closed) + true + else + false + end.should be_true + + case Issue::Status.new(:opened).value + when Issue::Status::Opened + true + else + false + end.should be_true + + case Issue::Status::Opened.value + when Issue::Status::Opened + true + else + false + end.should be_true end end