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 +4 -4
- data/lib/active_enum/base.rb +16 -2
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/base_spec.rb +45 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e3ba1613a53b9f8ba7fcf13ff5b109f8c5e6ed02916fda74124dc9fdbac5055
|
4
|
+
data.tar.gz: 1da9d43ebd9a40564e7e7c46624d85f15216fe9906cf1ac85272cdc6454f1218
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d88b6864c8b46a21f02823989e90156f7a3aa631d45f5cebee5dd9aa448b6f5370a1614263fe144b8b497bbdae667a692421fb6583496fd04ed8fa75622b9a85
|
7
|
+
data.tar.gz: '09e15b242df5668932babded2d1a675831071474656b8368ddd6456638be509169fafb7136700d6a168fce4e690a6b9b12d730f665f98ce34af3bf9fa02aa934'
|
data/lib/active_enum/base.rb
CHANGED
@@ -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)
|
data/lib/active_enum/version.rb
CHANGED
@@ -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)
|