enumerize 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -2
- data/MIT-LICENSE +1 -1
- data/README.md +39 -28
- data/enumerize.gemspec +2 -2
- data/gemfiles/Gemfile-ar-3.1.x +1 -2
- data/lib/enumerize/attribute.rb +3 -2
- data/lib/enumerize/base.rb +1 -1
- data/lib/enumerize/value.rb +8 -3
- data/lib/enumerize/version.rb +1 -1
- data/test/attribute_map_test.rb +3 -2
- data/test/attribute_test.rb +15 -0
- data/test/base_test.rb +12 -0
- data/test/test_helper.rb +0 -1
- data/test/value_test.rb +2 -2
- metadata +6 -6
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Enumerize [![TravisCI](https://secure.travis-ci.org/
|
1
|
+
# Enumerize [![TravisCI](https://secure.travis-ci.org/brainspec/enumerize.png?branch=master)](http://travis-ci.org/brainspec/enumerize) [![Gemnasium](https://gemnasium.com/brainspec/enumerize.png)](https://gemnasium.com/brainspec/enumerize)
|
2
2
|
|
3
3
|
Enumerated attributes with I18n and ActiveRecord/Mongoid support
|
4
4
|
|
@@ -93,6 +93,42 @@ use it with forms:
|
|
93
93
|
<% end %>
|
94
94
|
```
|
95
95
|
|
96
|
+
Boolean methods:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
user.sex = :male
|
100
|
+
user.sex.male? #=> true
|
101
|
+
user.sex.female? #=> false
|
102
|
+
```
|
103
|
+
|
104
|
+
To make some attributes shared across different classes it's possible to define them in a separate module and then include it into classes:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
module PersonEnumerations
|
108
|
+
include Enumerize
|
109
|
+
|
110
|
+
enumerize :sex, :in => %w[male female]
|
111
|
+
end
|
112
|
+
|
113
|
+
class Person
|
114
|
+
include PersonEnumerations
|
115
|
+
end
|
116
|
+
|
117
|
+
class User
|
118
|
+
include PersonEnumerations
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
It's also possible to store enumerized attribute value using custom values (e.g. integers). You can pass a hash as `:in` option to achieve this:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class User < ActiveRecord::Base
|
126
|
+
enumerize :role, :in => {:user => 1, :admin => 2}
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
### SimpleForm
|
131
|
+
|
96
132
|
If you are using SimpleForm gem you don't need to specify input type (`:select` by default) and collection:
|
97
133
|
|
98
134
|
```erb
|
@@ -109,6 +145,8 @@ and if you want it as radio buttons:
|
|
109
145
|
<% end %>
|
110
146
|
```
|
111
147
|
|
148
|
+
### Formtastic
|
149
|
+
|
112
150
|
If you are using Formtastic gem you also don't need to specify input type (`:select` by default) and collection:
|
113
151
|
|
114
152
|
```erb
|
@@ -125,33 +163,6 @@ and if you want it as radio buttons:
|
|
125
163
|
<% end %>
|
126
164
|
```
|
127
165
|
|
128
|
-
Boolean methods:
|
129
|
-
|
130
|
-
```ruby
|
131
|
-
user.sex = :male
|
132
|
-
user.sex.male? #=> true
|
133
|
-
user.sex.female? #=> false
|
134
|
-
```
|
135
|
-
|
136
|
-
To make some attributes shared across different classes it's possible to define them in a separate module and then include it into classes:
|
137
|
-
|
138
|
-
```ruby
|
139
|
-
module PersonEnumerations
|
140
|
-
include Enumerize
|
141
|
-
|
142
|
-
enumerize :sex, :in => %w[male female]
|
143
|
-
end
|
144
|
-
|
145
|
-
class Person
|
146
|
-
include PersonEnumerations
|
147
|
-
end
|
148
|
-
|
149
|
-
class User
|
150
|
-
include PersonEnumerations
|
151
|
-
end
|
152
|
-
```
|
153
|
-
|
154
|
-
|
155
166
|
## Contributing
|
156
167
|
|
157
168
|
1. Fork it
|
data/enumerize.gemspec
CHANGED
@@ -3,10 +3,10 @@ require File.expand_path('../lib/enumerize/version', __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Sergey Nartimov"]
|
6
|
-
gem.email = "
|
6
|
+
gem.email = "team@brainspec.com"
|
7
7
|
gem.description = %q{Enumerated attributes with I18n and ActiveRecord/Mongoid support}
|
8
8
|
gem.summary = %q{Enumerated attributes with I18n and ActiveRecord/Mongoid support}
|
9
|
-
gem.homepage = "https://github.com/
|
9
|
+
gem.homepage = "https://github.com/brainspec/enumerize"
|
10
10
|
|
11
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
data/gemfiles/Gemfile-ar-3.1.x
CHANGED
data/lib/enumerize/attribute.rb
CHANGED
@@ -7,8 +7,9 @@ module Enumerize
|
|
7
7
|
|
8
8
|
@klass = klass
|
9
9
|
@name = name.to_sym
|
10
|
-
@values = Array(options[:in]).map { |v| Value.new(self, v) }
|
11
|
-
@value_hash = Hash[@values.map { |v| [v.to_s, v] }]
|
10
|
+
@values = Array(options[:in]).map { |v| Value.new(self, *v) }
|
11
|
+
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
|
12
|
+
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
|
12
13
|
|
13
14
|
if options[:default]
|
14
15
|
@default_value = find_value(options[:default])
|
data/lib/enumerize/base.rb
CHANGED
@@ -75,7 +75,7 @@ module Enumerize
|
|
75
75
|
_enumerized_values_for_validation[:#{attr.name}] = new_value.nil? ? nil : new_value.to_s
|
76
76
|
|
77
77
|
allowed_value_or_nil = self.class.enumerized_attributes[:#{attr.name}].find_value(new_value)
|
78
|
-
allowed_value_or_nil = allowed_value_or_nil.
|
78
|
+
allowed_value_or_nil = allowed_value_or_nil.value unless allowed_value_or_nil.nil?
|
79
79
|
|
80
80
|
if defined?(super)
|
81
81
|
super allowed_value_or_nil
|
data/lib/enumerize/value.rb
CHANGED
@@ -2,10 +2,15 @@ require 'i18n'
|
|
2
2
|
|
3
3
|
module Enumerize
|
4
4
|
class Value < String
|
5
|
-
def initialize(attr, value)
|
6
|
-
@attr
|
5
|
+
def initialize(attr, name, value=nil)
|
6
|
+
@attr = attr
|
7
|
+
@value = value || name.to_s
|
7
8
|
|
8
|
-
super(
|
9
|
+
super(name.to_s)
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
@value
|
9
14
|
end
|
10
15
|
|
11
16
|
def text
|
data/lib/enumerize/version.rb
CHANGED
data/test/attribute_map_test.rb
CHANGED
@@ -50,10 +50,11 @@ module Enumerize
|
|
50
50
|
|
51
51
|
it 'updates dependants' do
|
52
52
|
attr = make_attr(:a)
|
53
|
-
dependant =
|
54
|
-
dependant.
|
53
|
+
dependant = MiniTest::Mock.new
|
54
|
+
dependant.expect(:<<, nil, [attr])
|
55
55
|
subject.add_dependant dependant
|
56
56
|
subject << attr
|
57
|
+
dependant.verify
|
57
58
|
end
|
58
59
|
|
59
60
|
it 'adds attrs to dependant' do
|
data/test/attribute_test.rb
CHANGED
@@ -25,4 +25,19 @@ describe Enumerize::Attribute do
|
|
25
25
|
attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
|
26
26
|
end
|
27
27
|
end
|
28
|
+
|
29
|
+
describe 'values hash' do
|
30
|
+
before do
|
31
|
+
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns hash keys as values' do
|
35
|
+
attr.values.must_equal %w[a b]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'finds values by hash values' do
|
39
|
+
attr.find_value(1).must_equal 'a'
|
40
|
+
attr.find_value(2).must_equal 'b'
|
41
|
+
end
|
42
|
+
end
|
28
43
|
end
|
data/test/base_test.rb
CHANGED
@@ -167,4 +167,16 @@ describe Enumerize::Base do
|
|
167
167
|
object.foo.must_equal 'test'
|
168
168
|
object.attributes.must_equal(:foo => 'test')
|
169
169
|
end
|
170
|
+
|
171
|
+
it 'returns stores hash values' do
|
172
|
+
klass.enumerize(:foo, :in => {:a => 1, :b => 2})
|
173
|
+
|
174
|
+
object.foo = :a
|
175
|
+
object.instance_variable_get(:@foo).must_equal 1
|
176
|
+
object.foo.must_equal 'a'
|
177
|
+
|
178
|
+
object.foo = :b
|
179
|
+
object.instance_variable_get(:@foo).must_equal 2
|
180
|
+
object.foo.must_equal 'b'
|
181
|
+
end
|
170
182
|
end
|
data/test/test_helper.rb
CHANGED
data/test/value_test.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
describe Enumerize::Value do
|
4
|
-
let(:attr) {
|
4
|
+
let(:attr) { Struct.new(:values).new([]) }
|
5
5
|
let(:value) { Enumerize::Value.new(attr, 'test_value') }
|
6
6
|
|
7
7
|
it 'is a string' do
|
@@ -14,7 +14,7 @@ describe Enumerize::Value do
|
|
14
14
|
|
15
15
|
describe 'boolean methods comparison' do
|
16
16
|
before do
|
17
|
-
attr.
|
17
|
+
attr.values = [value, Enumerize::Value.new(attr, 'other_value')]
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'returns true if value equals method' do
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.1.3
|
30
30
|
description: Enumerated attributes with I18n and ActiveRecord/Mongoid support
|
31
|
-
email:
|
31
|
+
email: team@brainspec.com
|
32
32
|
executables: []
|
33
33
|
extensions: []
|
34
34
|
extra_rdoc_files: []
|
@@ -64,7 +64,7 @@ files:
|
|
64
64
|
- test/support/view_test_helper.rb
|
65
65
|
- test/test_helper.rb
|
66
66
|
- test/value_test.rb
|
67
|
-
homepage: https://github.com/
|
67
|
+
homepage: https://github.com/brainspec/enumerize
|
68
68
|
licenses: []
|
69
69
|
post_install_message:
|
70
70
|
rdoc_options: []
|
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
segments:
|
80
80
|
- 0
|
81
|
-
hash:
|
81
|
+
hash: -3692164922268239392
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: -3692164922268239392
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
93
|
rubygems_version: 1.8.23
|