Skip to content

Commit

Permalink
stop doing the same calculation over and over
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Nov 4, 2013
1 parent 9104702 commit 238ee10
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions activerecord/lib/active_record/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ def enum(definitions)
definitions.each do |name, values|
# DIRECTION = { }
const = const_set name.to_s.upcase, {}
name = name.to_sym

# def direction=(value) self[:direction] = DIRECTION[value] end
define_method "#{name}=" do |value|
self[:"#{name}"] = const[value]
self[name] = const[value]
end

# def direction() DIRECTION.key self[:direction] end
define_method name do
const.key self[:"#{name}"]
const.key self[name]
end

pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
Expand All @@ -59,12 +60,12 @@ def enum(definitions)

# def incoming?() direction == 0 end
define_method "#{value}?" do
self[:"#{name}"] == i
self[name] == i
end

# def incoming! update! direction: :incoming end
define_method "#{value}!" do
update! :"#{name}" => :"#{value}"
update! name => value.to_sym
end
end
end
Expand Down

0 comments on commit 238ee10

Please sign in to comment.