Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0 2 parents b5cd187 + 8264701
David Heinemeier Hansson dhh authored
Showing with 12 additions and 4 deletions.
  1. +12 −4 activerecord/lib/active_record/enum.rb
16 activerecord/lib/active_record/enum.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.