precedences 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69c48ce2d4dd7a75e39e091581e48c02475ad26e4715a70ef99f705fd1d95ab8
4
- data.tar.gz: 621913cb7d2b49741586fbdb0cccac0c0487b830c63b67d59330d8b3f3c68cd3
3
+ metadata.gz: 1a735e7b8f40a271f4259c8a4c3057d6a2836f2db06ab4ca89e094a8ced4b6f8
4
+ data.tar.gz: 7ebd0aafee4fa270219b8f46c83ca2e3fc0ad3870997277ba91ac4dcdd45f1aa
5
5
  SHA512:
6
- metadata.gz: ea0d0643888097323ebe4cfe4c2c44c5593f5256bf292e3a6da7c0a79e0a56c8737ef33c7747be29f49178b0168a7db668f99f1b9dd6052d1c7a0d9c98c1f465
7
- data.tar.gz: 9f8759e3f0583e4c8184268de8884bac91864e738457e3029d205e26d9fdedb1bad35ca1d8029ef766d7a403d84a9411045a35528724ebedce4194230451e445
6
+ metadata.gz: 8518f9fd7c03c3204c4930cd6faa99feb28d48e34d7d44d8fb1681fdc4d18407ef9649a4873784e1554d97d33b333f0fb2943c13d1eb98e049e8442c6ebc5f04
7
+ data.tar.gz: d0c399a24bf4178581e8c0573dd515f3b8ef9d1fa31b0ed09fc9cce013abe61476e7e2d224d26fccd865f322938f115cb26052ea632c5d35763544fd2a033a43
data/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ # 1.7.0
2
+
3
+ - Possiblité d’ajouter des menus hors classement à l’aide
4
+ de la méthode block #add (ou son alias #add_choice)
5
+
6
+ # 1.6.1
7
+
8
+ - Correction du bug quand classement par index et suppression
9
+ d’un item (trou)
10
+ - Ajout de la possibilité de définir une autre clé pour le
11
+ classement (`per_other_key`).
12
+
1
13
  # 1.5.1
2
14
 
3
15
  - Ajout de l’option `:cycle` pour `Q.select`
data/Manual/Manuel-fr.md CHANGED
@@ -86,7 +86,27 @@ choix = precedencize(choices, file) do |q|
86
86
  end
87
87
  ~~~
88
88
 
89
+ On peut aussi définir une autre clé que `:value` pour le tri, avec la propriété `per_other_key`
89
90
 
91
+ ~~~ruby
92
+ require 'precedences'
93
+
94
+ #
95
+ # Des choix avec des valeurs spéciales
96
+ #
97
+ choices = [
98
+ {name:"La classe Integer" , pkey: :entier, value: Integer},
99
+ {name:"La classe Array" , pkey: :liste ,value: Array},
100
+ {name:"La classe Hash" , pkey: :table , value: Hash},
101
+ ]
102
+
103
+ choix = precedencize(choices, file) do |q|
104
+ q.question "Choisis une classe"
105
+ q.per_other_key :pkey # <=== autre clé
106
+ end
107
+ ~~~
108
+
109
+ <a name="options"></a>
90
110
 
91
111
  ## Options possibles
92
112
 
@@ -142,6 +162,14 @@ choix = precedencize(choices, precfile) do |q|
142
162
  q.default = "premier"
143
163
  # ou q.default "premier"
144
164
  # => Sélectionnera le choix "Choix premier"
165
+
166
+ # Ajout de menus à la fin, jamais classés
167
+ q.add "Dernier menu", :last
168
+ q.add_choice "Tout dernier", :very_last
169
+
170
+ # Ajout de menus au début, jamais classés
171
+ q.add_choice "Tout premier".bleu, :very_first, **{at_top:true}
172
+ q.add("Premier", :first, {at_top: true})
145
173
 
146
174
  end
147
175
 
@@ -168,3 +196,9 @@ end
168
196
  ~~~
169
197
 
170
198
  > 😃 Noter qu’on peut en fait se servir de ce menu pour ajouter n’importe quel autre menu que “Renoncer”.
199
+
200
+
201
+
202
+ #### Ajouter un menu quelconque
203
+
204
+ Utiliser les méthodes `#add` ou `#add_choice` (alias). Cf. ci-dessus [Options possibles](#options).
data/Manual/Manuel-fr.pdf CHANGED
Binary file
data/Rakefile CHANGED
@@ -4,7 +4,8 @@ require "rake/testtask"
4
4
  Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
6
  t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
7
+ # t.test_files = FileList["test/**/*_test.rb"]
8
+ t.test_files = FileList["test/**/precedences_test.rb"]
8
9
  end
9
10
 
10
11
  task :default => :test
@@ -21,6 +21,7 @@ class Precedence
21
21
  @per_page = nil
22
22
  @default = 1
23
23
  @precedences_per_index = false
24
+ @per_other_key = nil
24
25
  @add_choice_cancel = nil
25
26
  end
26
27
 
@@ -89,6 +90,10 @@ class Precedence
89
90
  @precedences_per_index === true
90
91
  end
91
92
 
93
+ def per_other_key?
94
+ not(@per_other_key.nil?)
95
+ end
96
+
92
97
  def add_choice_cancel?
93
98
  not(@add_choice_cancel.nil?)
94
99
  end
@@ -135,6 +140,17 @@ class Precedence
135
140
  end
136
141
  def precedences_per_index=(value) ; precedences_per_index(value) end
137
142
 
143
+ def per_other_key(value = :__no_value)
144
+ if value == :__no_value
145
+ return @per_other_key
146
+ elsif not(value)
147
+ @per_other_key = nil
148
+ else
149
+ @per_other_key = value
150
+ end
151
+ end
152
+ def per_other_key=(value) ; per_other_key(value) end
153
+
138
154
  ##
139
155
  # To add the cancel choice
140
156
  #
@@ -153,16 +169,42 @@ class Precedence
153
169
  @add_choice_cancel = params
154
170
  end
155
171
 
172
+ ##
173
+ # To add any choice not precedencized
174
+ #
175
+ # @param name [String] The menu name
176
+ # @param value [Any] Then value of the menu
177
+ # @param params [Hash]
178
+ # :at_top If true, add the item at the top
179
+ # Else (default) add at the bottom
180
+ #
181
+ attr_reader :added_choices_before
182
+ attr_reader :added_choices_after
183
+ def add(name, value, **params)
184
+ @added_choices_before = []
185
+ @added_choices_after = []
186
+ name.is_a?(String) || raise(ArgumentError.new("First argument should be a String."))
187
+ lechoix = {name: name, value: value}
188
+ if params[:at_top]
189
+ @added_choices_before << lechoix
190
+ else
191
+ @added_choices_after << lechoix
192
+ end
193
+ end
194
+ alias :add_choice :add
195
+
156
196
  private
157
197
 
158
198
  ##
159
199
  # = main =
200
+ # @private
160
201
  #
161
202
  def prepare_choices(choices)
162
203
  #
163
204
  # Classement des choix par précédence
164
205
  #
165
206
  choices = sort_items(choices)
207
+
166
208
  #
167
209
  # Faut-il ajouter un choix cancel ?
168
210
  #
@@ -170,6 +212,17 @@ class Precedence
170
212
  add_method = (@add_choice_cancel[:position] == :down) ? :push : :unshift
171
213
  choices.send(add_method, @add_choice_cancel)
172
214
  end
215
+
216
+ #
217
+ # Y a-t-il des menus à ajouter ?
218
+ #
219
+ if added_choices_before && not(added_choices_before.empty?)
220
+ choices = added_choices_before + choices
221
+ end
222
+ if added_choices_after && not(added_choices_after.empty?)
223
+ choices = choices + added_choices_after
224
+ end
225
+
173
226
  #
174
227
  # On retourne les choix préparés
175
228
  #
@@ -178,16 +231,17 @@ class Precedence
178
231
  #
179
232
  # Main method whose sort items
180
233
  #
181
- # @api private
234
+ # @private
182
235
  def sort_items(choices)
183
236
  return choices unless File.exist?(filepath)
184
237
  prec_ids = get_precedences_ids
185
238
  if precedences_per_index?
186
239
  choices_copy = choices.dup
187
- choices = prec_ids.map do |id|
188
- item = choices_copy[id.to_i - 1]
240
+ choices = []
241
+ prec_ids.each do |id|
242
+ item = choices_copy[id.to_i - 1] || next
189
243
  choices_copy[id.to_i - 1] = nil
190
- item
244
+ choices << item
191
245
  end
192
246
  # On ajoute les choix restants
193
247
  choices += choices_copy.compact
@@ -195,8 +249,9 @@ class Precedence
195
249
  #
196
250
  # Cas normal
197
251
  #
252
+ key_prec = per_other_key? ? per_other_key : :value
198
253
  choices.sort!{|a, b|
199
- (prec_ids.index(a[:value].to_s)||10000) <=> (prec_ids.index(b[:value].to_s)||10000)
254
+ (prec_ids.index(a[key_prec].to_s)||10000) <=> (prec_ids.index(b[key_prec].to_s)||10000)
200
255
  }
201
256
  end
202
257
  return choices
@@ -219,7 +274,7 @@ class Precedence
219
274
 
220
275
  # Save the values order in filepath file
221
276
  #
222
- # @api private
277
+ # @private
223
278
  def set_precedences_ids(value)
224
279
  if precedences_per_index?
225
280
  #
@@ -245,7 +300,7 @@ class Precedence
245
300
 
246
301
  # Get the values sorted if filepath exists.
247
302
  #
248
- # @api private
303
+ # @private
249
304
  def get_precedences_ids
250
305
  @get_precedences_ids ||= begin
251
306
  File.exist?(filepath) ? File.read(filepath).split("\n") : []
@@ -257,7 +312,7 @@ class Precedence
257
312
  # Raise an argument error otherwise.
258
313
  # If it's a folder, set to .precedences file
259
314
  #
260
- # @api private
315
+ # @private
261
316
  def filepath_validize_or_raises
262
317
  File.exist?(File.dirname(filepath)) || raise(ArgumentError.new("Precedences incorrect file: its folder should exist."))
263
318
  if File.exist?(filepath) && File.directory?(filepath)
@@ -270,7 +325,7 @@ class Precedence
270
325
  ##
271
326
  # Check if given choices are valid. Raise an ArgumentError otherwise
272
327
  #
273
- # @api private
328
+ # @private
274
329
  def choices_valid_or_raises(choices)
275
330
  #
276
331
  # On en aura besoin
@@ -1,3 +1,3 @@
1
1
  module Precedences
2
- VERSION = "1.5.1"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: precedences
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PhilippePerret
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-23 00:00:00.000000000 Z
11
+ date: 2024-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clir