Permalink
Browse files

Merge pull request #12754 from ck3g/replace-class-evals-with-define-m…

…ethod-for-enum

Replace `class_eval` with `define_method` for enum
  • Loading branch information...
2 parents b5cd187 + 8264701 commit 6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0 @dhh dhh committed Nov 4, 2013
Showing with 12 additions and 4 deletions.
  1. +12 −4 activerecord/lib/active_record/enum.rb
@@ -42,10 +42,14 @@ def enum(definitions)
const_set const_name, {}
# def direction=(value) self[:direction] = DIRECTION[value] end
- class_eval "def #{name}=(value) self[:#{name}] = #{const_name}[value] end"
+ define_method "#{name}=" do |value|
+ self[:"#{name}"] = const_name[value]
+ end
# def direction() DIRECTION.key self[:direction] end
- class_eval "def #{name}() #{const_name}.key self[:#{name}] end"
+ define_method name do
+ const_name.key self[:"#{name}"]
+ end
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
pairs.each do |value, i|
@@ -56,10 +60,14 @@ def enum(definitions)
scope value, -> { where name => i }
# def incoming?() direction == 0 end
- class_eval "def #{value}?() self[:#{name}] == #{i} end"
+ define_method "#{value}?" do
+ self[:"#{name}"] == i
+ end
# def incoming! update! direction: :incoming end
- class_eval "def #{value}!() update! #{name}: :#{value} end"
+ define_method "#{value}!" do
+ update! :"#{name}" => :"#{value}"
+ end
end
end
end

0 comments on commit 6e4a810

Please sign in to comment.