enumerize 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +2 -2
- data/README.md +24 -2
- data/lib/enumerize.rb +2 -2
- data/lib/enumerize/integrations/active_record.rb +0 -18
- data/lib/enumerize/integrations/basic.rb +12 -4
- data/lib/enumerize/value.rb +7 -0
- data/lib/enumerize/version.rb +1 -1
- data/test/{active_record_test.rb → activerecord_test.rb} +20 -5
- data/test/mongoid_test.rb +55 -0
- data/test/test_helper.rb +2 -0
- data/test/value_test.rb +28 -2
- metadata +11 -12
- data/test/active_record/migrate/1_create_tables.rb +0 -8
- data/test/active_record/models.rb +0 -7
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2012
|
1
|
+
Copyright (c) 2012 Twinslash
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Enumerize [](http://travis-ci.org/twinslash/enumerize)
|
1
|
+
# Enumerize [](http://travis-ci.org/twinslash/enumerize) [](https://gemnasium.com/twinslash/enumerize)
|
2
2
|
|
3
3
|
Enumerated attributes with I18n and ActiveRecord support
|
4
4
|
|
@@ -40,6 +40,18 @@ class User < ActiveRecord::Base
|
|
40
40
|
end
|
41
41
|
```
|
42
42
|
|
43
|
+
Mongoid:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class User
|
47
|
+
include Mongoid::Document
|
48
|
+
include Enumerize
|
49
|
+
|
50
|
+
field :role
|
51
|
+
enumerize :role, :in => [:user, :admin], :default => :user
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
43
55
|
I18n:
|
44
56
|
|
45
57
|
```ruby
|
@@ -64,7 +76,7 @@ en:
|
|
64
76
|
get attribute value:
|
65
77
|
|
66
78
|
```ruby
|
67
|
-
|
79
|
+
@user.sex_text # or @user.sex.text
|
68
80
|
```
|
69
81
|
|
70
82
|
use it with forms:
|
@@ -75,6 +87,16 @@ use it with forms:
|
|
75
87
|
<% end %>
|
76
88
|
```
|
77
89
|
|
90
|
+
Boolean methods:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
user.sex = :male
|
94
|
+
user.sex.male? #=> true
|
95
|
+
user.sex.female? #=> false
|
96
|
+
```
|
97
|
+
|
98
|
+
|
99
|
+
|
78
100
|
## Contributing
|
79
101
|
|
80
102
|
1. Fork it
|
data/lib/enumerize.rb
CHANGED
@@ -8,11 +8,11 @@ module Enumerize
|
|
8
8
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
11
|
+
include Integrations::Basic
|
12
|
+
|
11
13
|
included do
|
12
14
|
if defined?(ActiveRecord::Base) && self < ActiveRecord::Base
|
13
15
|
include Integrations::ActiveRecord
|
14
|
-
else
|
15
|
-
include Integrations::Basic
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -4,24 +4,6 @@ module Enumerize
|
|
4
4
|
module Integrations
|
5
5
|
module ActiveRecord
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
8
|
-
include Integrations::Basic
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
private
|
12
|
-
|
13
|
-
def _define_enumerize_attribute(mod, attr)
|
14
|
-
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
|
-
def #{attr.name}
|
16
|
-
self.class.#{attr.name}.find_value(super)
|
17
|
-
end
|
18
|
-
|
19
|
-
def #{attr.name}=(new_value)
|
20
|
-
super self.class.#{attr.name}.find_value(new_value).to_s
|
21
|
-
end
|
22
|
-
RUBY
|
23
|
-
end
|
24
|
-
end
|
25
7
|
end
|
26
8
|
end
|
27
9
|
end
|
@@ -37,15 +37,23 @@ module Enumerize
|
|
37
37
|
def _define_enumerize_attribute(mod, attr)
|
38
38
|
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
39
39
|
def #{attr.name}
|
40
|
-
if defined?(
|
41
|
-
self.class.#{attr.name}.find_value(
|
40
|
+
if defined?(super)
|
41
|
+
self.class.#{attr.name}.find_value(super)
|
42
42
|
else
|
43
|
-
@#{attr.name}
|
43
|
+
if defined?(@#{attr.name})
|
44
|
+
self.class.#{attr.name}.find_value(@#{attr.name})
|
45
|
+
else
|
46
|
+
@#{attr.name} = nil
|
47
|
+
end
|
44
48
|
end
|
45
49
|
end
|
46
50
|
|
47
51
|
def #{attr.name}=(new_value)
|
48
|
-
|
52
|
+
if defined?(super)
|
53
|
+
super self.class.#{attr.name}.find_value(new_value).to_s
|
54
|
+
else
|
55
|
+
@#{attr.name} = self.class.#{attr.name}.find_value(new_value).to_s
|
56
|
+
end
|
49
57
|
end
|
50
58
|
RUBY
|
51
59
|
end
|
data/lib/enumerize/value.rb
CHANGED
@@ -15,5 +15,12 @@ module Enumerize
|
|
15
15
|
i18n_keys.map! { |k| :"enumerize.#{k}#{@attr.name}.#{self}" }
|
16
16
|
I18n.t(i18n_keys.shift, :default => i18n_keys)
|
17
17
|
end
|
18
|
+
|
19
|
+
def method_missing(method, *args)
|
20
|
+
value = method.to_s.gsub(/\?\Z/, '')
|
21
|
+
super unless @attr.values.include?(value)
|
22
|
+
raise ArgumentError if args.any?
|
23
|
+
value == self
|
24
|
+
end
|
18
25
|
end
|
19
26
|
end
|
data/lib/enumerize/version.rb
CHANGED
@@ -2,11 +2,26 @@ require 'test_helper'
|
|
2
2
|
require 'active_record'
|
3
3
|
require 'logger'
|
4
4
|
|
5
|
-
|
6
|
-
ActiveRecord::
|
7
|
-
ActiveRecord::Base.
|
8
|
-
ActiveRecord::
|
9
|
-
|
5
|
+
silence_warnings do
|
6
|
+
ActiveRecord::Migration.verbose = false
|
7
|
+
ActiveRecord::Base.logger = Logger.new(nil)
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.connection.instance_eval do
|
12
|
+
create_table :users do |t|
|
13
|
+
t.string :sex
|
14
|
+
t.string :role
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
include Enumerize
|
20
|
+
|
21
|
+
enumerize :sex, :in => [:male, :female]
|
22
|
+
|
23
|
+
enumerize :role, :in => [:user, :admin], :default => :user
|
24
|
+
end
|
10
25
|
|
11
26
|
describe Enumerize::Integrations::ActiveRecord do
|
12
27
|
it 'sets nil if invalid value is passed' do
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
silence_warnings do
|
4
|
+
require 'mongoid'
|
5
|
+
end
|
6
|
+
|
7
|
+
Mongoid.configure do |config|
|
8
|
+
config.master = Mongo::Connection.new('127.0.0.1', 27017).db('enumerize-test-suite')
|
9
|
+
config.use_utc = true
|
10
|
+
config.include_root_in_json = true
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Enumerize do
|
14
|
+
class MongoidUser
|
15
|
+
include Mongoid::Document
|
16
|
+
include Enumerize
|
17
|
+
|
18
|
+
field :sex
|
19
|
+
field :role
|
20
|
+
enumerize :sex, :in => %w[male female]
|
21
|
+
enumerize :role, :in => %w[admine user], :default => 'user'
|
22
|
+
end
|
23
|
+
|
24
|
+
before { $VERBOSE = nil }
|
25
|
+
after { $VERBOSE = true }
|
26
|
+
|
27
|
+
let(:model) { MongoidUser }
|
28
|
+
|
29
|
+
it 'sets nil if invalid value is passed' do
|
30
|
+
user = model.new
|
31
|
+
user.sex = :invalid
|
32
|
+
user.sex.must_equal nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'saves value' do
|
36
|
+
model.delete_all
|
37
|
+
user = model.new
|
38
|
+
user.sex = :female
|
39
|
+
user.save!
|
40
|
+
user.sex.must_equal 'female'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'loads value' do
|
44
|
+
model.delete_all
|
45
|
+
I18n.backend.store_translations(:en, :enumerize => {:sex => {:male => 'Male'}})
|
46
|
+
model.create!(:sex => :male)
|
47
|
+
user = model.first
|
48
|
+
user.sex.must_equal 'male'
|
49
|
+
user.sex_text.must_equal 'Male'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'has default value' do
|
53
|
+
model.new.role.must_equal 'user'
|
54
|
+
end
|
55
|
+
end
|
data/test/test_helper.rb
CHANGED
data/test/value_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require 'yaml'
|
3
2
|
|
4
3
|
describe Enumerize::Value do
|
5
|
-
let(:
|
4
|
+
let(:attr) { Object.new }
|
5
|
+
let(:value) { Enumerize::Value.new(attr, 'test_value') }
|
6
6
|
|
7
7
|
it 'is a string' do
|
8
8
|
value.must_be_kind_of String
|
@@ -15,4 +15,30 @@ describe Enumerize::Value do
|
|
15
15
|
it 'is frozen' do
|
16
16
|
value.must_be :frozen?
|
17
17
|
end
|
18
|
+
|
19
|
+
describe 'boolean methods comparison' do
|
20
|
+
before do
|
21
|
+
attr.stubs(:values).returns([value, Enumerize::Value.new(attr, 'other_value')])
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns true if value equals method' do
|
25
|
+
value.test_value?.must_equal true
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns false if value does not equal method' do
|
29
|
+
value.other_value?.must_equal false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'raises NoMethodError if there are no values like boolean method' do
|
33
|
+
proc {
|
34
|
+
value.some_method?
|
35
|
+
}.must_raise NoMethodError
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'raises ArgumentError if arguments are passed' do
|
39
|
+
proc {
|
40
|
+
value.other_value?('<3')
|
41
|
+
}.must_raise ArgumentError
|
42
|
+
end
|
43
|
+
end
|
18
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &23049480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.1.3
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *23049480
|
25
25
|
description: Enumerated attributes with I18n and ActiveRecord support
|
26
26
|
email: info@twinslash.com
|
27
27
|
executables: []
|
@@ -30,6 +30,7 @@ extra_rdoc_files: []
|
|
30
30
|
files:
|
31
31
|
- .gitignore
|
32
32
|
- .travis.yml
|
33
|
+
- CHANGELOG.md
|
33
34
|
- Gemfile
|
34
35
|
- MIT-LICENSE
|
35
36
|
- README.md
|
@@ -42,11 +43,10 @@ files:
|
|
42
43
|
- lib/enumerize/integrations/basic.rb
|
43
44
|
- lib/enumerize/value.rb
|
44
45
|
- lib/enumerize/version.rb
|
45
|
-
- test/
|
46
|
-
- test/active_record/models.rb
|
47
|
-
- test/active_record_test.rb
|
46
|
+
- test/activerecord_test.rb
|
48
47
|
- test/attribute_test.rb
|
49
48
|
- test/basic_test.rb
|
49
|
+
- test/mongoid_test.rb
|
50
50
|
- test/test_helper.rb
|
51
51
|
- test/value_test.rb
|
52
52
|
homepage: https://github.com/twinslash/enumerize
|
@@ -63,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash:
|
66
|
+
hash: 1283897538830316283
|
67
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
68
|
none: false
|
69
69
|
requirements:
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
segments:
|
74
74
|
- 0
|
75
|
-
hash:
|
75
|
+
hash: 1283897538830316283
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
78
78
|
rubygems_version: 1.8.15
|
@@ -80,10 +80,9 @@ signing_key:
|
|
80
80
|
specification_version: 3
|
81
81
|
summary: Enumerated attributes with I18n and ActiveRecord support
|
82
82
|
test_files:
|
83
|
-
- test/
|
84
|
-
- test/active_record/models.rb
|
85
|
-
- test/active_record_test.rb
|
83
|
+
- test/activerecord_test.rb
|
86
84
|
- test/attribute_test.rb
|
87
85
|
- test/basic_test.rb
|
86
|
+
- test/mongoid_test.rb
|
88
87
|
- test/test_helper.rb
|
89
88
|
- test/value_test.rb
|