inum 2.1.2 → 3.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: c5055c9ab3014b32c6e8aec6eaaa6bfdc7b78ba3
4
- data.tar.gz: 7fed7e2c4a17826aa469410d02e38fad5b39b616
3
+ metadata.gz: 99359111fe269cc5e91a4b3cfcca01412405ce0e
4
+ data.tar.gz: d41f12ce30debb7bb1e566bc64658d3ba3c7da73
5
5
  SHA512:
6
- metadata.gz: e08410058e6c7aff4ffd11ced7dca2d3132f7e7e77885cc9301cc1d673b9674e441970d2471881ac3958b6c22fd1833c3632834e0febbd81aeb1560f111bcd8a
7
- data.tar.gz: 0de1515b63dd86c93099f37f29b51d77f8cfb7d066d27d17e80c16395ab4dd2b0dccf286801b626b35bf0865863e9ba31946c5ccaac8a3d44c9834a201454cbb
6
+ metadata.gz: afc248454f732406d18b7a38a36a03e1b0106eae10bf9aa81c9727916420b58551aa784a4201516b0baf74cd148c791747f0039884c38a66ae33936dbfe2aac3
7
+ data.tar.gz: e626427aa8db41a1d7ecca7f95844a5f40aefe5374918195a07d2725701eca0e84ca860388b3e08af4314e1946842c482ad2de6f9ab67bb30dbdf215ed9c1d10
data/.gitignore CHANGED
@@ -16,4 +16,7 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  vendor/
19
- nbproject/
19
+
20
+ # IDE Projects files.
21
+ nbproject/
22
+ .project
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ before_install:
4
+ - gem install bundler
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.1.0
8
+ env:
9
+ - RAILS=3.2.16
10
+ - RAILS=4.0.2
11
+ - RAILS=4.1.0.beta1
data/README.md CHANGED
@@ -25,37 +25,35 @@ For example create enum(inum) of Japanese Anime.
25
25
 
26
26
  ``` ruby
27
27
  class AnimeType < Inum::Base
28
- define_enum :EVANGELION, 0
29
- define_enum :HARUHI, 1
30
- define_enum :NYARUKO, 2
28
+ define :EVANGELION, 0
29
+ define :HARUHI, 1
30
+ define :NYARUKO, 2
31
31
  end
32
32
  ```
33
33
 
34
34
  If value(integer) is omitted, value(integer) is auto-incremented.
35
35
 
36
36
  ``` ruby
37
- define_enum :EVANGELION # => value will auto-increment.
37
+ define :EVANGELION # => value will auto-increment.
38
38
  ```
39
39
 
40
40
  ### Use Enum(Inum)
41
41
  How to use Enum(Inum).
42
42
 
43
43
  ``` ruby
44
- p AnimeType::EVANGELION # => EVANGELION
45
- p AnimeType::EVANGELION.to_i # => 0
44
+ p AnimeType::EVANGELION.label # => :EVANGELION (if use to_s, return "EVANGELION")
45
+ p AnimeType::EVANGELION.value # => 0 (can use to_i.)
46
46
  p AnimeType::EVANGELION.translate # => エヴァンゲリオン (i18n find `inum.anime_type.evangelion`.)
47
47
 
48
48
  # parse object to instance of AnimeType.
49
49
  # object can use class of Symbol or String or Integer or Self.
50
- type = AnimeType::parse(0)
51
- p type.equal?(AnimeType::EVANGELION) # => true (member of Enum is singleton.)
52
-
50
+ type = AnimeType.parse(0)
53
51
  p AnimeType::HARUHI.eql?('HARUHI') # => true (eql? can compare all parsable object.)
54
52
 
55
53
  p AnimeType::HARUHI + 1 # => NYARUKO
56
54
  p AnimeType::NYARUKO - 1 # => HARUHI
57
55
 
58
- # can use each method.
56
+ # each method.
59
57
  AnimeType::each {|enum| p enum }
60
58
 
61
59
  ```
@@ -64,33 +62,33 @@ can use Enumerable and Comparable.
64
62
 
65
63
  - more detail is [Class::Base](http://rubydoc.info/github/alfa-jpn/inum/Inum/Base)
66
64
 
67
- ### Use define\_check\_method
68
- define\_check\_method can extend your class.
65
+ ### if use ActiveRecord, can use bind\_inum
69
66
 
70
67
  ``` ruby
71
- class Anime
72
- extend Inum::DefineCheckMethod
73
-
74
- attr_accessor :type
68
+ class Anime < ActiveRecord::Base
69
+ bind_inum :column => :type, :class => AnimeType
70
+ end
75
71
 
76
- define_check_method :type, AnimeType
72
+ # if set prefix.
73
+ class Anime < ActiveRecord::Base
74
+ bind_inum :column => :type, :class => AnimeType, :prefix => nil # remove prefix, when prefix is nil.
77
75
  end
78
76
 
79
77
  ```
80
78
 
81
- define\_check\_method generates methods for type confirmation.
79
+ bind\_enum generates useful methods and validation.
82
80
 
83
81
  ``` ruby
84
- anime = Anime.new
85
- anime.type = AnimeType::NYARUKO
82
+ anime = Anime.new(type: AnimeType::NYARUKO)
83
+ p anime.type.t # => '這いよれ!ニャル子さん' t is aliased translate.
86
84
 
87
- p anime.evangelion? # => false
88
- p anime.nyaruko? # => true
85
+ p anime.type_evangelion? # => false
86
+ p anime.type_nyaruko? # => true
89
87
 
90
88
 
91
89
  # type can use all parsable object.
92
90
  anime.type = 1
93
- p anime.haruhi? # => true
91
+ p anime.type_haruhi? # => true
94
92
 
95
93
  ```
96
94
 
data/inum.gemspec CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "yard"
25
+ spec.add_development_dependency "activerecord"
26
+ spec.add_development_dependency "sqlite3"
25
27
 
26
28
  spec.add_dependency "i18n", "~> 0.6.5"
27
29
  end
data/lib/inum.rb CHANGED
@@ -1,3 +1,11 @@
1
- require "inum/version"
2
- require "inum/base"
3
- require "inum/define_check_method"
1
+ require 'i18n'
2
+
3
+ require 'inum/version'
4
+ require 'inum/base'
5
+ require 'inum/active_record_mixin.rb'
6
+
7
+ module Inum
8
+ if Object.const_defined?(:ActiveRecord)
9
+ ActiveRecord::Base.send :extend, Inum::ActiveRecordMixin
10
+ end
11
+ end
@@ -0,0 +1,55 @@
1
+ module Inum
2
+ # Mixin module to ActiveRecord.
3
+ #
4
+ # @example
5
+ # class Fruit < ActiveRecord::Base
6
+ # bind_enum :type, FruitType
7
+ # end
8
+ #
9
+ module ActiveRecordMixin
10
+ # Define compare method in class.
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]}_"
25
+
26
+ 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
+ }
32
+ end
33
+
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)
44
+ end
45
+ end
46
+
47
+ opts[:class].each do |enum|
48
+ define_method("#{opts[:prefix]}#{enum.underscore}?") do
49
+ enum.eql?(send(opts[:column]))
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/inum/base.rb CHANGED
@@ -7,16 +7,14 @@ module Inum
7
7
  # @abstract Inum class should be a inheritance of Inum::Base.
8
8
  # @example
9
9
  # class FruitType < Inum::Base
10
- # define_enum :APPLE, 0
11
- # define_enum :BANANA, 1
12
- # define_enum :ORANGE, 2
10
+ # define :APPLE, 0
11
+ # define :BANANA, 1
12
+ # define :ORANGE, 2
13
13
  # end
14
14
  class Base
15
15
  extend Enumerable
16
16
  include Comparable
17
17
 
18
- private_class_method :new
19
-
20
18
  # initialize Inum::Base with value.
21
19
  # @note The instance of Enum Member is singleton.
22
20
  #
@@ -66,16 +64,16 @@ module Inum
66
64
  self.equal?(self.class.parse(object))
67
65
  end
68
66
 
69
- # Enum to Integer.
67
+ # Label of Enum.
70
68
  #
71
- # @return [Integer] integer value of Enum.
72
- def to_i
73
- @value
69
+ # @return [Symbol] Label of Enum.
70
+ def label
71
+ @label
74
72
  end
75
73
 
76
74
  # Enum to String.
77
75
  #
78
- # @return [String] string value(label) of Enum.
76
+ # @return [String] Label(String).
79
77
  def to_s
80
78
  @label.to_s
81
79
  end
@@ -87,7 +85,7 @@ module Inum
87
85
  def translate
88
86
  I18n.t(self.class.i18n_key(@label))
89
87
  end
90
- alias_method :to_t, :translate
88
+ alias_method :t, :translate
91
89
 
92
90
  # Enum label to underscore string.
93
91
  #
@@ -95,41 +93,48 @@ module Inum
95
93
  def underscore
96
94
  @underscore_label
97
95
  end
98
- alias_method :to_u, :underscore
96
+
97
+ # Value of Enum.
98
+ #
99
+ # @return [Integer] Value of Enum.
100
+ def value
101
+ @value
102
+ end
103
+ alias_method :to_i, :value
99
104
 
100
105
  # Execute the yield(block) with each member of enum.
101
106
  #
102
- # @yield [enum] instance of enum.
103
- def self.each
104
- defined_enums.each_key {|key| yield parse(key.to_s)}
107
+ # @param &block [proc{|enum| .. }] execute the block.
108
+ def self.each(&block)
109
+ enums.each(&block)
105
110
  end
106
111
 
107
112
  # get all labels of Enum.
108
113
  #
109
114
  # @return [Array<Symbol>] all labels of Enum.
110
115
  def self.labels
111
- defined_enums.keys
116
+ enum_format.keys
112
117
  end
113
118
 
114
119
  # get Enum length.
115
120
  #
116
121
  # @return [Integer] count of Enums.
117
122
  def self.length
118
- defined_enums.length
123
+ enum_format.length
119
124
  end
120
125
 
121
126
  # return array of Enums.
122
127
  #
123
- # @return [Array<Array<Symbol, Integer>>] sorted array of Enums.
128
+ # @return [Array<Inum>] sorted array of Enums.
124
129
  def self.to_a
125
- defined_enums.flatten(0).sort{|a,b| a[1] <=> b[1] }
130
+ enums.dup
126
131
  end
127
132
 
128
133
  # return hash of Enums.
129
134
  #
130
135
  # @return [Hash<Symbol, Integer>] hash of Enums.
131
136
  def self.to_h
132
- defined_enums.dup
137
+ enum_format.dup
133
138
  end
134
139
 
135
140
  # Parse Object to Enum.(unsafe:An exception may occur.)
@@ -143,7 +148,7 @@ module Inum
143
148
  when Symbol
144
149
  parse object.to_s
145
150
  when Integer
146
- parse self.defined_enums.key(object).to_s
151
+ parse self.enum_format.key(object).to_s
147
152
  when self
148
153
  object
149
154
  else
@@ -175,28 +180,39 @@ module Inum
175
180
  #
176
181
  # @return [Array<Integer>] all values of Enum.
177
182
  def self.values
178
- defined_enums.values
183
+ enum_format.values
179
184
  end
180
185
 
181
- private
186
+
182
187
  # Define Enum in called class.
183
188
  #
184
189
  # @param label [Symbol] label of Enum.
185
190
  # @param value [Integer] value of Enum.(default:autoincrement for 0.)
186
- def self.define_enum(label, value = defined_enums.size)
191
+ def self.define(label, value = enum_format.size)
187
192
  value = value.to_i
188
-
189
193
  validate_enum_args!(label, value)
190
194
 
191
- defined_enums[label] = value
192
- self.const_set(label, new(label, value))
195
+ enum = new(label, value)
196
+ enum_format[label] = value
197
+
198
+ enums.push(enum)
199
+ self.const_set(label, enum)
193
200
  end
194
201
 
195
- # get hash of @defined_enums.
202
+ # get hash of @enum_format.
203
+ # @private
196
204
  #
197
- # @return [Hash] hash of defined enums.
198
- def self.defined_enums
199
- @defined_enums
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
200
216
  end
201
217
 
202
218
  # get key for I18n.t method.
@@ -210,9 +226,11 @@ module Inum
210
226
  end
211
227
 
212
228
  # call after inherited.
213
- # @note Define hash of :DEFINED_ENUMS in child.
229
+ #
230
+ # @note Define hash of :enum_format in child.
214
231
  def self.inherited(child)
215
- child.instance_variable_set(:@defined_enums, Hash.new)
232
+ child.instance_variable_set(:@enum_format, Hash.new)
233
+ child.instance_variable_set(:@enums, Array.new)
216
234
  end
217
235
 
218
236
  # Validate enum args, and raise exception.
@@ -224,13 +242,15 @@ module Inum
224
242
  raise ArgumentError, "The label(#{label}!) isn't instance of Symbol."
225
243
  end
226
244
 
227
- if defined_enums.has_key?(label)
245
+ if labels.include?(label)
228
246
  raise ArgumentError, "The label(#{label}!) already exists!!"
229
247
  end
230
248
 
231
- if defined_enums.has_value?(value)
249
+ if values.include?(value)
232
250
  raise ArgumentError, "The value(#{value}!) already exists!!"
233
251
  end
234
252
  end
253
+
254
+ private_class_method :new, :define, :validate_enum_args!
235
255
  end
236
256
  end
data/lib/inum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = "2.1.2"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -0,0 +1,144 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe Inum::ActiveRecordMixin do
5
+ create_temp_table(:tvs){|t| t.integer :anime; t.integer :favorite; t.integer :watching}
6
+
7
+ before :each do
8
+ class Anime < Inum::Base
9
+ define :NYARUKO, 0
10
+ define :MUROMISAN, 1
11
+ define :NOURIN, 2
12
+ define :KMB, 4
13
+ end
14
+
15
+ class TV < ActiveRecord::Base
16
+ bind_inum :column => :anime, :class => Anime
17
+
18
+ bind_inum :column => :favorite, :class => Anime, :prefix => :fav, :allow_nil => true
19
+
20
+ bind_inum :column => :watching, :class => Anime, :prefix => nil, :validation => false
21
+ end
22
+ end
23
+
24
+ after :each do
25
+ Object.class_eval do
26
+ remove_const :TV
27
+ remove_const :Anime
28
+ end
29
+ end
30
+
31
+ let :tv do
32
+ TV.create(anime: Anime::NYARUKO)
33
+ end
34
+
35
+ context 'Comparison methods' do
36
+ it 'Be defined' do
37
+ expect(TV.method_defined?(:anime_nyaruko?)).to be_true
38
+ expect(TV.method_defined?(:anime_muromisan?)).to be_true
39
+ expect(TV.method_defined?(:anime_nourin?)).to be_true
40
+ expect(TV.method_defined?(:anime_kmb?)).to be_true
41
+ end
42
+
43
+ it 'Can compare' do
44
+ expect(tv.anime_nyaruko?).to be_true
45
+ end
46
+ end
47
+
48
+ context 'getter' do
49
+ it 'Can set enum' do
50
+ tv.anime = Anime::NOURIN
51
+ expect(tv.anime_nourin?).to be_true
52
+ end
53
+
54
+ it 'Can set integer' do
55
+ tv.anime = 1
56
+ expect(tv.anime_muromisan?).to be_true
57
+ end
58
+
59
+ it 'Can set string' do
60
+ tv.anime = 'KMB'
61
+ expect(tv.anime_kmb?).to be_true
62
+ end
63
+
64
+ it 'Can set symbol' do
65
+ tv.anime = :KMB
66
+ expect(tv.anime_kmb?).to be_true
67
+ end
68
+ end
69
+
70
+ it 'Prefix is enable, when prefix was set.' do
71
+ expect(TV.method_defined?(:fav_nyaruko?)).to be_true
72
+ expect(TV.method_defined?(:fav_muromisan?)).to be_true
73
+ expect(TV.method_defined?(:fav_nourin?)).to be_true
74
+ expect(TV.method_defined?(:fav_kmb?)).to be_true
75
+ end
76
+
77
+ it 'Prefix is nothing, when prefix is nil.' do
78
+ expect(TV.method_defined?(:nyaruko?)).to be_true
79
+ expect(TV.method_defined?(:muromisan?)).to be_true
80
+ expect(TV.method_defined?(:nourin?)).to be_true
81
+ expect(TV.method_defined?(:kmb?)).to be_true
82
+ end
83
+
84
+ it 'setter return enum' do
85
+ expect(tv.anime.instance_of?(Anime)).to be_true
86
+ end
87
+
88
+ context 'update methods can update enum column' do
89
+ it 'update' do
90
+ tv.update!(anime: Anime::NOURIN)
91
+ expect(tv.reload.anime).to eq(Anime::NOURIN)
92
+ end
93
+
94
+ it 'update_attribute' do
95
+ tv.update_attribute(:anime, Anime::NOURIN)
96
+ expect(tv.reload.anime).to eq(Anime::NOURIN)
97
+ end
98
+ end
99
+
100
+ context 'validation' do
101
+ let :validators do
102
+ TV.validators.map{|v| v.attributes[0]}
103
+ end
104
+
105
+ it 'Enable validator' do
106
+ expect(validators).to include(:anime)
107
+ expect(validators).to include(:favorite)
108
+ end
109
+
110
+ it 'Disable validator when column has option of validation false.' do
111
+ expect(validators).not_to include(:watching)
112
+ end
113
+
114
+ context 'valid' do
115
+ it 'default is validation passed.' do
116
+ expect(tv).to be_valid
117
+ end
118
+
119
+ it 'Correct values are validation passed.' do
120
+ Anime.each do |enum|
121
+ tv.anime = enum
122
+ expect(tv).to be_valid
123
+ end
124
+ end
125
+
126
+ it 'Incorrect values are validation failed.' do
127
+ [-1000, 9999, 'Nothing!!', :Nothing].each do |value|
128
+ tv.anime = value
129
+ expect(tv).not_to be_valid
130
+ end
131
+ end
132
+
133
+ it 'Can not set nil' do
134
+ tv.anime = nil
135
+ expect(tv).not_to be_valid
136
+ end
137
+
138
+ it 'Can set nil when column has option of allow_nil true.' do
139
+ tv.favorite = nil
140
+ expect(tv).to be_valid
141
+ end
142
+ end
143
+ end
144
+ end
@@ -3,204 +3,213 @@ require 'spec_helper'
3
3
 
4
4
  describe Inum::Base do
5
5
 
6
- it 'define_enum validate correct' do
7
- # correct.
8
- expect{
9
- Class.new(Inum::Base) { define_enum :REDBULL, 0 }
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
14
- }
15
- }.not_to raise_error
16
-
17
- # wrong name.
18
- expect{
19
- Class.new(Inum::Base) { define_enum 1111, 0 }
20
- }.to raise_error
21
-
22
- # wrong value.
23
- expect{
24
- Class.new(Inum::Base) { define_enum :REDBULL, :no_int }
25
- }.to raise_error
26
-
27
- # dup name.
28
- expect{
29
- Class.new(Inum::Base) {
30
- define_enum :REDBULL, 0
31
- define_enum :REDBULL, 1
32
- }
33
- }.to raise_error
34
-
35
- # dup value.
36
- expect{
37
- Class.new(Inum::Base) {
38
- define_enum :REDBULL, 0
39
- define_enum :REDBULL2, 0
40
- }
41
- }.to raise_error
42
- end
43
-
44
- it 'define_enum called without value, value is autoincrement.' do
45
- enum = Class.new(Inum::Base) do
46
- define_enum :REDBULL
47
- define_enum :MONSTER
48
- define_enum :BURN
6
+ context 'When define enum,' do
7
+ it 'Correct definition be validation passed.' do
8
+ expect{
9
+ Class.new(Inum::Base) { define :REDBULL, 0 }
10
+ Class.new(Inum::Base) {
11
+ define :REDBULL, 0
12
+ define :MONSTER, 1
13
+ }
14
+ }.not_to raise_error
49
15
  end
50
16
 
51
- expect(enum::REDBULL.to_i).to eq(0)
52
- expect(enum::MONSTER.to_i).to eq(1)
53
- expect(enum::BURN.to_i).to eq(2)
54
- end
17
+ it 'Incorrect definition be validation failed.' do
18
+ # wrong name.
19
+ expect{
20
+ Class.new(Inum::Base) { define 1111, 0 }
21
+ }.to raise_error
22
+
23
+ # wrong value.
24
+ expect{
25
+ Class.new(Inum::Base) { define :REDBULL, :no_int }
26
+ }.to raise_error
27
+
28
+ # dup name.
29
+ expect{
30
+ Class.new(Inum::Base) {
31
+ define :REDBULL, 0
32
+ define :REDBULL, 1
33
+ }
34
+ }.to raise_error
55
35
 
56
- context 'define class of extended Inum::Base,' do
57
- before(:each) do
58
- I18n.stub(:t).and_return('Good drink!')
59
- @enum = Class.new(Inum::Base) do
60
- define_enum :REDBULL, 0
61
- define_enum :MONSTER, 1
62
- define_enum :BURN, 2
36
+ # dup value.
37
+ expect{
38
+ Class.new(Inum::Base) {
39
+ define :REDBULL, 0
40
+ define :MONSTER, 0
41
+ }
42
+ }.to raise_error
43
+ end
44
+
45
+ it 'Autoincrement value when without default value.' do
46
+ enum = Class.new(Inum::Base) do
47
+ define :REDBULL
48
+ define :MONSTER
49
+ define :BURN
63
50
  end
64
- end
65
51
 
66
- it 'can not call new.' do
67
- expect{ @enum.new(1) }.to raise_error
52
+ expect(enum::REDBULL.value).to eq(0)
53
+ expect(enum::MONSTER.value).to eq(1)
54
+ expect(enum::BURN.value).to eq(2)
68
55
  end
69
56
 
70
- it 'DEFINED_ENUM is different instance.' do
71
- @enum2 = Class.new(Inum::Base) do
72
- define_enum :REDBULL, 0
73
- end
57
+ it 'Instances of enum are different each definition.' do
58
+ first = Class.new(Inum::Base){ define :REDBULL }
59
+ second = Class.new(Inum::Base){ define :MONSTER }
74
60
 
75
- defined_enum_inst = @enum.instance_variable_get(:@defined_enums)
76
- defined_enum_inst2 = @enum2.instance_variable_get(:@defined_enums)
61
+ first_enum_format = first.instance_variable_get(:@enum_format)
62
+ second_enum_format = second.instance_variable_get(:@enum_format)
77
63
 
78
- expect( defined_enum_inst.eql?(defined_enum_inst2) ).to be_false
79
- expect( defined_enum_inst.equal?(defined_enum_inst2) ).to be_false
80
- end
64
+ first_enums = first.instance_variable_get(:@enums)
65
+ second_enums = second.instance_variable_get(:@enums)
66
+
67
+ expect(first_enum_format.eql?(second_enum_format)).to be_false
68
+ expect(first_enum_format.equal?(second_enum_format)).to be_false
81
69
 
82
- it 'A enum instance is equal instance.' do
83
- expect( @enum::BURN.equal?(@enum::BURN )).to be_true
70
+ expect(first_enums.eql?(second_enums)).to be_false
71
+ expect(first_enums.equal?(second_enums)).to be_false
84
72
  end
73
+ end
85
74
 
86
- it '<=> return a correct value.' do
87
- expect( (@enum::REDBULL <=> 1) < 0 ).to be_true
88
- expect( (@enum::REDBULL <=> 0) == 0 ).to be_true
89
- expect( (@enum::MONSTER <=> 0) > 0 ).to be_true
90
75
 
91
- expect( @enum::MONSTER <=> 'a' ).to be_nil
76
+ describe 'Defined Inum::Base' do
77
+ before :each do
78
+ class Anime < Inum::Base
79
+ define :NYARUKO, 0
80
+ define :MUROMISAN, 1
81
+ define :NOURIN, 2
82
+ define :KMB, 4
83
+ end
92
84
  end
93
85
 
94
- it '+ return a correct Inum.' do
95
- expect(@enum::REDBULL + 1).to eq(@enum::MONSTER)
96
- expect(@enum::REDBULL + @enum::MONSTER).to eq(@enum::MONSTER)
86
+ after :each do
87
+ Object.class_eval{ remove_const :Anime }
97
88
  end
98
89
 
99
- it '- return a correct Inum.' do
100
- expect(@enum::BURN - 1).to eq(@enum::MONSTER)
101
- expect(@enum::BURN - @enum::MONSTER).to eq(@enum::MONSTER)
90
+ it 'Can not create instance.(Singleton)' do
91
+ expect{ Anime.new(:NICONICO, 2525) }.to raise_error
102
92
  end
103
93
 
104
- it 'Comparable module enable.' do
105
- expect(@enum::REDBULL.between?(0,1)).to be_true
94
+ it 'The instance of enum is equal.' do
95
+ expect(Anime::NYARUKO.equal?(Anime.parse!('NYARUKO'))).to be_true
106
96
  end
107
97
 
108
- it 'eql? return a correct result.' do
109
- expect( @enum::REDBULL.eql?(0) ).to be_true
110
- expect( @enum::REDBULL.eql?(1) ).to be_false
111
- end
98
+ it '<=> method return a correct value.' do
99
+ expect((Anime::MUROMISAN <=> 0) > 0 ).to be_true
100
+ expect((Anime::MUROMISAN <=> 1) == 0 ).to be_true
101
+ expect((Anime::MUROMISAN <=> 2) < 0 ).to be_true
112
102
 
113
- it 'to_i return integer.' do
114
- expect( @enum::REDBULL.to_i ).to eq(0)
103
+ expect(Anime::NYARUKO <=> 'Value can not compare.').to be_nil
115
104
  end
116
105
 
117
- it 'to_s return string.' do
118
- expect( @enum::MONSTER.to_s ).to eq('MONSTER')
106
+ it '+ method return a correct Inum.' do
107
+ expect(Anime::NYARUKO + 1).to eq(Anime::MUROMISAN)
119
108
  end
120
109
 
121
- it 'translate and to_t return localized string.' do
122
- expect( @enum::REDBULL.translate ).to eq('Good drink!')
123
- expect( @enum::REDBULL.to_t ).to eq('Good drink!')
110
+ it '- method return a correct Inum.' do
111
+ expect(Anime::NOURIN - 1).to eq(Anime::MUROMISAN)
124
112
  end
125
113
 
126
- it 'underscore and to_u return underscore string.' do
127
- expect( @enum::REDBULL.underscore ).to eq('redbull')
128
- expect( @enum::REDBULL.to_u ).to eq('redbull')
114
+ it 'Comparable module enable.' do
115
+ expect(Anime::MUROMISAN.between?(0,2)).to be_true
129
116
  end
130
117
 
131
- it 'each can execute block with enum' do
118
+ it 'each method can execute block with enum' do
132
119
  count = 0
133
120
  expect{
134
- @enum::each do |enum|
135
- expect(enum.instance_of?(@enum)).to be_true
121
+ Anime.each do |enum|
122
+ expect(enum.instance_of?(Anime)).to be_true
136
123
  count += 1
137
124
  end
138
- }.to change{count}.by(3)
125
+ }.to change{count}.by(Anime.length)
139
126
  end
140
127
 
141
128
  it 'Enumerable module enable.' do
142
- expect(@enum::count).to eq(3)
143
- expect(@enum::include?(@enum::REDBULL)).to be_true
129
+ expect(Anime.count).to eq(Anime.length)
130
+ expect(Anime.include?(Anime::NOURIN)).to be_true
131
+ end
132
+
133
+ it 'eql? method return a correct result.' do
134
+ expect(Anime::KMB.eql?(0) ).to be_false
135
+ expect(Anime::KMB.eql?(4) ).to be_true
144
136
  end
145
137
 
146
- it 'labels return Array<Symbol>.' do
147
- expect(@enum::labels.length).to eq(3)
148
- expect(@enum::labels.instance_of?(Array)).to be_true
149
- expect(@enum::labels[0].instance_of?(Symbol)).to be_true
138
+ it 'labels method return Array<Symbol>.' do
139
+ expect(Anime.labels.length).to eq(Anime.length)
140
+ expect(Anime.labels.instance_of?(Array)).to be_true
141
+ expect(Anime.labels[0].instance_of?(Symbol)).to be_true
150
142
  end
151
143
 
152
144
  it 'length return count of enum.' do
153
- expect(@enum::length).to eq(3)
145
+ expect(Anime.length).to eq(4)
154
146
  end
155
147
 
156
- it 'to_a return Array<Array<Symbol,Integer>>.' do
157
- expect(@enum::to_a.length).to eq(3)
158
- expect(@enum::to_a.instance_of?(Array)).to be_true
159
- expect(@enum::to_a[0].instance_of?(Array)).to be_true
160
- expect(@enum::to_a[0][0].instance_of?(Symbol)).to be_true
161
- expect(@enum::to_a[0][1].integer?).to be_true
148
+ context 'Parse method' do
149
+ it 'Parsable string.' do
150
+ expect(Anime.parse('NOURIN')).to eq(Anime::NOURIN)
151
+ end
162
152
 
163
- expect(@enum::to_a[0][0]).to eq(:REDBULL)
164
- (0..2).each{|i| expect(@enum::to_a[i][1]).to eq(i) }
165
- end
153
+ it 'Parsable integer.' do
154
+ expect(Anime.parse(1)).to eq(Anime::MUROMISAN)
155
+ end
166
156
 
167
- it 'to_h return Hash' do
168
- defined_enum_inst = @enum.instance_variable_get(:@defined_enums)
157
+ it 'Parsable symbol.' do
158
+ expect(Anime.parse(:KMB)).to eq(Anime::KMB)
159
+ end
160
+
161
+ it 'Parsable self instance.' do
162
+ expect(Anime.parse(Anime::NYARUKO)).to eq(Anime::NYARUKO)
163
+ end
169
164
 
170
- expect(@enum::to_h.instance_of?(Hash)).to be_true
165
+ it 'return nil for a unknown value.' do
166
+ expect(Anime.parse('Nothing') ).to eq(nil)
167
+ end
171
168
 
172
- expect(@enum::to_h.eql?(defined_enum_inst)).to be_true
173
- expect(@enum::to_h.equal?(defined_enum_inst)).to be_false
169
+ it 'parse! method raise exception for a unknown value.' do
170
+ expect{Anime.parse!('Nothing') }.to raise_error(NameError)
171
+ end
174
172
  end
175
173
 
176
- it 'parse return instance from string.' do
177
- expect( @enum::parse('REDBULL') ).to eq( @enum::REDBULL )
174
+ it 'to_a method return Array<Enum>.' do
175
+ enums = Anime.instance_variable_get(:@enums)
176
+
177
+ expect(Anime.to_a.eql?(enums)).to be_true
178
+ expect(Anime.to_a.equal?(enums)).to be_false
178
179
  end
179
180
 
180
- it 'parse return instance from integer.' do
181
- expect( @enum::parse(1) ).to eq( @enum::MONSTER )
181
+ it 'to_h method return Hash' do
182
+ enum_format = Anime.instance_variable_get(:@enum_format)
183
+
184
+ expect(Anime.to_h.eql?(enum_format)).to be_true
185
+ expect(Anime.to_h.equal?(enum_format)).to be_false
182
186
  end
183
187
 
184
- it 'parse return instance from symbol.' do
185
- expect( @enum::parse(:BURN) ).to eq( @enum::BURN )
188
+ it 'to_i and value methods return integer.' do
189
+ expect(Anime::KMB.to_i).to eq(4)
190
+ expect(Anime::KMB.value).to eq(4)
186
191
  end
187
192
 
188
- it 'parse return instance from self instance.' do
189
- expect( @enum::parse(@enum::REDBULL) ).to eq( @enum::REDBULL )
193
+ it 'to_s method return string.' do
194
+ expect(Anime::NOURIN.to_s ).to eq('NOURIN')
190
195
  end
191
196
 
192
- it 'parse return nil from a unknown value.' do
193
- expect( @enum::parse('hoge') ).to eq( nil )
197
+ it 'translate and t methods return localized string.' do
198
+ I18n.should_receive(:t).with('inum.anime.nourin').and_return('NO-RIN!')
199
+ I18n.should_receive(:t).with('inum.anime.kmb').and_return('KIRUMI-BEIBE-')
200
+
201
+ expect(Anime::NOURIN.t).to eq('NO-RIN!')
202
+ expect(Anime::KMB.translate).to eq('KIRUMI-BEIBE-')
194
203
  end
195
204
 
196
- it 'parse! raise exception from a unknown value.' do
197
- expect{ @enum::parse!('hoge') }.to raise_error( NameError )
205
+ it 'underscore method return underscore string.' do
206
+ expect(Anime::NYARUKO.underscore).to eq('nyaruko')
198
207
  end
199
208
 
200
- it 'values return Array<Integer>.' do
201
- expect(@enum::values.length).to eq(3)
202
- expect(@enum::values.instance_of?(Array)).to be_true
203
- expect(@enum::values[0].integer?).to be_true
209
+ it 'values method return Array<integer>.' do
210
+ expect(Anime.values.length).to eq(Anime.length)
211
+ expect(Anime.values.instance_of?(Array)).to be_true
212
+ expect(Anime.values[0].integer?).to be_true
204
213
  end
205
214
  end
206
215
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,29 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ require 'active_record'
3
4
  require 'inum'
4
- require 'inum/utils'
5
- require 'i18n'
6
5
 
7
6
  RSpec.configure do |config|
8
7
  config.mock_framework = :rspec
8
+ config.before(:all) {
9
+ Dir.mkdir('tmp') unless Dir.exists?('tmp')
10
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'tmp/rspec.sqlite')
11
+ I18n.enforce_available_locales = false
12
+ }
13
+ end
14
+
15
+ def create_temp_table(name, &block)
16
+ raise 'No Block given!' unless block_given?
17
+
18
+ before :all do
19
+ migration = ActiveRecord::Migration.new
20
+ migration.verbose = false
21
+ migration.create_table name, &block
22
+ end
23
+
24
+ after :all do
25
+ migration = ActiveRecord::Migration.new
26
+ migration.verbose = false
27
+ migration.drop_table name
28
+ end
9
29
  end
metadata CHANGED
@@ -1,83 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 3.0.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-12 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: yard
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
67
95
  - !ruby/object:Gem::Version
68
96
  version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: i18n
71
99
  requirement: !ruby/object:Gem::Requirement
72
100
  requirements:
73
- - - ~>
101
+ - - "~>"
74
102
  - !ruby/object:Gem::Version
75
103
  version: 0.6.5
76
104
  type: :runtime
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
- - - ~>
108
+ - - "~>"
81
109
  - !ruby/object:Gem::Version
82
110
  version: 0.6.5
83
111
  description: Inum(enumerated type of Integer) provide a java-Enum-like.
@@ -87,19 +115,20 @@ executables: []
87
115
  extensions: []
88
116
  extra_rdoc_files: []
89
117
  files:
90
- - .gitignore
118
+ - ".gitignore"
119
+ - ".travis.yml"
91
120
  - Gemfile
92
121
  - LICENSE.txt
93
122
  - README.md
94
123
  - Rakefile
95
124
  - inum.gemspec
96
125
  - lib/inum.rb
126
+ - lib/inum/active_record_mixin.rb
97
127
  - lib/inum/base.rb
98
- - lib/inum/define_check_method.rb
99
128
  - lib/inum/utils.rb
100
129
  - lib/inum/version.rb
130
+ - spec/inum/active_record_mixin_spec.rb
101
131
  - spec/inum/base_spec.rb
102
- - spec/inum/define_check_method_spec.rb
103
132
  - spec/inum/utils_spec.rb
104
133
  - spec/spec_helper.rb
105
134
  homepage: https://github.com/alfa-jpn/inum
@@ -112,23 +141,23 @@ require_paths:
112
141
  - lib
113
142
  required_ruby_version: !ruby/object:Gem::Requirement
114
143
  requirements:
115
- - - '>='
144
+ - - ">="
116
145
  - !ruby/object:Gem::Version
117
146
  version: '0'
118
147
  required_rubygems_version: !ruby/object:Gem::Requirement
119
148
  requirements:
120
- - - '>='
149
+ - - ">="
121
150
  - !ruby/object:Gem::Version
122
151
  version: '0'
123
152
  requirements: []
124
153
  rubyforge_project:
125
- rubygems_version: 2.0.3
154
+ rubygems_version: 2.2.0
126
155
  signing_key:
127
156
  specification_version: 4
128
157
  summary: Inum(enumerated type of Integer) provide a java-Enum-like.
129
158
  test_files:
159
+ - spec/inum/active_record_mixin_spec.rb
130
160
  - spec/inum/base_spec.rb
131
- - spec/inum/define_check_method_spec.rb
132
161
  - spec/inum/utils_spec.rb
133
162
  - spec/spec_helper.rb
134
163
  has_rdoc:
@@ -1,33 +0,0 @@
1
- module Inum
2
- # DefinedCheckMethod module should be extend.
3
- #
4
- # @example
5
- # class Fruit
6
- # extend Inum::DefineCheckMethod
7
- # define_check_method :type, FruitType
8
- #
9
- # attr_accessor :type
10
- # end
11
- #
12
- module DefineCheckMethod
13
- require 'inum/utils'
14
-
15
- # Define compare method in class.
16
- #
17
- # @param variable_name [String] name of variable.
18
- # @param enum_class [Inum::Base] class of extended Enum::EnumBase.
19
- # @param prefix [String] prefix of define method.
20
- def define_check_method(variable_name, enum_class, prefix = nil)
21
- prefix = (prefix.nil?)? '' : "#{prefix}_"
22
-
23
- self.class_eval do
24
- enum_class::each do |enum|
25
- define_method("#{prefix}#{enum.to_u}?") do
26
- enum.eql?(send(variable_name))
27
- end
28
- end
29
- end
30
- end
31
-
32
- end
33
- end
@@ -1,82 +0,0 @@
1
- require 'rspec'
2
- require 'spec_helper'
3
-
4
- describe Inum::DefineCheckMethod do
5
-
6
- context 'create class after call define_check_method' do
7
- before(:each) do
8
- enum = @enum = Class.new(Inum::Base) do
9
- define_enum :REDBULL, 0
10
- define_enum :MONSTER, 1
11
- define_enum :BURN, 2
12
- end
13
-
14
- @mixin = Class.new do
15
- attr_accessor :drink
16
-
17
- extend Inum::DefineCheckMethod
18
- self.define_check_method(:drink, enum)
19
- end
20
- end
21
-
22
- it 'defined comparison method.' do
23
- expect(@mixin.method_defined?(:redbull?)).to be_true
24
- expect(@mixin.method_defined?(:monster?)).to be_true
25
- expect(@mixin.method_defined?(:burn?)).to be_true
26
- end
27
-
28
- context 'create instance the class,' do
29
- before(:each) do
30
- @inst = @mixin.new
31
- end
32
-
33
- it 'can compare integer.' do
34
- @inst.drink = 0
35
- expect(@inst.redbull?).to be_true
36
- expect(@inst.monster?).to be_false
37
- end
38
-
39
- it 'can compare string.' do
40
- @inst.drink = 'REDBULL'
41
- expect(@inst.redbull?).to be_true
42
- expect(@inst.monster?).to be_false
43
- end
44
-
45
- it 'can compare symbol.' do
46
- @inst.drink = :REDBULL
47
- expect(@inst.redbull?).to be_true
48
- expect(@inst.monster?).to be_false
49
- end
50
-
51
- it 'can compare self instance.' do
52
- @inst.drink = @enum::REDBULL
53
- expect(@inst.redbull?).to be_true
54
- expect(@inst.monster?).to be_false
55
- end
56
-
57
- end
58
- end
59
-
60
- context 'create class after call define_check_method with prefix.' do
61
- before(:each) do
62
- enum = @enum = Class.new(Inum::Base) do
63
- define_enum :REDBULL, 0
64
- define_enum :MONSTER, 1
65
- define_enum :BURN, 2
66
- end
67
-
68
- @mixin = Class.new do
69
- attr_accessor :drink
70
-
71
- extend Inum::DefineCheckMethod
72
- self.define_check_method(:drink, enum, 'drink_type')
73
- end
74
- end
75
-
76
- it 'defined comparison method.' do
77
- expect(@mixin.method_defined?(:drink_type_redbull?)).to be_true
78
- expect(@mixin.method_defined?(:drink_type_monster?)).to be_true
79
- expect(@mixin.method_defined?(:drink_type_burn?)).to be_true
80
- end
81
- end
82
- end