Class: Clir::Precedence

Inherits:
Object
  • Object
show all
Defined in:
lib/precedences/precedence.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Precedence

Returns a new instance of Precedence.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/precedences/precedence.rb', line 14

def initialize(filepath)
  @filepath = filepath
  filepath_validize_or_raises
  # --- Default values ---
  @question   = "Choose:"
  @show_help  = nil
  @help       = ''
  @per_page   = nil
  @default    = 1
  @precedences_per_index  = false
  @per_other_key          = nil
  @add_choice_cancel      = nil
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



7
8
9
# File 'lib/precedences/precedence.rb', line 7

def current
  @current
end

Instance Attribute Details

#added_choices_afterObject (readonly)

Returns the value of attribute added_choices_after.



182
183
184
# File 'lib/precedences/precedence.rb', line 182

def added_choices_after
  @added_choices_after
end

#added_choices_beforeObject (readonly)

To add any choice not precedencized

Parameters:

  • name (String)

    The menu name

  • value (Any)

    Then value of the menu

  • params (Hash)

    :at_top If true, add the item at the top

    Else (default) add at the bottom
    


181
182
183
# File 'lib/precedences/precedence.rb', line 181

def added_choices_before
  @added_choices_before
end

#filepathObject (readonly)

INSTANCE ###################



12
13
14
# File 'lib/precedences/precedence.rb', line 12

def filepath
  @filepath
end

Instance Method Details

#add(name, value, **params) ⇒ Object Also known as: add_choice



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/precedences/precedence.rb', line 183

def add(name, value, **params)
  @added_choices_before ||= []
  @added_choices_after  ||= []
  name.is_a?(String) || raise(ArgumentError.new("First argument should be a String."))
  lechoix = {name: name, value: value}
  if params[:at_top]
    @added_choices_before << lechoix
  else
    @added_choices_after << lechoix
  end
end

#add_choice_cancel(where = :down, **params) ⇒ Object

To add the cancel choice



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/precedences/precedence.rb', line 157

def add_choice_cancel(where = :down, **params)
  if where.is_a?(Hash)
    params = where
    where   = nil
  else
    params ||= {}
  end
  params ||= {}
  default_params = {value: nil, name: "Cancel", position: :down}
  params = default_params.merge(params)
  params.merge!(position: where.to_s.downcase.to_sym) unless where.nil?
  params.merge!(name: params[:name].orange)
  @add_choice_cancel = params
end

#add_choice_cancel?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/precedences/precedence.rb', line 97

def add_choice_cancel?
  not(@add_choice_cancel.nil?)
end

#default(value = nil) ⇒ Object



121
122
123
124
# File 'lib/precedences/precedence.rb', line 121

def default(value = nil)
  @default = value unless value.nil?
  return @default
end

#default=(value) ⇒ Object



125
# File 'lib/precedences/precedence.rb', line 125

def default=(value) ; default(value) end

#help(value = nil) ⇒ Object



127
128
129
130
# File 'lib/precedences/precedence.rb', line 127

def help(value = nil)
  @help = value unless value.nil?
  return @help
end

#help=(value) ⇒ Object



131
# File 'lib/precedences/precedence.rb', line 131

def help=(value) ; help(value) end

#per_other_key(value = :__no_value) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/precedences/precedence.rb', line 143

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

#per_other_key=(value) ⇒ Object



152
# File 'lib/precedences/precedence.rb', line 152

def per_other_key=(value) ; per_other_key(value) end

#per_other_key?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/precedences/precedence.rb', line 93

def per_other_key?
  not(@per_other_key.nil?)
end

#per_page(value = nil) ⇒ Object



109
110
111
112
# File 'lib/precedences/precedence.rb', line 109

def per_page(value = nil)
  @per_page = value unless value.nil?
  return @per_page
end

#per_page=(value) ⇒ Object



113
# File 'lib/precedences/precedence.rb', line 113

def per_page=(value); per_page(value) end

#precedences_per_index(value = nil) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/precedences/precedence.rb', line 133

def precedences_per_index(value = nil)
  if value === false
    @precedences_per_index = false
  else
    @precedences_per_index = true
  end
  return @precedences_per_index
end

#precedences_per_index=(value) ⇒ Object



141
# File 'lib/precedences/precedence.rb', line 141

def precedences_per_index=(value) ; precedences_per_index(value) end

#precedences_per_index?Boolean

— Predicate Methods —

Returns:

  • (Boolean)


89
90
91
# File 'lib/precedences/precedence.rb', line 89

def precedences_per_index?
  @precedences_per_index === true
end

#question(quest = nil) ⇒ Object

— Tty prompt Methods —



103
104
105
106
# File 'lib/precedences/precedence.rb', line 103

def question(quest = nil)
  @question = quest unless quest.nil?
  return @question
end

#question=(quest) ⇒ Object



107
# File 'lib/precedences/precedence.rb', line 107

def question=(quest) ; question(quest) end

#show_help(value = nil) ⇒ Object



115
116
117
118
# File 'lib/precedences/precedence.rb', line 115

def show_help(value = nil)
  @show_help = value unless value === nil
  return @show_help
end

#show_help=(value) ⇒ Object



119
# File 'lib/precedences/precedence.rb', line 119

def show_help=(value) ; show_help(value) end

#sort(choices_ini, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/precedences/precedence.rb', line 28

def sort(choices_ini, &block)

  # 
  # On doit évaluer le bloc ici car certaines valeurs peuvent
  # modifier le comportement de la suite.
  # Par exemple, si @precedences_per_index a été mis à true,
  # on peut permettre n'importe quel type de :value dans les 
  # hash
  if block_given?
    block.call(self)
  end

  # 
  # List of choices must be valid
  # 
  choices_valid_or_raises(choices_ini)

  # 
  # Use a clone rather than original list to leave the initial
  # list of choices alone.
  # 
  choices = choices_ini.dup

  #
  # Sort the list of choices 
  # (and treate other choices — add cancel, etc.)
  # 
  choices = prepare_choices(choices)

  if block_given?
    # 
    # Tty-select options
    # 
    options = define_tty_options(choices)
    # 
    # On procède au choix
    # 
    begin
      choix = Q.select(question.jaune, choices, **options)
    rescue TTY::Reader::InputInterrupt
      # Annulation par ^C
      return nil
    end
    # 
    # On enregistre ce choix (sauf si null ou :cancel)
    # 
    set_precedence(choix) unless choix.nil? || choix == :cancel
    # 
    # On retourne le choix
    # 
    return choix
  else
    # 
    # Sinon, sans block, on renvoie la liste classée
    # 
    return choices
  end
end