Class: HandySort::Ranker
- Inherits:
-
Object
- Object
- HandySort::Ranker
- Defined in:
- lib/handy_sort/ranker.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#initialize(key) ⇒ Ranker
constructor
A new instance of Ranker.
- #move_affected(relation, old_val, new_val) ⇒ Object
Constructor Details
#initialize(key) ⇒ Ranker
Returns a new instance of Ranker.
5 6 7 |
# File 'lib/handy_sort/ranker.rb', line 5 def initialize(key) @key = key.to_sym end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/handy_sort/ranker.rb', line 3 def key @key end |
Instance Method Details
#move_affected(relation, old_val, new_val) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/handy_sort/ranker.rb', line 9 def move_affected(relation, old_val, new_val) affected = relation.where(key => Range.new(*[old_val, new_val].sort)) offset = old_val > new_val ? 1 : -1 affected.update_all "%s = %s + (%d)" % [key.to_s, key.to_s, offset] end |