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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c79754f5d68c12d7955e1a77e870612ffb1fdb62cc65681e50efc137da125ba
4
- data.tar.gz: e0604ad18f10935d8111491b18481e3b273b7d6d064c0284c41c02836cadff3c
3
+ metadata.gz: 8e3ba1613a53b9f8ba7fcf13ff5b109f8c5e6ed02916fda74124dc9fdbac5055
4
+ data.tar.gz: 1da9d43ebd9a40564e7e7c46624d85f15216fe9906cf1ac85272cdc6454f1218
5
5
  SHA512:
6
- metadata.gz: '07383e22c02937744c155c72e0c35333c03169f7f3807dfd54765f490a225035ecd24017664fe7b1274c134422e164b12d4fc1a18f57b227f649b93bb166c3b2'
7
- data.tar.gz: 8bd02a86d9a478ec1bf1cbdf76f28fa6d673b00c61f2edddf6a9c3d90782a33986087ff8a3cf789ab439ab58b60227f3f7c36c4ab5ab097ee6b6430bff119119
6
+ metadata.gz: d88b6864c8b46a21f02823989e90156f7a3aa631d45f5cebee5dd9aa448b6f5370a1614263fe144b8b497bbdae667a692421fb6583496fd04ed8fa75622b9a85
7
+ data.tar.gz: '09e15b242df5668932babded2d1a675831071474656b8368ddd6456638be509169fafb7136700d6a168fce4e690a6b9b12d730f665f98ce34af3bf9fa02aa934'
data/README.rdoc CHANGED
@@ -15,7 +15,7 @@ From gem version 1.0 onwards this plugin will only have Rails 4+ support. If you
15
15
 
16
16
  Put this in your Gemfile
17
17
 
18
- gem 'active_enum', '~> 1.0.0.rc4'
18
+ gem 'active_enum', '~> 1.0.0'
19
19
 
20
20
  Then generate the config initializer file
21
21
 
@@ -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
- !get(value).nil?
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 || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
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.delete(:id) || next_id
102
- name = hash.delete(:name)
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
@@ -92,9 +92,9 @@ module ActiveEnum
92
92
  when :id
93
93
  value if enum[value]
94
94
  when :name
95
- enum[value]
95
+ enum[value].dup
96
96
  when Symbol
97
- (enum.meta(value) || {})[arg]
97
+ (enum.meta(value) || {})[arg].try(:dup)
98
98
  end
99
99
  end
100
100
  DEF
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.0.0.rc7'
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc7
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: 2020-07-13 00:00:00.000000000 Z
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: 1.3.1
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