diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-04 06:57:37 -0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-04 06:57:37 -0800 |
commit | 6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0 (patch) | |
tree | e1f13966752e4644bd2209fd0aed136c5fe9801d | |
parent | b5cd187ae4c0fe69116e6e05ffef34d6b50dca29 (diff) | |
parent | 8264701355ef2fffa194732f6318a9ca180216d6 (diff) | |
download | rails-6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0.tar.gz rails-6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0.tar.bz2 rails-6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0.zip |
Merge pull request #12754 from ck3g/replace-class-evals-with-define-method-for-enum
Replace `class_eval` with `define_method` for enum
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index d6d1a8c9a2..3baaea7268 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -42,10 +42,14 @@ module ActiveRecord 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 @@ module ActiveRecord 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 |