precedences 1.5.1 → 1.6.1

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: f38cb3d4a0d686152dba85df8e36c3bfb7f9c78e9a1e2167fe526cc8826a3b22
4
+ data.tar.gz: 302e835fa556e5a71ccd2896db769372eda815e1d73779a0feb4cb6055bf3bf1
5
5
  SHA512:
6
- metadata.gz: ea0d0643888097323ebe4cfe4c2c44c5593f5256bf292e3a6da7c0a79e0a56c8737ef33c7747be29f49178b0168a7db668f99f1b9dd6052d1c7a0d9c98c1f465
7
- data.tar.gz: 9f8759e3f0583e4c8184268de8884bac91864e738457e3029d205e26d9fdedb1bad35ca1d8029ef766d7a403d84a9411045a35528724ebedce4194230451e445
6
+ metadata.gz: 18b50411d57cbc635d49ef76d8e2c1f5383411817b7abe084427a5dbfc300d23b12cbd5745bd911dbd0aecebe41b4bc414dbc7419291551d357bd7e9ea895476
7
+ data.tar.gz: 5a908f668ff5b34d262ca5a24ec437bf3dcc8049d67e06d900924417ce1d85b64867d7640e7813888e8752550ee3e572279e227295b1b508ad7942bdb7ac0274
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ # 1.6.1
2
+
3
+ - Correction du bug quand classement par index et suppression
4
+ d’un item (trou)
5
+ - Ajout de la possibilité de définir une autre clé pour le
6
+ classement (`per_other_key`).
7
+
1
8
  # 1.5.1
2
9
 
3
10
  - Ajout de l’option `:cycle` pour `Q.select`
data/Manual/Manuel-fr.md CHANGED
@@ -86,6 +86,25 @@ 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`
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
+ ~~~
89
108
 
90
109
 
91
110
  ## Options possibles
@@ -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
  #
@@ -178,16 +194,17 @@ class Precedence
178
194
  #
179
195
  # Main method whose sort items
180
196
  #
181
- # @api private
197
+ # @private
182
198
  def sort_items(choices)
183
199
  return choices unless File.exist?(filepath)
184
200
  prec_ids = get_precedences_ids
185
201
  if precedences_per_index?
186
202
  choices_copy = choices.dup
187
- choices = prec_ids.map do |id|
188
- item = choices_copy[id.to_i - 1]
203
+ choices = []
204
+ prec_ids.each do |id|
205
+ item = choices_copy[id.to_i - 1] || next
189
206
  choices_copy[id.to_i - 1] = nil
190
- item
207
+ choices << item
191
208
  end
192
209
  # On ajoute les choix restants
193
210
  choices += choices_copy.compact
@@ -195,8 +212,9 @@ class Precedence
195
212
  #
196
213
  # Cas normal
197
214
  #
215
+ key_prec = per_other_key? ? per_other_key : :value
198
216
  choices.sort!{|a, b|
199
- (prec_ids.index(a[:value].to_s)||10000) <=> (prec_ids.index(b[:value].to_s)||10000)
217
+ (prec_ids.index(a[key_prec].to_s)||10000) <=> (prec_ids.index(b[key_prec].to_s)||10000)
200
218
  }
201
219
  end
202
220
  return choices
@@ -219,7 +237,7 @@ class Precedence
219
237
 
220
238
  # Save the values order in filepath file
221
239
  #
222
- # @api private
240
+ # @private
223
241
  def set_precedences_ids(value)
224
242
  if precedences_per_index?
225
243
  #
@@ -245,7 +263,7 @@ class Precedence
245
263
 
246
264
  # Get the values sorted if filepath exists.
247
265
  #
248
- # @api private
266
+ # @private
249
267
  def get_precedences_ids
250
268
  @get_precedences_ids ||= begin
251
269
  File.exist?(filepath) ? File.read(filepath).split("\n") : []
@@ -257,7 +275,7 @@ class Precedence
257
275
  # Raise an argument error otherwise.
258
276
  # If it's a folder, set to .precedences file
259
277
  #
260
- # @api private
278
+ # @private
261
279
  def filepath_validize_or_raises
262
280
  File.exist?(File.dirname(filepath)) || raise(ArgumentError.new("Precedences incorrect file: its folder should exist."))
263
281
  if File.exist?(filepath) && File.directory?(filepath)
@@ -270,7 +288,7 @@ class Precedence
270
288
  ##
271
289
  # Check if given choices are valid. Raise an ArgumentError otherwise
272
290
  #
273
- # @api private
291
+ # @private
274
292
  def choices_valid_or_raises(choices)
275
293
  #
276
294
  # On en aura besoin
@@ -1,3 +1,3 @@
1
1
  module Precedences
2
- VERSION = "1.5.1"
2
+ VERSION = "1.6.1"
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.6.1
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-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clir