active_enum 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4dd61a23068e97e9582ac6bb9109414172733ae0c26759177ecab8082819cd6
4
- data.tar.gz: bab417444c404468c0fea123e31a05ef392bf0a14b32ccefa84fa9d2f63129d1
3
+ metadata.gz: 8e3ba1613a53b9f8ba7fcf13ff5b109f8c5e6ed02916fda74124dc9fdbac5055
4
+ data.tar.gz: 1da9d43ebd9a40564e7e7c46624d85f15216fe9906cf1ac85272cdc6454f1218
5
5
  SHA512:
6
- metadata.gz: c5383983a54ff4169f5cbefe0a4907c9007144808a322e0eb31fd4e0ba52e86e2a814d7f8c6dedac24ce9127a56db556878f337ce437175cfe43f111e65bc81d
7
- data.tar.gz: 9ba6f7231906b870a9f9832ab477a658a99e5df227fcde6fcff47e47ba86533bf7f182147befd6295b00de285a6248f70d53fa803cb48617cbc71eb68e767f23
6
+ metadata.gz: d88b6864c8b46a21f02823989e90156f7a3aa631d45f5cebee5dd9aa448b6f5370a1614263fe144b8b497bbdae667a692421fb6583496fd04ed8fa75622b9a85
7
+ data.tar.gz: '09e15b242df5668932babded2d1a675831071474656b8368ddd6456638be509169fafb7136700d6a168fce4e690a6b9b12d730f665f98ce34af3bf9fa02aa934'
@@ -4,6 +4,7 @@ module ActiveEnum
4
4
  class NotFound < StandardError; end
5
5
 
6
6
  class Base
7
+ DEFAULT_GROUPED_SELECT_TRANSFORM = proc { |group| group&.html_safe }
7
8
 
8
9
  class << self
9
10
  attr_accessor :store
@@ -60,12 +61,25 @@ module ActiveEnum
60
61
  end
61
62
 
62
63
  # Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
63
- def to_grouped_select(group_by)
64
+ def to_grouped_select(group_by, group_transform: DEFAULT_GROUPED_SELECT_TRANSFORM)
64
65
  store.values.group_by { |(_id, _name, meta)| (meta || {})[group_by] }.map { |group, collection|
65
- [ group, collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
66
+ [ group_transform.call(group), collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
66
67
  }
67
68
  end
68
69
 
70
+ # Return a simple hash of key value pairs id => name for each value
71
+ def to_h
72
+ store.values.inject({}) { |hash, row|
73
+ hash.merge(row[0] => row[1])
74
+ }
75
+ end
76
+
77
+ # Return count of values defined
78
+ def length
79
+ store.values.length
80
+ end
81
+ alias_method :size, :length
82
+
69
83
  # Access id or name value. Pass an id number to retrieve the name or
70
84
  # a symbol or string to retrieve the matching id.
71
85
  def get(index)
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -272,6 +272,51 @@ describe ActiveEnum::Base do
272
272
  [ nil, [ ['Name 2',2] ] ]
273
273
  ])
274
274
  end
275
+
276
+ it 'shoud transform group name with custom group transform proc' do
277
+ enum = define_enum do
278
+ value :id => 1, :name => 'Name 1', :category => 'Foo'
279
+ value :id => 2, :name => 'Name 2'
280
+ end
281
+
282
+ expect(enum.to_grouped_select(:category, group_transform: proc { |group| group.to_s.upcase })).to eq([
283
+ [ 'FOO', [ ['Name 1',1] ] ],
284
+ [ '', [ ['Name 2',2] ] ]
285
+ ])
286
+ end
287
+ end
288
+
289
+ describe ".to_h" do
290
+ it 'should return hash of ids as keys and names as values' do
291
+ enum = define_enum do
292
+ value :id => 1, :name => 'Name 1'
293
+ value :id => 2, :name => 'Name 2'
294
+ end
295
+ expect(enum.to_h).to eq({ 1 => 'Name 1', 2 => 'Name 2' })
296
+ end
297
+ end
298
+
299
+ describe ".length" do
300
+ it 'should return number of values defined' do
301
+ enum = define_enum do
302
+ value :id => 1, :name => 'Name 1'
303
+ value :id => 2, :name => 'Name 2'
304
+ end
305
+ expect(enum.length).to eq 2
306
+ end
307
+
308
+ it 'should return 0 when no values defined' do
309
+ enum = define_enum {}
310
+ expect(enum.length).to eq 0
311
+ end
312
+
313
+ it 'should be aliased as size' do
314
+ enum = define_enum do
315
+ value :id => 1, :name => 'Name 1'
316
+ value :id => 2, :name => 'Name 2'
317
+ end
318
+ expect(enum.size).to eq enum.length
319
+ end
275
320
  end
276
321
 
277
322
  def define_enum(&block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan