lib/precedences/precedence.rb in precedences-1.5.1 vs lib/precedences/precedence.rb in precedences-1.6.1

- old
+ new

@@ -19,10 +19,11 @@ @show_help = nil @help = '' @per_page = nil @default = 1 @precedences_per_index = false + @per_other_key = nil @add_choice_cancel = nil end def sort(choices_ini, &block) @@ -87,10 +88,14 @@ def precedences_per_index? @precedences_per_index === true end + def per_other_key? + not(@per_other_key.nil?) + end + def add_choice_cancel? not(@add_choice_cancel.nil?) end # --- Tty prompt Methods --- @@ -133,10 +138,21 @@ end return @precedences_per_index end def precedences_per_index=(value) ; precedences_per_index(value) end + def per_other_key(value = :__no_value) + if value == :__no_value + return @per_other_key + elsif not(value) + @per_other_key = nil + else + @per_other_key = value + end + end + def per_other_key=(value) ; per_other_key(value) end + ## # To add the cancel choice # def add_choice_cancel(where = :down, **params) if where.is_a?(Hash) @@ -176,29 +192,31 @@ return choices end # # Main method whose sort items # - # @api private + # @private def sort_items(choices) return choices unless File.exist?(filepath) prec_ids = get_precedences_ids if precedences_per_index? choices_copy = choices.dup - choices = prec_ids.map do |id| - item = choices_copy[id.to_i - 1] + choices = [] + prec_ids.each do |id| + item = choices_copy[id.to_i - 1] || next choices_copy[id.to_i - 1] = nil - item + choices << item end # On ajoute les choix restants choices += choices_copy.compact else # # Cas normal # + key_prec = per_other_key? ? per_other_key : :value choices.sort!{|a, b| - (prec_ids.index(a[:value].to_s)||10000) <=> (prec_ids.index(b[:value].to_s)||10000) + (prec_ids.index(a[key_prec].to_s)||10000) <=> (prec_ids.index(b[key_prec].to_s)||10000) } end return choices end @@ -217,11 +235,11 @@ } end # Save the values order in filepath file # - # @api private + # @private def set_precedences_ids(value) if precedences_per_index? # # Mémorisation des choix par index # (:value de n'importe quel type, mais la liste original @@ -243,11 +261,11 @@ File.write(filepath, pids.join("\n")) end # Get the values sorted if filepath exists. # - # @api private + # @private def get_precedences_ids @get_precedences_ids ||= begin File.exist?(filepath) ? File.read(filepath).split("\n") : [] end end @@ -255,11 +273,11 @@ ## # Check if given filepath (for saving order of choices) is valid. # Raise an argument error otherwise. # If it's a folder, set to .precedences file # - # @api private + # @private def filepath_validize_or_raises File.exist?(File.dirname(filepath)) || raise(ArgumentError.new("Precedences incorrect file: its folder should exist.")) if File.exist?(filepath) && File.directory?(filepath) @filepath = File.join(filepath, '.precedences') elsif File.extname(filepath).empty? && !File.basename(filepath).start_with?('.') @@ -268,10 +286,10 @@ end ## # Check if given choices are valid. Raise an ArgumentError otherwise # - # @api private + # @private def choices_valid_or_raises(choices) # # On en aura besoin # @choices_ini = choices.dup.freeze