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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99359111fe269cc5e91a4b3cfcca01412405ce0e
4
- data.tar.gz: d41f12ce30debb7bb1e566bc64658d3ba3c7da73
3
+ metadata.gz: 7a13fbdf7013fbb32ae52aa22ed0a6190c952ce2
4
+ data.tar.gz: 8b6d48c73567b96073e0f4b4774ed11ca372a2eb
5
5
  SHA512:
6
- metadata.gz: afc248454f732406d18b7a38a36a03e1b0106eae10bf9aa81c9727916420b58551aa784a4201516b0baf74cd148c791747f0039884c38a66ae33936dbfe2aac3
7
- data.tar.gz: e626427aa8db41a1d7ecca7f95844a5f40aefe5374918195a07d2725701eca0e84ca860388b3e08af4314e1946842c482ad2de6f9ab67bb30dbdf215ed9c1d10
6
+ metadata.gz: fb3b3e5298b8993ca0b3bff39b7437a44ff82224c749457995e1298360ec37fd164d025162a36e808255d44203ae5f7a6e33f7fb65a288c0a884af4c59edd4e5
7
+ data.tar.gz: 322444e9be0196b9339b6491d36658005511b2f23fe2e07ccd02570cc06780fbb78bbac3a967e233eb2419abbc0582d0a3259dbff69fa31640f4816939772d20
data/.travis.yml CHANGED
@@ -5,7 +5,8 @@ before_install:
5
5
  rvm:
6
6
  - 1.9.3
7
7
  - 2.1.0
8
+ - 2.1.2
8
9
  env:
9
- - RAILS=3.2.16
10
- - RAILS=4.0.2
11
- - RAILS=4.1.0.beta1
10
+ - ACTIVE_RECORD_VERSION=3.2.16
11
+ - ACTIVE_RECORD_VERSION=4.0.0
12
+ - ACTIVE_RECORD_VERSION=4.1.4
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ if ENV['ACTIVE_RECORD_VERSION']
4
+ gem 'activerecord', ENV['ACTIVE_RECORD_VERSION']
5
+ end
6
+
3
7
  # Specify your gem's dependencies in inum.gemspec
4
8
  gemspec
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 AnimeType < Inum::Base
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, value(integer) is auto-incremented.
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 AnimeType::EVANGELION.label # => :EVANGELION (if use to_s, return "EVANGELION")
45
- p AnimeType::EVANGELION.value # => 0 (can use to_i.)
46
- p AnimeType::EVANGELION.translate # => エヴァンゲリオン (i18n find `inum.anime_type.evangelion`.)
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
- # parse object to instance of AnimeType.
49
- # object can use class of Symbol or String or Integer or Self.
50
- type = AnimeType.parse(0)
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
- p AnimeType::HARUHI + 1 # => NYARUKO
54
- p AnimeType::NYARUKO - 1 # => HARUHI
49
+ # Compare object.
50
+ p AnimeTypes::HARUHI.eql?('HARUHI') # => true (This method can compare all parsable object.)
55
51
 
56
52
  # each method.
57
- AnimeType::each {|enum| p enum }
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
- ### if use ActiveRecord, can use bind\_inum
60
+ ### If use ActiveRecord, can use bind\_inum
66
61
 
67
62
  ``` ruby
68
- class Anime < ActiveRecord::Base
69
- bind_inum :column => :type, :class => AnimeType
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 generates useful methods and validation.
68
+ bind\_enum wrap accessor of column.
80
69
 
81
70
  ``` ruby
82
- anime = Anime.new(type: AnimeType::NYARUKO)
83
- p anime.type.t # => '這いよれ!ニャル子さん' t is aliased translate.
71
+ fav = Favorite.new(anime: AnimeTypes::NYARUKO)
84
72
 
85
- p anime.type_evangelion? # => false
86
- p anime.type_nyaruko? # => true
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
- p anime.type_haruhi? # => true
78
+ anime.type = 'NYARUKO'
92
79
 
80
+ # check methods.
81
+ p fav.anime_evangelion? # => false
93
82
  ```
94
83
 
95
84
  ### Localize(i18n)
96
- to_t methods localize enum by i18n.
97
- default i18n key is inum.{name_space}.{class_name}.{enum_member_label}.
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
- Inum::Utils::underscore("inum::#{self.name}::#{label}")
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.add_development_dependency "bundler", "~> 1.3"
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", "~> 0.6.5"
30
+ spec.add_dependency "i18n"
31
+ spec.add_dependency "activesupport"
29
32
  end
data/lib/inum.rb CHANGED
@@ -5,6 +5,8 @@ require 'inum/base'
5
5
  require 'inum/active_record_mixin.rb'
6
6
 
7
7
  module Inum
8
+ class NotDefined < StandardError; end
9
+
8
10
  if Object.const_defined?(:ActiveRecord)
9
11
  ActiveRecord::Base.send :extend, Inum::ActiveRecordMixin
10
12
  end
@@ -9,44 +9,32 @@ module Inum
9
9
  module ActiveRecordMixin
10
10
  # Define compare method in class.
11
11
  #
12
- # @param options [Hash] option
13
- # @option options [Symbol] :column Binding column. (require)
14
- # @option options [Inum:Base] :class Binding enum. (require)
15
- # @option options [Symbol] :prefix Prefix. (default: "{option[:column]}_")
16
- # @option options [Bool] :validation Enable validation (default: true)
17
- # @option options [Bool] :allow_nil Allow nil when validate. (default: false)
18
- def bind_inum(options)
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
- if opts[:validation]
28
- validates opts[:column], {
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("#{opts[:column]}") do
35
- opts[:class].parse(read_attribute(opts[:column]))
36
- end
37
-
38
- define_method("#{opts[:column]}=") do |value|
39
- enum = opts[:class].parse(value)
40
- if enum
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
- opts[:class].each do |enum|
48
- define_method("#{opts[:prefix]}#{enum.underscore}?") do
49
- enum.eql?(send(opts[:column]))
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 = label
25
- @value = value
26
-
27
- @underscore_label = Inum::Utils::underscore(label)
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
- @label.to_s
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(self.class.i18n_key(@label))
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
- enum_format.keys
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
- enum_format.length
134
+ @enums.length
124
135
  end
125
136
 
126
- # return array of Enums.
137
+ # Parse object to Enum.
127
138
  #
128
- # @return [Array<Inum>] sorted array of Enums.
129
- def self.to_a
130
- enums.dup
131
- end
132
-
133
- # return hash of Enums.
134
- #
135
- # @return [Hash<Symbol, Integer>] hash of Enums.
136
- def self.to_h
137
- enum_format.dup
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 Object to Enum.(unsafe:An exception may occur.)
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] instance of Inum::Base. or raise Exception.
162
+ # @return [Inum::Base] enum.
144
163
  def self.parse!(object)
145
- case object
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
- # Parse Object to Enum.
167
+ # return array of Enums.
160
168
  #
161
- # @param object [Object] string or symbol or integer or Inum::Base.
162
- # @return [Inum::Base] instance of Inum::Base. or nil.
163
- def self.parse(object)
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
- enum_format.values
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 = enum_format.size)
192
- value = value.to_i
185
+ def self.define(label, value = @enums.size)
193
186
  validate_enum_args!(label, value)
194
187
 
195
- enum = new(label, value)
196
- enum_format[label] = value
197
-
198
- enums.push(enum)
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
- # call after inherited.
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, and raise exception.
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, "The label(#{label}!) isn't instance of Symbol."
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, "The label(#{label}!) already exists!!"
214
+ raise ArgumentError, "#{label} already exists label."
247
215
  end
248
216
 
249
217
  if values.include?(value)
250
- raise ArgumentError, "The value(#{value}!) already exists!!"
218
+ raise ArgumentError, "#{value} already exists value."
251
219
  end
252
220
  end
253
221