simple_enum 2.0.1 → 2.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
  SHA1:
3
- metadata.gz: 15a4d23ebb4c06389cc6567924cd06fdf9120534
4
- data.tar.gz: 7ae02ee44339aa33f7b2f2e4b80818e5c07b0afe
3
+ metadata.gz: 764053ae9a23795f23ac3d6c9a108d5c1c17f307
4
+ data.tar.gz: 7e3bf97b4177c12cf5582b1302dced1b90a90b39
5
5
  SHA512:
6
- metadata.gz: c3a157e56f6876c9e62e567fd39b13c30cd1b5a52242eea88eaf4b696119b41f061392792371a3bde550478f4d7cc55eef4041b4128656bdf040cb5d3fcba4b9
7
- data.tar.gz: 9285798a1feee8f685154a4135d9644fe94ef7ef3911ff863f9305e80fffeaee7217b2a46a614c07f364deed8391cd6ac0c5b94c34e649f9e66118b5d9d58e3c
6
+ metadata.gz: 5e40c68baf656cb1064153f47eb3cc826375d578988bd6260d5016bdce25e42accf6c3de77cce14adb0b44433f78a9389a4f3b581d54d74335871e635ab08b8b
7
+ data.tar.gz: be89ccd3fa051379cc0ea45be3ceec545a14d1a0af4e5a6de5e2afc8bcc182922106e0719c0ebee5a894fdfce19f861abbc0b7bb428bfaa7330de5d89a78bf8a
data/README.md CHANGED
@@ -96,9 +96,10 @@ joe.gender_cd # => 0
96
96
  Accessing actual enum values is possible at the class level:
97
97
 
98
98
  ```ruby
99
- User.genders # => #<SimpleEnum::Enum:0x0....>
100
- User.genders[:male] # => 0
101
- User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
99
+ User.genders # => #<SimpleEnum::Enum:0x0....>
100
+ User.genders[:male] # => 0
101
+ User.genders.values_at(:male, :female) # => [0, 1]
102
+ User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
102
103
  ```
103
104
 
104
105
  ### Wait, there's more!
@@ -153,6 +154,7 @@ User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd
153
154
  user.gender_was
154
155
  # => :male
155
156
  ```
157
+ - Require translated enum values? See [SimpleEnum::ViewHelpers](https://github.com/lwe/simple_enum/blob/master/lib/simple_enum/view_helpers.rb)
156
158
  - Need to provide custom options for the mongoid field, or skip the automatically generated field?
157
159
 
158
160
  ```ruby
@@ -219,7 +221,7 @@ Do not use values named after existing, or well known method names, like `new`,
219
221
  as_enum :handle, [:new, :create, :update]
220
222
 
221
223
  # GOOD, prefixes all methods
222
- as_enum :handle, [:new, :create, :update], :prefix => true
224
+ as_enum :handle, [:new, :create, :update], prefix: true
223
225
  ```
224
226
 
225
227
  Searching for certain values by using the finder methods:
@@ -231,16 +233,17 @@ User.females # => returns an ActiveRecord::Relation
231
233
  Contributors
232
234
  ------------
233
235
 
234
- * @dmitry - bugfixes and other improvements
235
- * @tarsolya - implemented all the ruby 1.9 and rails 3 goodness!
236
- * @dbalatero - rails 2.3.5 bugfix & validator fixes
237
- * @johnthethird - feature for <tt>_for_select</tt> to return the values
238
- * @sinsiliux - ruby 1.9 fixes and removed AR dependency
239
- * @sled - mongoid support
240
- * @abrom - <tt>find_by_...</tt> method
241
- * @mhuggins - translations fixes
242
- * @patbenatar - for helping move towards 2.0 (scopes et all)
243
- * and all others: https://github.com/lwe/simple_enum/graphs/contributors thanks
236
+ - @dmitry - bugfixes and other improvements
237
+ - @tarsolya - implemented all the ruby 1.9 and rails 3 goodness!
238
+ - @dbalatero - rails 2.3.5 bugfix & validator fixes
239
+ - @johnthethird - feature for `_for_select` to return the values
240
+ - @sinsiliux - ruby 1.9 fixes and removed AR dependency
241
+ - @sled - mongoid support
242
+ - @abrom - `find_by_...` method
243
+ - @mhuggins - translations fixes
244
+ - @patbenatar - for helping move towards 2.0 (scopes et all)
245
+ - @abacha - translation helpers
246
+ - and all others: https://github.com/lwe/simple_enum/graphs/contributors thanks
244
247
 
245
248
  License & Copyright
246
249
  -------------------
@@ -38,6 +38,11 @@ module SimpleEnum
38
38
  hash.keys
39
39
  end
40
40
 
41
+ def values_at(*keys)
42
+ keys = keys.map(&:to_s)
43
+ hash.values_at(*keys)
44
+ end
45
+
41
46
  def to_s
42
47
  name
43
48
  end
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
 
3
3
  # The current `SimpleEnum` version.
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.0"
5
5
  end
@@ -29,6 +29,22 @@ module SimpleEnum
29
29
  }
30
30
  end
31
31
 
32
+ # Helper method to return the translated value of an enum.
33
+ #
34
+ # translate_enum(user, :gender) # => "Frau"
35
+ #
36
+ # Has been aliased to `te` as a convenience method as well.
37
+ #
38
+ # record - The model instance holding the enum
39
+ # key - The Symbol with the name of the enum, i.e. same key as used in the
40
+ # `as_enum` call
41
+ #
42
+ # Returns String with translation of enum
43
+ def translate_enum(record, key)
44
+ record.class.human_enum_name(key, record.public_send(key))
45
+ end
46
+ alias_method :te, :translate_enum
47
+
32
48
  private
33
49
 
34
50
  def translate_enum_key(enum, key)
@@ -99,4 +99,14 @@ describe SimpleEnum::Enum do
99
99
  expect(subject.include?(2)).to be_false
100
100
  end
101
101
  end
102
+
103
+ context '#values_at' do
104
+ it 'fetches multiple values by string' do
105
+ expect(subject.values_at("male", "female")).to eq [0, 1]
106
+ end
107
+
108
+ it 'fetches multiple values by symbol' do
109
+ expect(subject.values_at(:male)).to eq [0]
110
+ end
111
+ end
102
112
  end
@@ -59,4 +59,13 @@ describe SimpleEnum::ViewHelpers, i18n: true do
59
59
  end
60
60
  end
61
61
  end
62
+
63
+ context '#translate_enum' do
64
+ let(:fake_object) { klass.new }
65
+ it "translates with object scope" do
66
+ fake_object.gender = :male
67
+ expect(klass).to receive(:human_enum_name).with(:gender, :male) { "Mr." }
68
+ expect(helper.translate_enum(fake_object, :gender)).to eq "Mr."
69
+ end
70
+ end
62
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-14 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport