inum 3.0.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/Gemfile +4 -0
- data/README.md +35 -49
- data/inum.gemspec +6 -3
- data/lib/inum.rb +2 -0
- data/lib/inum/active_record_mixin.rb +19 -31
- data/lib/inum/base.rb +75 -107
- data/lib/inum/version.rb +1 -1
- data/spec/inum/active_record_mixin_spec.rb +58 -93
- data/spec/inum/base_spec.rb +235 -132
- data/spec/spec_helper.rb +2 -2
- metadata +26 -15
- data/lib/inum/utils.rb +0 -23
- data/spec/inum/utils_spec.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a13fbdf7013fbb32ae52aa22ed0a6190c952ce2
|
4
|
+
data.tar.gz: 8b6d48c73567b96073e0f4b4774ed11ca372a2eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb3b3e5298b8993ca0b3bff39b7437a44ff82224c749457995e1298360ec37fd164d025162a36e808255d44203ae5f7a6e33f7fb65a288c0a884af4c59edd4e5
|
7
|
+
data.tar.gz: 322444e9be0196b9339b6491d36658005511b2f23fe2e07ccd02570cc06780fbb78bbac3a967e233eb2419abbc0582d0a3259dbff69fa31640f4816939772d20
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -24,97 +24,83 @@ Or install it yourself as:
|
|
24
24
|
For example create enum(inum) of Japanese Anime.
|
25
25
|
|
26
26
|
``` ruby
|
27
|
-
class
|
27
|
+
class AnimeTypes < Inum::Base
|
28
28
|
define :EVANGELION, 0
|
29
29
|
define :HARUHI, 1
|
30
30
|
define :NYARUKO, 2
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
|
-
If value(integer) is omitted,
|
35
|
-
|
36
|
-
``` ruby
|
37
|
-
define :EVANGELION # => value will auto-increment.
|
38
|
-
```
|
34
|
+
If the value(integer) is omitted, It is auto-incremented.(deprecated.)
|
39
35
|
|
40
36
|
### Use Enum(Inum)
|
41
37
|
How to use Enum(Inum).
|
42
38
|
|
43
39
|
``` ruby
|
44
|
-
p
|
45
|
-
p
|
46
|
-
p
|
40
|
+
p AnimeTypes::EVANGELION.label # => :EVANGELION
|
41
|
+
p AnimeTypes::EVANGELION.to_s # => "EVANGELION"
|
42
|
+
p AnimeTypes::EVANGELION.value # => 0 (can use to_i.)
|
43
|
+
p AnimeTypes::EVANGELION.translate # => エヴァンゲリオン (I18n.t will be called with `anime_types.evangelion`.)
|
47
44
|
|
48
|
-
#
|
49
|
-
#
|
50
|
-
|
51
|
-
p AnimeType::HARUHI.eql?('HARUHI') # => true (eql? can compare all parsable object.)
|
45
|
+
# Parse object.
|
46
|
+
# This method is parsable Symbol and String and Integer and Self.
|
47
|
+
p AnimeTypes.parse(1) # => AnimeTypes::HARUHI
|
52
48
|
|
53
|
-
|
54
|
-
p
|
49
|
+
# Compare object.
|
50
|
+
p AnimeTypes::HARUHI.eql?('HARUHI') # => true (This method can compare all parsable object.)
|
55
51
|
|
56
52
|
# each method.
|
57
|
-
|
58
|
-
|
53
|
+
AnimeTypes.each {|enum| p enum}
|
59
54
|
```
|
60
55
|
|
61
56
|
can use Enumerable and Comparable.
|
62
57
|
|
63
58
|
- more detail is [Class::Base](http://rubydoc.info/github/alfa-jpn/inum/Inum/Base)
|
64
59
|
|
65
|
-
###
|
60
|
+
### If use ActiveRecord, can use bind\_inum
|
66
61
|
|
67
62
|
``` ruby
|
68
|
-
class
|
69
|
-
bind_inum :
|
63
|
+
class Favorite < ActiveRecord::Base
|
64
|
+
bind_inum :anime, AnimeTypes
|
70
65
|
end
|
71
|
-
|
72
|
-
# if set prefix.
|
73
|
-
class Anime < ActiveRecord::Base
|
74
|
-
bind_inum :column => :type, :class => AnimeType, :prefix => nil # remove prefix, when prefix is nil.
|
75
|
-
end
|
76
|
-
|
77
66
|
```
|
78
67
|
|
79
|
-
bind\_enum
|
68
|
+
bind\_enum wrap accessor of column.
|
80
69
|
|
81
70
|
``` ruby
|
82
|
-
|
83
|
-
p anime.type.t # => '這いよれ!ニャル子さん' t is aliased translate.
|
71
|
+
fav = Favorite.new(anime: AnimeTypes::NYARUKO)
|
84
72
|
|
85
|
-
|
86
|
-
p anime.
|
73
|
+
# #getter will return instance of AnimeTypes.
|
74
|
+
p fav.anime.t # => '這いよれ!ニャル子さん' t is aliased translate.
|
87
75
|
|
88
|
-
|
89
|
-
# type can use all parsable object.
|
76
|
+
# #setter can set parsable object.
|
90
77
|
anime.type = 1
|
91
|
-
|
78
|
+
anime.type = 'NYARUKO'
|
92
79
|
|
80
|
+
# check methods.
|
81
|
+
p fav.anime_evangelion? # => false
|
93
82
|
```
|
94
83
|
|
95
84
|
### Localize(i18n)
|
96
|
-
|
97
|
-
default i18n key is
|
85
|
+
translate methods localize enum by i18n.
|
86
|
+
default i18n key is `#{name_space}.#{class_name}.#{label}`.
|
87
|
+
|
88
|
+
``` yaml
|
89
|
+
ja:
|
90
|
+
anime_types:
|
91
|
+
evangelion: 'エヴァンゲリオン'
|
92
|
+
haruhi: 'ハルヒ'
|
93
|
+
nyaruko: '這いよれ!ニャル子さん'
|
94
|
+
```
|
95
|
+
|
98
96
|
If change default, Override i18n_key class method.
|
99
97
|
|
100
98
|
``` ruby
|
101
|
-
# default i18n_key.
|
102
99
|
def self.i18n_key(label)
|
103
|
-
|
100
|
+
"hogehoge.#{label}"
|
104
101
|
end
|
105
102
|
```
|
106
103
|
|
107
|
-
example
|
108
|
-
|
109
|
-
``` yaml
|
110
|
-
ja:
|
111
|
-
inum:
|
112
|
-
anime_type:
|
113
|
-
evangelion: 'エヴァンゲリオン'
|
114
|
-
haruhi: 'ハルヒ'
|
115
|
-
nyaruko: '這いよれ!ニャル子さん'
|
116
|
-
```
|
117
|
-
|
118
104
|
## API DOCUMENT
|
119
105
|
|
120
106
|
- [Documentation for alfa-jpn/Inum(master)](http://rubydoc.info/github/alfa-jpn/inum/frames)
|
data/inum.gemspec
CHANGED
@@ -18,12 +18,15 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
21
|
+
spec.required_ruby_version = ">= 1.9.3"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
24
|
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
24
26
|
spec.add_development_dependency "yard"
|
25
27
|
spec.add_development_dependency "activerecord"
|
26
28
|
spec.add_development_dependency "sqlite3"
|
27
29
|
|
28
|
-
spec.add_dependency "i18n"
|
30
|
+
spec.add_dependency "i18n"
|
31
|
+
spec.add_dependency "activesupport"
|
29
32
|
end
|
data/lib/inum.rb
CHANGED
@@ -9,44 +9,32 @@ module Inum
|
|
9
9
|
module ActiveRecordMixin
|
10
10
|
# Define compare method in class.
|
11
11
|
#
|
12
|
-
# @param
|
13
|
-
# @
|
14
|
-
# @
|
15
|
-
# @option options [Symbol] :prefix Prefix.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
opts = {
|
20
|
-
allow_nil: false,
|
21
|
-
prefix: options[:column],
|
22
|
-
validation: true,
|
23
|
-
}.merge(options)
|
24
|
-
opts[:prefix] = (opts[:prefix].nil?)? '' : "#{opts[:prefix]}_"
|
12
|
+
# @param column [Symbol] Binding column name.
|
13
|
+
# @param enum_class [Inum::Base] Binding Enum.
|
14
|
+
# @param options [Hash] option
|
15
|
+
# @option options [Symbol] :prefix Prefix. (default: column)
|
16
|
+
def bind_inum(column, enum_class, options = {})
|
17
|
+
options = { prefix: column }.merge(options)
|
18
|
+
options[:prefix] = options[:prefix] ? "#{options[:prefix]}_" : ''
|
25
19
|
|
26
20
|
self.class_eval do
|
27
|
-
|
28
|
-
|
29
|
-
allow_nil: opts[:allow_nil],
|
30
|
-
inclusion: {in: opts[:class].to_a},
|
31
|
-
}
|
21
|
+
define_method(column) do
|
22
|
+
enum_class.parse(read_attribute(column))
|
32
23
|
end
|
33
24
|
|
34
|
-
define_method("#{
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
write_attribute(opts[:column], enum.to_i)
|
42
|
-
else
|
43
|
-
write_attribute(opts[:column], nil)
|
25
|
+
define_method("#{column}=") do |value|
|
26
|
+
enum_class.parse(value).tap do |enum|
|
27
|
+
if enum
|
28
|
+
write_attribute(column, enum.to_i)
|
29
|
+
else
|
30
|
+
write_attribute(column, nil)
|
31
|
+
end
|
44
32
|
end
|
45
33
|
end
|
46
34
|
|
47
|
-
|
48
|
-
define_method("#{
|
49
|
-
enum.eql?(
|
35
|
+
enum_class.each do |enum|
|
36
|
+
define_method("#{options[:prefix]}#{enum.to_s.underscore}?") do
|
37
|
+
enum.eql?(read_attribute(column))
|
50
38
|
end
|
51
39
|
end
|
52
40
|
end
|
data/lib/inum/base.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Inum
|
2
|
+
require 'active_support/inflector'
|
2
3
|
require 'i18n'
|
3
|
-
require 'inum/utils'
|
4
4
|
|
5
5
|
# InumBase class.
|
6
6
|
#
|
@@ -21,10 +21,10 @@ module Inum
|
|
21
21
|
# @param label [Symbol] label of Enum.
|
22
22
|
# @param value [Integer] value of Enum.
|
23
23
|
def initialize(label, value)
|
24
|
-
@label
|
25
|
-
@
|
26
|
-
|
27
|
-
@
|
24
|
+
@label = label
|
25
|
+
@label_string = label.to_s
|
26
|
+
@value = value
|
27
|
+
@i18n_key = ActiveSupport::Inflector.underscore("#{self.class.name}::#{label}").gsub('/', '.')
|
28
28
|
end
|
29
29
|
|
30
30
|
# Compare object.
|
@@ -32,11 +32,10 @@ module Inum
|
|
32
32
|
# @param object [Object] parsable object.
|
33
33
|
# @return [Integer] same normal <=>.
|
34
34
|
def <=> (object)
|
35
|
-
other = self.class.parse(object)
|
36
|
-
if other.nil?
|
37
|
-
nil
|
38
|
-
else
|
35
|
+
if other = self.class.parse(object)
|
39
36
|
@value <=> other.to_i
|
37
|
+
else
|
38
|
+
nil
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
@@ -75,7 +74,7 @@ module Inum
|
|
75
74
|
#
|
76
75
|
# @return [String] Label(String).
|
77
76
|
def to_s
|
78
|
-
@
|
77
|
+
@label_string
|
79
78
|
end
|
80
79
|
|
81
80
|
# Translate Enum to localized string.(use i18n)
|
@@ -83,17 +82,10 @@ module Inum
|
|
83
82
|
#
|
84
83
|
# @return [String] localized string of Enum.
|
85
84
|
def translate
|
86
|
-
I18n.t(
|
85
|
+
I18n.t(@i18n_key)
|
87
86
|
end
|
88
87
|
alias_method :t, :translate
|
89
88
|
|
90
|
-
# Enum label to underscore string.
|
91
|
-
#
|
92
|
-
# @return [String] under_score string value(label) of Enum.
|
93
|
-
def underscore
|
94
|
-
@underscore_label
|
95
|
-
end
|
96
|
-
|
97
89
|
# Value of Enum.
|
98
90
|
#
|
99
91
|
# @return [Integer] Value of Enum.
|
@@ -102,152 +94,128 @@ module Inum
|
|
102
94
|
end
|
103
95
|
alias_method :to_i, :value
|
104
96
|
|
97
|
+
# Get collection.
|
98
|
+
# @note Type of usable with a Rails form helper.
|
99
|
+
# @exapmle
|
100
|
+
# f.select :name, Enum.collection
|
101
|
+
# f.select :name, Enum.collection(except:[:HOGE]) # Except Enum::HOGE
|
102
|
+
# f.select :name, Enum.collection(only:[:HOGE, :FUGA]) # Only Enum::HOGE and Enum::FUGA
|
103
|
+
#
|
104
|
+
# @param option [Hash] Options.
|
105
|
+
# @option option [Array<Symbol>] except Except enum.
|
106
|
+
# @option option [Array<Symbol>] only Limit enum.
|
107
|
+
# @return [Array<Array>] collection.
|
108
|
+
def self.collection(option)
|
109
|
+
map { |e|
|
110
|
+
next if option[:except] and option[:except].include?(e.label)
|
111
|
+
next if option[:only] and !option[:only].include?(e.label)
|
112
|
+
[e.translate, e.value]
|
113
|
+
}.compact
|
114
|
+
end
|
115
|
+
|
105
116
|
# Execute the yield(block) with each member of enum.
|
106
117
|
#
|
107
118
|
# @param &block [proc{|enum| .. }] execute the block.
|
108
119
|
def self.each(&block)
|
109
|
-
enums.each(&block)
|
120
|
+
@enums.each(&block)
|
110
121
|
end
|
111
122
|
|
112
123
|
# get all labels of Enum.
|
113
124
|
#
|
114
125
|
# @return [Array<Symbol>] all labels of Enum.
|
115
126
|
def self.labels
|
116
|
-
|
127
|
+
@enums.map(&:label)
|
117
128
|
end
|
118
129
|
|
119
130
|
# get Enum length.
|
120
131
|
#
|
121
132
|
# @return [Integer] count of Enums.
|
122
133
|
def self.length
|
123
|
-
|
134
|
+
@enums.length
|
124
135
|
end
|
125
136
|
|
126
|
-
#
|
137
|
+
# Parse object to Enum.
|
127
138
|
#
|
128
|
-
# @
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
# @param object [Object] string or symbol or integer or Inum::Base.
|
140
|
+
# @return [Inum::Base, Nil] enum or nil.
|
141
|
+
def self.parse(object)
|
142
|
+
case object
|
143
|
+
when String
|
144
|
+
upcase = object.upcase
|
145
|
+
find {|e| e.to_s == upcase}
|
146
|
+
when Symbol
|
147
|
+
upcase = object.upcase
|
148
|
+
find {|e| e.label == upcase}
|
149
|
+
when Integer
|
150
|
+
find {|e| e.value == object}
|
151
|
+
when self
|
152
|
+
object
|
153
|
+
else
|
154
|
+
nil
|
155
|
+
end
|
138
156
|
end
|
139
157
|
|
140
|
-
# Parse
|
158
|
+
# Parse object to Enum.
|
159
|
+
# @raise [Inum::NotDefined] raise if not found.
|
141
160
|
#
|
142
161
|
# @param object [Object] string or symbol or integer or Inum::Base.
|
143
|
-
# @return [Inum::Base]
|
162
|
+
# @return [Inum::Base] enum.
|
144
163
|
def self.parse!(object)
|
145
|
-
|
146
|
-
when String
|
147
|
-
self.const_get(object)
|
148
|
-
when Symbol
|
149
|
-
parse object.to_s
|
150
|
-
when Integer
|
151
|
-
parse self.enum_format.key(object).to_s
|
152
|
-
when self
|
153
|
-
object
|
154
|
-
else
|
155
|
-
raise ArgumentError, "#{object} is nani?"
|
156
|
-
end
|
164
|
+
parse(object) || raise(Inum::NotDefined)
|
157
165
|
end
|
158
166
|
|
159
|
-
#
|
167
|
+
# return array of Enums.
|
160
168
|
#
|
161
|
-
# @
|
162
|
-
|
163
|
-
|
164
|
-
case object
|
165
|
-
when String
|
166
|
-
return nil unless labels.include?(object.to_sym)
|
167
|
-
when Symbol
|
168
|
-
return nil unless labels.include?(object)
|
169
|
-
when Integer
|
170
|
-
return nil unless values.include?(object)
|
171
|
-
when self
|
172
|
-
# do nothing.
|
173
|
-
else
|
174
|
-
return nil
|
175
|
-
end
|
176
|
-
parse!(object)
|
169
|
+
# @return [Array<Inum>] sorted array of Enums.
|
170
|
+
def self.to_a
|
171
|
+
@enums.dup
|
177
172
|
end
|
178
173
|
|
179
174
|
# get all values of Enum.
|
180
175
|
#
|
181
176
|
# @return [Array<Integer>] all values of Enum.
|
182
177
|
def self.values
|
183
|
-
|
178
|
+
@enums.map(&:value)
|
184
179
|
end
|
185
180
|
|
186
|
-
|
187
|
-
# Define Enum in called class.
|
181
|
+
# Define Enum.
|
188
182
|
#
|
189
183
|
# @param label [Symbol] label of Enum.
|
190
184
|
# @param value [Integer] value of Enum.(default:autoincrement for 0.)
|
191
|
-
def self.define(label, value =
|
192
|
-
value = value.to_i
|
185
|
+
def self.define(label, value = @enums.size)
|
193
186
|
validate_enum_args!(label, value)
|
194
187
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
self.const_set(label, enum)
|
200
|
-
end
|
201
|
-
|
202
|
-
# get hash of @enum_format.
|
203
|
-
# @private
|
204
|
-
#
|
205
|
-
# @return [Hash] format(hash) of enum.
|
206
|
-
def self.enum_format
|
207
|
-
@enum_format
|
208
|
-
end
|
209
|
-
|
210
|
-
# get array of @enums.
|
211
|
-
# @private
|
212
|
-
#
|
213
|
-
# @return [Array] array of defined enums.
|
214
|
-
def self.enums
|
215
|
-
@enums
|
216
|
-
end
|
217
|
-
|
218
|
-
# get key for I18n.t method.
|
219
|
-
# @note default: Namespace.Classname.Label(label of enum member.)
|
220
|
-
#
|
221
|
-
# @param label [Symbol] label of enum member.
|
222
|
-
# @return [String] key for I18n.t method.
|
223
|
-
# @abstract If change key from the default.
|
224
|
-
def self.i18n_key(label)
|
225
|
-
Inum::Utils::underscore("inum::#{self.name}::#{label}")
|
188
|
+
new(label, value).tap do |enum|
|
189
|
+
const_set(label, enum)
|
190
|
+
@enums.push(enum)
|
191
|
+
end
|
226
192
|
end
|
227
193
|
|
228
|
-
#
|
229
|
-
#
|
230
|
-
# @note Define hash of :enum_format in child.
|
194
|
+
# Initialize inherited class.
|
231
195
|
def self.inherited(child)
|
232
|
-
child.instance_variable_set(:@enum_format, Hash.new)
|
233
196
|
child.instance_variable_set(:@enums, Array.new)
|
234
197
|
end
|
235
198
|
|
236
|
-
# Validate enum args
|
199
|
+
# Validate enum args.
|
200
|
+
# @raise [ArgumentError] If argument is wrong.
|
237
201
|
#
|
238
202
|
# @param label [Object] label of Enum.
|
239
203
|
# @param value [Integer] value of Enum.
|
240
204
|
def self.validate_enum_args!(label, value)
|
241
205
|
unless label.instance_of?(Symbol)
|
242
|
-
raise ArgumentError, "
|
206
|
+
raise ArgumentError, "#{label} isn't instance of Symbol."
|
207
|
+
end
|
208
|
+
|
209
|
+
if labels =~ /[^A-Z\d_]/
|
210
|
+
raise ArgumentError, "#{label} is wrong constant name. Label allow uppercase and digits and underscore."
|
243
211
|
end
|
244
212
|
|
245
213
|
if labels.include?(label)
|
246
|
-
raise ArgumentError, "
|
214
|
+
raise ArgumentError, "#{label} already exists label."
|
247
215
|
end
|
248
216
|
|
249
217
|
if values.include?(value)
|
250
|
-
raise ArgumentError, "
|
218
|
+
raise ArgumentError, "#{value} already exists value."
|
251
219
|
end
|
252
220
|
end
|
253
221
|
|