diff --git a/lib/enum/enum.rb b/lib/enum/enum.rb index 6f07ebc..e022305 100644 --- a/lib/enum/enum.rb +++ b/lib/enum/enum.rb @@ -19,7 +19,7 @@ def enum(columns) columns.each do |column, values| values = values.zip(values.map(&:to_s)).to_h if values.is_a?(Array) values.each do |method, value| - method_definitions << "def #{method}?; #{column} == '#{value}'; end" + method_definitions << "def #{method}?; #{column} == #{value.inspect}; end" end end class_eval(method_definitions.uniq.join(";")) diff --git a/spec/enum/enum_spec.rb b/spec/enum/enum_spec.rb index 39b1866..8f089d8 100644 --- a/spec/enum/enum_spec.rb +++ b/spec/enum/enum_spec.rb @@ -109,20 +109,20 @@ class Neighborhood < ActiveHash::Base expect(Article::ARTICLE_3.archived?).to be_truthy end - it "defines a predicate method for each value in the enum" do + it "multi type data (ex: string, integer and symbol) enum" do NotifyType = Class.new(ActiveHash::Base) do include ActiveHash::Enum self.data = [ { name: 'Like', action: 'LIKE'}, - { name: 'Comment', action: 'COMMENT'}, - { name: 'Follow', action: 'FOLLOW'}, + { name: 'Comment', action: 1}, + { name: 'Follow', action: :FOLLOW}, { name: 'Mention', action: 'MENTION'} ] enum_accessor :name - enum action: { like: 'LIKE', comment: 'COMMENT', follow: 'FOLLOW', mention: 'MENTION' } + enum action: { like: 'LIKE', comment: 1, follow: :FOLLOW, mention: 'MENTION' } end expect(NotifyType::LIKE.like?).to be_truthy