inum 2.0.5 → 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: cb32378c9020ed24378f516a690610f0783bc7f8
4
- data.tar.gz: b5ad93ed97447e1b339fc1b03e7647999776d615
3
+ metadata.gz: 22aedb5b28a96d4fb2b87466cbffc375c4a70021
4
+ data.tar.gz: bdf437ba85301196c9ddef1e7845dc7edbe26aec
5
5
  SHA512:
6
- metadata.gz: bde688e1396bb28dc39d2506be12b6c5172cb4f65c072c00fc8a1097964b62056968791e2b19c649ccd2aa9f2f4815428b05b444bbb1afef221e4cdf8b1d04ff
7
- data.tar.gz: 56387c80afda5178087060b581f281b9bf4b43b10112ef1cedf0bb3070956007d0a946f3427d4e9a5cec49ab0f4c2df956765244857d382e58e619d6d20b6f2e
6
+ metadata.gz: 452079b77b61139657ecec9728a330630f1a0fea0cd36d7c6f4ff55bf2cac2c8273662d107670f312f0fb1c3ee0d05a5111ccb726d00b49780e24574ad9b926b
7
+ data.tar.gz: a16a2b6c01fdbb707d836efc10104cc952d65e694464e046673d9c1fe0deb1cbadf1746273c816704b18ccaf7b9a0d3034318955bfd912d57080e5b3066a8280
data/lib/inum/base.rb CHANGED
@@ -127,11 +127,11 @@ module Inum
127
127
  defined_enums.dup
128
128
  end
129
129
 
130
- # Parse Object to Enum.
130
+ # Parse Object to Enum.(unsafe:An exception may occur.)
131
131
  #
132
132
  # @param object [Object] string or symbol or integer or Inum::Base.
133
- # @return [Inum::Base] instance of Inum::Base.
134
- def self.parse(object)
133
+ # @return [Inum::Base] instance of Inum::Base. or raise Exception.
134
+ def self.parse!(object)
135
135
  case object
136
136
  when String
137
137
  self.const_get(object)
@@ -146,6 +146,26 @@ module Inum
146
146
  end
147
147
  end
148
148
 
149
+ # Parse Object to Enum.
150
+ #
151
+ # @param object [Object] string or symbol or integer or Inum::Base.
152
+ # @return [Inum::Base] instance of Inum::Base. or nil.
153
+ def self.parse(object)
154
+ case object
155
+ when String
156
+ return nil unless labels.include?(object.to_sym)
157
+ when Symbol
158
+ return nil unless labels.include?(object)
159
+ when Integer
160
+ return nil unless values.include?(object)
161
+ when self
162
+ # do nothing.
163
+ else
164
+ return nil
165
+ end
166
+ parse!(object)
167
+ end
168
+
149
169
  # get all values of Enum.
150
170
  #
151
171
  # @return [Array<Integer>] all values of Enum.
data/lib/inum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = "2.0.5"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -187,6 +187,14 @@ describe Inum::Base do
187
187
  expect( @enum::parse(@enum::REDBULL) ).to eq( @enum::REDBULL )
188
188
  end
189
189
 
190
+ it 'parse return nil from a unknown value.' do
191
+ expect( @enum::parse('hoge') ).to eq( nil )
192
+ end
193
+
194
+ it 'parse! raise exception from a unknown value.' do
195
+ expect{ @enum::parse!('hoge') }.to raise_error( NameError )
196
+ end
197
+
190
198
  it 'values return Array<Integer>.' do
191
199
  expect(@enum::values.length).to eq(3)
192
200
  expect(@enum::values.instance_of?(Array)).to be_true
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: 2.0.5
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-10 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler