Class: Tidied::Sorting
- Inherits:
-
Object
- Object
- Tidied::Sorting
- Defined in:
- lib/tidied/sorting.rb
Instance Method Summary collapse
- #execute(collection) ⇒ Object
-
#initialize(*instructions) ⇒ Sorting
constructor
A new instance of Sorting.
Constructor Details
#initialize(*instructions) ⇒ Sorting
Returns a new instance of Sorting.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tidied/sorting.rb', line 7 def initialize(*instructions) @defaults = { direction: :ascending, nils: :small, accessor: :itself, case_sensitive: true, normalized: false, natural: false } @instructions = instructions.map do |instruction| @defaults.merge(instruction) end end |
Instance Method Details
#execute(collection) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tidied/sorting.rb', line 21 def execute(collection) collection.sort_by do |item| @instructions.map do |instruction| raw_value = access_value(from: item, at: instruction[:accessor]) processed_value = process_value(from: raw_value, given: instruction) numeric_value = numeric_value(from: processed_value) direction_multiplier = direction_multiplier(given: instruction[:direction]) nils_multiplier = nils_multiplier(given: instruction[:nils]) # p({ raw_value: raw_value, processed_value: processed_value, numeric_value: numeric_value, direction_multiplier: direction_multiplier, nils_multiplier: nils_multiplier }) if numeric_value.nil? # [1, 0] [-1, 0] [direction_multiplier * nils_multiplier, 0] else # [0, n] [0, -n] [0, numeric_value * direction_multiplier] end # [-1, 0] [0, -n] [0, n] [1, 0] end end end |