inum 1.3.0 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/inum/base.rb +24 -16
- data/lib/inum/define_check_method.rb +2 -2
- data/lib/inum/utils.rb +7 -7
- data/lib/inum/version.rb +1 -1
- data/spec/inum/base_spec.rb +30 -26
- data/spec/inum/define_check_method_spec.rb +1 -1
- data/spec/inum/utils_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3475adce821be0a84959f8a664c1a430ffa070a
|
4
|
+
data.tar.gz: 968e8893be8c4ad5253501e2579361a748e7f7db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c297c68abe3578abac17207713cb412c4a13d24b6e8779e77f0da20562e6284b7e0c8c0fc73a562d9b74db1e22b90edf2202496dba133daae1aaea8fdf05a44
|
7
|
+
data.tar.gz: 013f475f2cf4a22fc645370fad72ec2bd37dc791864a504458371bba44acc8cbe019a5862792286ee766ae984c8b914716ac0ed89c6b867b9ab163c490dde0d9
|
data/README.md
CHANGED
@@ -59,6 +59,7 @@ p AnimeType::NYARUKO - 1 # => HARUHI
|
|
59
59
|
AnimeType::each {|enum| p enum }
|
60
60
|
|
61
61
|
```
|
62
|
+
|
62
63
|
can use Enumerable and Comparable.
|
63
64
|
|
64
65
|
- more detail is [Class::Base](http://rubydoc.info/github/alfa-jpn/inum/Inum/Base)
|
@@ -97,6 +98,7 @@ p anime.haruhi? # => true
|
|
97
98
|
to_t methods localize enum by i18n.
|
98
99
|
default i18n key is name_space.class_name.enum_member_label.
|
99
100
|
If change default, Override i18n_key class method.
|
101
|
+
|
100
102
|
``` ruby
|
101
103
|
# default i18n_key.
|
102
104
|
def self.i18n_key(label)
|
data/lib/inum/base.rb
CHANGED
@@ -8,7 +8,7 @@ module Inum
|
|
8
8
|
# @example
|
9
9
|
# class FruitType < Inum::Base
|
10
10
|
# define_enum :APPLE, 0
|
11
|
-
# define_enum :BANANA, 1
|
11
|
+
# define_enum :BANANA, 1
|
12
12
|
# define_enum :ORANGE, 2
|
13
13
|
# end
|
14
14
|
class Base
|
@@ -26,7 +26,8 @@ module Inum
|
|
26
26
|
@label = label
|
27
27
|
@value = value
|
28
28
|
|
29
|
-
@localized_enum
|
29
|
+
@localized_enum = I18n.t(self.class.i18n_key(label))
|
30
|
+
@underscore_label = Inum::Utils::underscore(label)
|
30
31
|
end
|
31
32
|
|
32
33
|
# Compare object.
|
@@ -44,7 +45,7 @@ module Inum
|
|
44
45
|
def + (value)
|
45
46
|
self.class.parse(@value + value.to_i)
|
46
47
|
end
|
47
|
-
|
48
|
+
|
48
49
|
# minus object.
|
49
50
|
#
|
50
51
|
# @param value [Integer] plus value.(call to_i in this method.)
|
@@ -60,9 +61,9 @@ module Inum
|
|
60
61
|
def eql?(object)
|
61
62
|
self.equal?(self.class.parse(object))
|
62
63
|
end
|
63
|
-
|
64
|
+
|
64
65
|
# Enum to Integer.
|
65
|
-
#
|
66
|
+
#
|
66
67
|
# @return [Integer] integer value of Enum.
|
67
68
|
def to_i
|
68
69
|
@value
|
@@ -83,6 +84,13 @@ module Inum
|
|
83
84
|
@localized_enum
|
84
85
|
end
|
85
86
|
|
87
|
+
# Enum label to underscore string.
|
88
|
+
#
|
89
|
+
# @return [String] under_score string value(label) of Enum.
|
90
|
+
def to_u
|
91
|
+
@underscore_label
|
92
|
+
end
|
93
|
+
|
86
94
|
# Execute the yield(block) with each member of enum.
|
87
95
|
#
|
88
96
|
# @yield [enum] instance of enum.
|
@@ -96,28 +104,28 @@ module Inum
|
|
96
104
|
def self.labels
|
97
105
|
defined_enums.keys
|
98
106
|
end
|
99
|
-
|
107
|
+
|
100
108
|
# get Enum length.
|
101
109
|
#
|
102
110
|
# @return [Integer] count of Enums.
|
103
111
|
def self.length
|
104
112
|
defined_enums.length
|
105
113
|
end
|
106
|
-
|
114
|
+
|
107
115
|
# return array of Enums.
|
108
116
|
#
|
109
117
|
# @return [Array<Array<Symbol, Integer>>] sorted array of Enums.
|
110
118
|
def self.to_a
|
111
119
|
defined_enums.flatten(0).sort{|a,b| a[1] <=> b[1] }
|
112
120
|
end
|
113
|
-
|
121
|
+
|
114
122
|
# return hash of Enums.
|
115
123
|
#
|
116
124
|
# @return [Hash<Symbol, Integer>] hash of Enums.
|
117
125
|
def self.to_h
|
118
126
|
defined_enums.dup
|
119
127
|
end
|
120
|
-
|
128
|
+
|
121
129
|
# Parse Object to Enum.
|
122
130
|
#
|
123
131
|
# @param object [Object] string or symbol or integer or Inum::Base.
|
@@ -136,7 +144,7 @@ module Inum
|
|
136
144
|
raise ArgumentError, "#{object} is nani?"
|
137
145
|
end
|
138
146
|
end
|
139
|
-
|
147
|
+
|
140
148
|
# get all values of Enum.
|
141
149
|
#
|
142
150
|
# @return [Array<Integer>] all values of Enum.
|
@@ -151,13 +159,13 @@ module Inum
|
|
151
159
|
# @param value [Integer] value of Enum.(default:autoincrement for 0.)
|
152
160
|
def self.define_enum(label, value = defined_enums.size)
|
153
161
|
value = value.to_i
|
154
|
-
|
162
|
+
|
155
163
|
validate_enum_args!(label, value)
|
156
164
|
|
157
165
|
defined_enums[label] = value
|
158
166
|
self.const_set(label, new(label, value))
|
159
167
|
end
|
160
|
-
|
168
|
+
|
161
169
|
# get hash of :DEFINED_ENUMS.
|
162
170
|
def self.defined_enums
|
163
171
|
self.const_get(:DEFINED_ENUMS)
|
@@ -178,16 +186,16 @@ module Inum
|
|
178
186
|
def self.inherited(child)
|
179
187
|
child.const_set(:DEFINED_ENUMS, Hash.new)
|
180
188
|
end
|
181
|
-
|
189
|
+
|
182
190
|
# Validate enum args, and raise exception.
|
183
|
-
#
|
191
|
+
#
|
184
192
|
# @param label [Object] label of Enum.
|
185
193
|
# @param value [Integer] value of Enum.
|
186
194
|
def self.validate_enum_args!(label, value)
|
187
195
|
unless label.instance_of?(Symbol)
|
188
196
|
raise ArgumentError, "The label(#{label}!) isn't instance of Symbol."
|
189
197
|
end
|
190
|
-
|
198
|
+
|
191
199
|
if label == :DEFINED_ENUMS
|
192
200
|
raise ArgumentError, "The label(#{label}!) is keyword."
|
193
201
|
end
|
@@ -195,7 +203,7 @@ module Inum
|
|
195
203
|
if defined_enums.has_key?(label)
|
196
204
|
raise ArgumentError, "The label(#{label}!) already exists!!"
|
197
205
|
end
|
198
|
-
|
206
|
+
|
199
207
|
if defined_enums.has_value?(value)
|
200
208
|
raise ArgumentError, "The value(#{value}!) already exists!!"
|
201
209
|
end
|
@@ -5,13 +5,13 @@ module Inum
|
|
5
5
|
# class Fruit
|
6
6
|
# extend Inum::DefineCheckMethod
|
7
7
|
# define_check_method :type, FruitType
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# attr_accessor :type
|
10
10
|
# end
|
11
11
|
#
|
12
12
|
module DefineCheckMethod
|
13
13
|
require 'inum/utils'
|
14
|
-
|
14
|
+
|
15
15
|
# Define compare method in class.
|
16
16
|
#
|
17
17
|
# @param variable_name [String] name of variable.
|
data/lib/inum/utils.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
module Inum
|
2
2
|
# Utils of Inum.
|
3
3
|
class Utils
|
4
|
-
|
4
|
+
|
5
5
|
# convert a camel cased word to a underscore word.
|
6
6
|
#
|
7
|
-
# @param camel_cased_word [String] camel cased word.
|
8
|
-
# @return [String
|
7
|
+
# @param camel_cased_word [String, Symbol] camel cased word.
|
8
|
+
# @return [String] underscore word.
|
9
9
|
def self.underscore(camel_cased_word)
|
10
10
|
word = camel_cased_word.to_s.dup
|
11
|
-
|
11
|
+
|
12
12
|
word.gsub!('::', '.')
|
13
13
|
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
14
14
|
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
15
15
|
word.tr!('-', '_')
|
16
|
-
|
16
|
+
|
17
17
|
word.downcase!
|
18
|
-
|
18
|
+
|
19
19
|
word
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
end
|
23
23
|
end
|
data/lib/inum/version.rb
CHANGED
data/spec/inum/base_spec.rb
CHANGED
@@ -6,37 +6,37 @@ describe Inum::Base do
|
|
6
6
|
it 'define_enum validate correct' do
|
7
7
|
# correct.
|
8
8
|
expect{
|
9
|
-
Class.new(Inum::Base) { define_enum :REDBULL, 0 }
|
9
|
+
Class.new(Inum::Base) { define_enum :REDBULL, 0 }
|
10
10
|
Class.new(Inum::Base) { define_enum :REDBULL, 0.to_i }
|
11
|
-
Class.new(Inum::Base) {
|
12
|
-
define_enum :REDBULL, 0
|
13
|
-
define_enum :MONSTER, 1
|
11
|
+
Class.new(Inum::Base) {
|
12
|
+
define_enum :REDBULL, 0
|
13
|
+
define_enum :MONSTER, 1
|
14
14
|
}
|
15
15
|
}.not_to raise_error
|
16
|
-
|
16
|
+
|
17
17
|
# wrong name.
|
18
18
|
expect{
|
19
|
-
Class.new(Inum::Base) { define_enum 1111, 0 }
|
19
|
+
Class.new(Inum::Base) { define_enum 1111, 0 }
|
20
20
|
}.to raise_error
|
21
|
-
|
21
|
+
|
22
22
|
# wrong value.
|
23
23
|
expect{
|
24
|
-
Class.new(Inum::Base) { define_enum :REDBULL, :no_int }
|
24
|
+
Class.new(Inum::Base) { define_enum :REDBULL, :no_int }
|
25
25
|
}.to raise_error
|
26
|
-
|
26
|
+
|
27
27
|
# dup name.
|
28
28
|
expect{
|
29
|
-
Class.new(Inum::Base) {
|
30
|
-
define_enum :REDBULL, 0
|
31
|
-
define_enum :REDBULL, 1
|
29
|
+
Class.new(Inum::Base) {
|
30
|
+
define_enum :REDBULL, 0
|
31
|
+
define_enum :REDBULL, 1
|
32
32
|
}
|
33
33
|
}.to raise_error
|
34
|
-
|
34
|
+
|
35
35
|
# dup value.
|
36
36
|
expect{
|
37
|
-
Class.new(Inum::Base) {
|
38
|
-
define_enum :REDBULL, 0
|
39
|
-
define_enum :REDBULL2, 0
|
37
|
+
Class.new(Inum::Base) {
|
38
|
+
define_enum :REDBULL, 0
|
39
|
+
define_enum :REDBULL2, 0
|
40
40
|
}
|
41
41
|
}.to raise_error
|
42
42
|
end
|
@@ -47,7 +47,7 @@ describe Inum::Base do
|
|
47
47
|
define_enum :MONSTER
|
48
48
|
define_enum :BURN
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
expect(enum::REDBULL.to_i).to eq(0)
|
52
52
|
expect(enum::MONSTER.to_i).to eq(1)
|
53
53
|
expect(enum::BURN.to_i).to eq(2)
|
@@ -123,6 +123,10 @@ describe Inum::Base do
|
|
123
123
|
expect( @enum::REDBULL.to_t ).to eq('Good drink!')
|
124
124
|
end
|
125
125
|
|
126
|
+
it 'to_u return underscore string.' do
|
127
|
+
expect( @enum::REDBULL.to_u ).to eq('redbull')
|
128
|
+
end
|
129
|
+
|
126
130
|
it 'each can execute block with enum' do
|
127
131
|
count = 0
|
128
132
|
expect{
|
@@ -139,26 +143,26 @@ describe Inum::Base do
|
|
139
143
|
end
|
140
144
|
|
141
145
|
it 'labels return Array<Symbol>.' do
|
142
|
-
expect(@enum::labels.length).to eq(3)
|
146
|
+
expect(@enum::labels.length).to eq(3)
|
143
147
|
expect(@enum::labels.instance_of?(Array)).to be_true
|
144
|
-
expect(@enum::labels[0].instance_of?(Symbol)).to be_true
|
148
|
+
expect(@enum::labels[0].instance_of?(Symbol)).to be_true
|
145
149
|
end
|
146
|
-
|
150
|
+
|
147
151
|
it 'length return count of enum.' do
|
148
152
|
expect(@enum::length).to eq(3)
|
149
153
|
end
|
150
|
-
|
154
|
+
|
151
155
|
it 'to_a return Array<Array<Symbol,Integer>>.' do
|
152
156
|
expect(@enum::to_a.length).to eq(3)
|
153
157
|
expect(@enum::to_a.instance_of?(Array)).to be_true
|
154
158
|
expect(@enum::to_a[0].instance_of?(Array)).to be_true
|
155
159
|
expect(@enum::to_a[0][0].instance_of?(Symbol)).to be_true
|
156
160
|
expect(@enum::to_a[0][1].integer?).to be_true
|
157
|
-
|
161
|
+
|
158
162
|
expect(@enum::to_a[0][0]).to eq(:REDBULL)
|
159
163
|
(0..2).each{|i| expect(@enum::to_a[i][1]).to eq(i) }
|
160
164
|
end
|
161
|
-
|
165
|
+
|
162
166
|
it 'to_h return Hash' do
|
163
167
|
expect(@enum::to_h.instance_of?(Hash)).to be_true
|
164
168
|
expect(@enum::to_h.eql?(@enum::DEFINED_ENUMS)).to be_true
|
@@ -180,11 +184,11 @@ describe Inum::Base do
|
|
180
184
|
it 'parse return instance from self instance.' do
|
181
185
|
expect( @enum::parse(@enum::REDBULL) ).to eq( @enum::REDBULL )
|
182
186
|
end
|
183
|
-
|
187
|
+
|
184
188
|
it 'values return Array<Integer>.' do
|
185
|
-
expect(@enum::values.length).to eq(3)
|
189
|
+
expect(@enum::values.length).to eq(3)
|
186
190
|
expect(@enum::values.instance_of?(Array)).to be_true
|
187
|
-
expect(@enum::values[0].integer?).to be_true
|
191
|
+
expect(@enum::values[0].integer?).to be_true
|
188
192
|
end
|
189
193
|
end
|
190
194
|
end
|
data/spec/inum/utils_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Inum::Utils do
|
|
7
7
|
expect(Inum::Utils::underscore('camelcase')).to eq('camelcase')
|
8
8
|
expect(Inum::Utils::underscore('CamelCase')).to eq('camel_case')
|
9
9
|
expect(Inum::Utils::underscore('CamelCaseWord')).to eq('camel_case_word')
|
10
|
-
|
10
|
+
|
11
11
|
expect(Inum::Utils::underscore('Came1Case')).to eq('came1_case')
|
12
12
|
|
13
13
|
expect(Inum::Utils::underscore('UNDER_SCORE')).to eq('under_score')
|
@@ -16,7 +16,7 @@ describe Inum::Utils do
|
|
16
16
|
expect(Inum::Utils::underscore('name::space')).to eq('name.space')
|
17
17
|
expect(Inum::Utils::underscore('Name::Space')).to eq('name.space')
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it 'underscore can convert Symbol to underscore_word.' do
|
21
21
|
expect(Inum::Utils::underscore(:CamelCase)).to eq('camel_case')
|
22
22
|
end
|
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: 1.3.
|
4
|
+
version: 1.3.5
|
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-11-
|
11
|
+
date: 2013-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|