active_enum 1.0.0.rc7 → 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/README.rdoc +1 -1
- data/lib/active_enum/base.rb +23 -9
- data/lib/active_enum/extensions.rb +2 -2
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/base_spec.rb +45 -0
- metadata +5 -5
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/README.rdoc
CHANGED
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)
|
@@ -76,7 +90,7 @@ module ActiveEnum
|
|
76
90
|
alias_method :[], :get
|
77
91
|
|
78
92
|
def include?(value)
|
79
|
-
!
|
93
|
+
!get_value(value, false).nil?
|
80
94
|
end
|
81
95
|
|
82
96
|
# Access any meta data defined for a given id or name. Returns a hash.
|
@@ -88,22 +102,22 @@ module ActiveEnum
|
|
88
102
|
private
|
89
103
|
|
90
104
|
# Access value row array for a given id or name value.
|
91
|
-
def get_value(index)
|
105
|
+
def get_value(index, raise_on_not_found = ActiveEnum.raise_on_not_found)
|
92
106
|
if index.is_a?(Integer)
|
93
107
|
store.get_by_id(index)
|
94
108
|
else
|
95
109
|
store.get_by_name(index)
|
96
|
-
end || (
|
110
|
+
end || (raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
97
111
|
end
|
98
112
|
|
99
113
|
def id_and_name_and_meta(hash)
|
100
114
|
if hash.has_key?(:name)
|
101
|
-
id = hash.
|
102
|
-
name = hash.
|
103
|
-
meta = hash
|
115
|
+
id = hash.fetch(:id) { next_id }
|
116
|
+
name = hash.fetch(:name).freeze
|
117
|
+
meta = hash.except(:id, :name).freeze
|
104
118
|
return id, name, (meta.empty? ? nil : meta)
|
105
119
|
elsif hash.keys.first.is_a?(Integer)
|
106
|
-
return *Array(hash).first
|
120
|
+
return *Array(hash).first.tap { |arr| arr[1].freeze }
|
107
121
|
else
|
108
122
|
raise ActiveEnum::InvalidValue, "The value supplied, #{hash}, is not a valid format."
|
109
123
|
end
|
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)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -83,11 +83,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: '0'
|
89
89
|
requirements: []
|
90
|
-
rubygems_version: 3.0.3
|
90
|
+
rubygems_version: 3.0.3.1
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|