inum 4.0.1 → 4.0.2

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: 3491f1414101351d964f664aa8257409286cc49b
4
- data.tar.gz: 1b2be9033dc22ffda1c14ef95107004e96fedbe4
3
+ metadata.gz: dac28fd8851f430ad59c8b5e3ed56eae764d6bc0
4
+ data.tar.gz: 7d268a99e422a2462b97eb9d7cd40bebbd53de9a
5
5
  SHA512:
6
- metadata.gz: 6926d5d38ed53b126069ab24b56af810d453acb02b03ead4603ea15dca5258b9bf16997ca47340db02ecc43d526ace91e598907ac42cee17e62a7a6bb9bacaf8
7
- data.tar.gz: 57f444f10bb6619a1ae8edb8e86da7ea422a93014856f0f5d258032ace0688044323f383c13315607b6dbede05f8b04229d5504803e9b6e3321c1f039e863c0e
6
+ metadata.gz: c89392877b24e55fcd2b570a2f30388edc691d94eb81ce0113f572953b80b0bff8e93911441c29ef5f9d17f0d575f25e237dcbfa2f02197f5de225882c5ac91c
7
+ data.tar.gz: 0a9e2560a3af73975fb7b462d108db424687c7608f98e8a3c0e44d15779b201cd42fa33e589a9211ece1bc340b1a410a70a42c79ab9d04779c126c7b4fb8ca46
data/README.md CHANGED
@@ -51,6 +51,21 @@ p AnimeTypes::HARUHI.eql?('HARUHI') # => true (This method can compare all par
51
51
 
52
52
  # each method.
53
53
  AnimeTypes.each {|enum| p enum}
54
+
55
+ # Get length
56
+ AnimeTypes.length # => 3
57
+
58
+ # Get labels and values
59
+ AnimeTypes.labels # => [:EVANGELION, :HARUHI, :NYARUKO]
60
+ AnimeTypes.values # => [0, 1, 2]
61
+
62
+ # Get collection array.
63
+ AnimeTypes.collection # => [["EVANGELION", 0], ["HARUHI", 1], ....]
64
+
65
+ # collection usually use with some rails view helpers.
66
+ # f.select :name, Enum.collection
67
+ # f.select :name, Enum.collection(except:[:EVANGELION])
68
+ # f.select :name, Enum.collection(only:[:EVANGELION])
54
69
  ```
55
70
 
56
71
  can use Enumerable and Comparable.
@@ -75,6 +90,7 @@ p fav.anime.t # => '這いよれ!ニャル子さん' t is aliased translate.
75
90
 
76
91
  # #setter can set parsable object.
77
92
  anime.type = 1
93
+ anime.type = '1'
78
94
  anime.type = 'NYARUKO'
79
95
 
80
96
  # check methods.
@@ -93,11 +109,13 @@ ja:
93
109
  nyaruko: '這いよれ!ニャル子さん'
94
110
  ```
95
111
 
96
- If change default, Override i18n_key class method.
112
+ If you want to change key of i18n, Override i18n_key class method.
97
113
 
98
114
  ``` ruby
99
- def self.i18n_key(label)
100
- "hogehoge.#{label}"
115
+ class AnimeTypes < Inum::Base
116
+ def self.i18n_key(underscore_class_path, underscore_label)
117
+ "inums.#{underscore_class_path}.#{label}"
118
+ end
101
119
  end
102
120
  ```
103
121
 
@@ -23,10 +23,6 @@ module Inum
23
23
  end
24
24
 
25
25
  define_method("#{column}=") do |value|
26
- if value.class == String and /^\d$/.match(value)
27
- value = value.to_i
28
- end
29
-
30
26
  enum_class.parse(value).tap do |enum|
31
27
  if enum
32
28
  write_attribute(column, enum.to_i)
@@ -24,7 +24,9 @@ module Inum
24
24
  @label = label
25
25
  @label_string = label.to_s
26
26
  @value = value
27
- @i18n_key = ActiveSupport::Inflector.underscore("#{self.class.name}::#{label}").gsub('/', '.')
27
+
28
+ @underscore_class_path = self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).gsub('/', '.') : ''
29
+ @underscore_label = ActiveSupport::Inflector.underscore(label).gsub('/', '.')
28
30
  end
29
31
 
30
32
  # Compare object.
@@ -82,7 +84,7 @@ module Inum
82
84
  #
83
85
  # @return [String] localized string of Enum.
84
86
  def translate
85
- I18n.t(@i18n_key)
87
+ I18n.t(self.class.i18n_key(@underscore_class_path, @underscore_label))
86
88
  end
87
89
  alias_method :t, :translate
88
90
 
@@ -121,6 +123,16 @@ module Inum
121
123
  @enums.each(&block)
122
124
  end
123
125
 
126
+ # Override the rule of i18n keys.
127
+ # @abstract if change the rule of i18n keys.
128
+ #
129
+ # @param underscore_class_path [String] underscore class name.
130
+ # @param underscore_label [String] underscore label.
131
+ # @return [String] i18n key.
132
+ def self.i18n_key(underscore_class_path, underscore_label)
133
+ "#{underscore_class_path}.#{underscore_label}"
134
+ end
135
+
124
136
  # get all labels of Enum.
125
137
  #
126
138
  # @return [Array<Symbol>] all labels of Enum.
@@ -142,8 +154,12 @@ module Inum
142
154
  def self.parse(object)
143
155
  case object
144
156
  when String
145
- upcase = object.upcase
146
- find {|e| e.to_s == upcase}
157
+ if /^\d+$/.match(object)
158
+ parse(object.to_i)
159
+ else
160
+ upcase = object.upcase
161
+ find {|e| e.to_s == upcase}
162
+ end
147
163
  when Symbol
148
164
  upcase = object.upcase
149
165
  find {|e| e.label == upcase}
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = "4.0.1"
2
+ VERSION = "4.0.2"
3
3
  end
@@ -77,7 +77,7 @@ describe Inum::Base do
77
77
  end
78
78
 
79
79
  describe 'Defined enum class' do
80
- before :all do
80
+ before :each do
81
81
  class Anime < Inum::Base
82
82
  define :NYARUKO, 0
83
83
  define :MUROMISAN, 1
@@ -86,7 +86,7 @@ describe Inum::Base do
86
86
  end
87
87
  end
88
88
 
89
- after :all do
89
+ after :each do
90
90
  Object.class_eval{ remove_const :Anime }
91
91
  end
92
92
 
@@ -138,9 +138,27 @@ describe Inum::Base do
138
138
 
139
139
  describe '#t' do
140
140
  subject do
141
- expect(I18n).to receive(:t).with('animes.kmb') { 'ok' }
141
+ expect(I18n).to receive(:t).with(key).once { 'ok' }
142
142
  expect(Anime::KMB.t).to eq('ok')
143
143
  end
144
+
145
+ let(:key) { 'anime.kmb' }
146
+
147
+ it 'behaviour is right.' do
148
+ subject
149
+ end
150
+
151
+ context 'override .i18n_key' do
152
+ before :each do
153
+ Anime.define_singleton_method(:i18n_key) {|path, label| "inum.#{path}.#{label}" }
154
+ end
155
+
156
+ let(:key) { 'inum.anime.kmb' }
157
+
158
+ it 'Use overridden key.' do
159
+ subject
160
+ end
161
+ end
144
162
  end
145
163
 
146
164
  describe '#to_i' do
@@ -157,9 +175,13 @@ describe Inum::Base do
157
175
 
158
176
  describe '#translate' do
159
177
  subject do
160
- expect(I18n).to receive(:t).with('animes.kmb') { 'ok' }
178
+ expect(I18n).to receive(:t).with('anime.kmb') { 'ok' }
161
179
  expect(Anime::KMB.translate).to eq('ok')
162
180
  end
181
+
182
+ it 'behaviour is right.' do
183
+ subject
184
+ end
163
185
  end
164
186
 
165
187
  describe '#value' do
@@ -264,6 +286,13 @@ describe Inum::Base do
264
286
  end
265
287
  end
266
288
 
289
+ context 'source is integer of string' do
290
+ let(:source) { '3' }
291
+ it 'success.' do
292
+ subject
293
+ end
294
+ end
295
+
267
296
  context 'source is enum' do
268
297
  let(:source) { Anime::KMB }
269
298
  it 'success.' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inum
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-10 00:00:00.000000000 Z
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler