active_enum 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README.rdoc +14 -0
- data/lib/active_enum/base.rb +5 -4
- data/lib/active_enum/extensions.rb +7 -8
- data/lib/active_enum/storage/i18n_store.rb +1 -1
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/storage/i18n_store_spec.rb +8 -4
- metadata +6 -6
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -108,6 +108,20 @@ Multiple attributes with same enum
|
|
108
108
|
enumerate :to, :from, :with => Sex
|
109
109
|
end
|
110
110
|
|
111
|
+
|
112
|
+
=== Validations
|
113
|
+
|
114
|
+
You can use an enum in a validation of inclusion like so
|
115
|
+
|
116
|
+
class User < ActiveRecord::Base
|
117
|
+
enumerate :sex, :with => Sex
|
118
|
+
|
119
|
+
validates_inclusion_of :sex, :in => Sex
|
120
|
+
end
|
121
|
+
|
122
|
+
This works because an enum class responds to #include?().
|
123
|
+
|
124
|
+
|
111
125
|
=== Attribute value lookup
|
112
126
|
|
113
127
|
Access the enum values and the enum class using the attribute method with a symbol for the enum component you want
|
data/lib/active_enum/base.rb
CHANGED
@@ -54,7 +54,7 @@ module ActiveEnum
|
|
54
54
|
|
55
55
|
# Access id or name value. Pass an id number to retrieve the name or
|
56
56
|
# a symbol or string to retrieve the matching id.
|
57
|
-
def
|
57
|
+
def get(index)
|
58
58
|
if index.is_a?(Fixnum)
|
59
59
|
row = store.get_by_id(index)
|
60
60
|
row[1] if row
|
@@ -63,9 +63,10 @@ module ActiveEnum
|
|
63
63
|
row[0] if row
|
64
64
|
end
|
65
65
|
end
|
66
|
+
alias_method :[], :get
|
66
67
|
|
67
68
|
def include?(value)
|
68
|
-
!
|
69
|
+
!get(value).nil?
|
69
70
|
end
|
70
71
|
|
71
72
|
# Access any meta data defined for a given id or name. Returns a hash.
|
@@ -81,11 +82,11 @@ module ActiveEnum
|
|
81
82
|
private
|
82
83
|
|
83
84
|
def id_and_name_and_meta(hash)
|
84
|
-
if hash.has_key?(:
|
85
|
+
if hash.has_key?(:name)
|
85
86
|
id = hash.delete(:id) || next_id
|
86
87
|
name = hash.delete(:name)
|
87
88
|
meta = hash
|
88
|
-
return id, name, (meta.
|
89
|
+
return id, name, (meta.empty? ? nil : meta)
|
89
90
|
elsif hash.keys.first.is_a?(Fixnum)
|
90
91
|
return *Array(hash).first
|
91
92
|
else
|
@@ -85,6 +85,8 @@ module ActiveEnum
|
|
85
85
|
|
86
86
|
enum = self.class.active_enum_for(:#{attribute})
|
87
87
|
case arg
|
88
|
+
when nil
|
89
|
+
#{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
|
88
90
|
when :id
|
89
91
|
value if enum[value]
|
90
92
|
when :name
|
@@ -93,8 +95,6 @@ module ActiveEnum
|
|
93
95
|
enum
|
94
96
|
when Symbol
|
95
97
|
(enum.meta(value) || {})[arg]
|
96
|
-
else
|
97
|
-
#{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
|
98
98
|
end
|
99
99
|
end
|
100
100
|
DEF
|
@@ -110,10 +110,9 @@ module ActiveEnum
|
|
110
110
|
class_eval <<-DEF
|
111
111
|
def #{attribute}=(arg)
|
112
112
|
if arg.is_a?(Symbol)
|
113
|
-
|
114
|
-
super(value)
|
113
|
+
super self.class.active_enum_for(:#{attribute})[arg]
|
115
114
|
else
|
116
|
-
super
|
115
|
+
super arg
|
117
116
|
end
|
118
117
|
end
|
119
118
|
DEF
|
@@ -126,9 +125,9 @@ module ActiveEnum
|
|
126
125
|
#
|
127
126
|
def define_active_enum_question_method(attribute)
|
128
127
|
class_eval <<-DEF
|
129
|
-
def #{attribute}?(
|
130
|
-
if
|
131
|
-
|
128
|
+
def #{attribute}?(arg=nil)
|
129
|
+
if arg
|
130
|
+
self.#{attribute} == self.class.active_enum_for(:#{attribute})[arg]
|
132
131
|
else
|
133
132
|
super()
|
134
133
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -78,18 +78,15 @@ describe ActiveEnum::Storage::I18nStore do
|
|
78
78
|
describe "#get_by_id" do
|
79
79
|
before do
|
80
80
|
I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
|
81
|
+
I18n.locale = :en
|
81
82
|
end
|
82
83
|
|
83
84
|
it 'should return the value for a given id' do
|
84
|
-
I18n.locale = :en
|
85
|
-
|
86
85
|
store.set 1, 'test'
|
87
86
|
store.get_by_id(1).should == [1, 'Testing']
|
88
87
|
end
|
89
88
|
|
90
89
|
it 'should return the value with meta for a given id' do
|
91
|
-
I18n.locale = :en
|
92
|
-
|
93
90
|
store.set 1, 'test', :description => 'meta'
|
94
91
|
store.get_by_id(1).should == [1, 'Testing', { :description => 'meta' }]
|
95
92
|
end
|
@@ -97,11 +94,18 @@ describe ActiveEnum::Storage::I18nStore do
|
|
97
94
|
it 'should return nil when id not found' do
|
98
95
|
store.get_by_id(1).should be_nil
|
99
96
|
end
|
97
|
+
|
98
|
+
it 'should return key when translation missing' do
|
99
|
+
I18n.locale = :ja
|
100
|
+
store.set 1, 'test'
|
101
|
+
store.get_by_id(1).should == [1, 'test']
|
102
|
+
end
|
100
103
|
end
|
101
104
|
|
102
105
|
describe "#get_by_name" do
|
103
106
|
before do
|
104
107
|
I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
|
108
|
+
I18n.locale = :en
|
105
109
|
end
|
106
110
|
|
107
111
|
it 'should return the value for a given name' do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 45
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 11
|
10
|
+
version: 0.9.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
@@ -15,12 +15,10 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-08-18 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name: activesupport
|
23
|
-
prerelease: false
|
24
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
23
|
none: false
|
26
24
|
requirements:
|
@@ -31,6 +29,8 @@ dependencies:
|
|
31
29
|
- 3
|
32
30
|
- 0
|
33
31
|
version: "3.0"
|
32
|
+
name: activesupport
|
33
|
+
prerelease: false
|
34
34
|
type: :runtime
|
35
35
|
version_requirements: *id001
|
36
36
|
description: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|