has_enumeration 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -2
- data/Gemfile.lock +24 -37
- data/HISTORY.txt +3 -0
- data/README.md +2 -1
- data/Rakefile +2 -2
- data/TODO.md +8 -0
- data/VERSION +1 -1
- data/features/step_definitions/has_enumeration_steps.rb +1 -1
- data/lib/has_enumeration/arel/table_extensions.rb +10 -12
- data/lib/has_enumeration/arel/table_extensions_arel_one.rb +70 -0
- data/lib/has_enumeration.rb +7 -2
- metadata +117 -10
data/Gemfile
CHANGED
@@ -3,11 +3,11 @@ source 'http://rubygems.org'
|
|
3
3
|
if ENV['AR_VERSION'] == '2.3'
|
4
4
|
gem 'activerecord', '~> 2.3.8'
|
5
5
|
else
|
6
|
-
gem 'activerecord', '~> 3.0.
|
6
|
+
gem 'activerecord', '~> 3.0.3'
|
7
7
|
gem 'meta_where'
|
8
8
|
end
|
9
9
|
|
10
|
-
gem 'rspec', '~> 2.0
|
10
|
+
gem 'rspec', '~> 2.1.0'
|
11
11
|
gem 'cucumber', '~> 0.9.3'
|
12
12
|
|
13
13
|
platforms :ruby do
|
data/Gemfile.lock
CHANGED
@@ -1,24 +1,19 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.0.
|
5
|
-
activesupport (= 3.0.
|
4
|
+
activemodel (3.0.3)
|
5
|
+
activesupport (= 3.0.3)
|
6
6
|
builder (~> 2.1.2)
|
7
|
-
i18n (~> 0.4
|
8
|
-
activerecord (3.0.
|
9
|
-
activemodel (= 3.0.
|
10
|
-
activesupport (= 3.0.
|
11
|
-
arel (~>
|
7
|
+
i18n (~> 0.4)
|
8
|
+
activerecord (3.0.3)
|
9
|
+
activemodel (= 3.0.3)
|
10
|
+
activesupport (= 3.0.3)
|
11
|
+
arel (~> 2.0.2)
|
12
12
|
tzinfo (~> 0.3.23)
|
13
|
-
|
14
|
-
|
15
|
-
activerecord-jdbc-adapter (= 0.9.7)
|
16
|
-
jdbc-sqlite3 (>= 3.6.3.054)
|
17
|
-
activesupport (3.0.1)
|
18
|
-
arel (1.0.1)
|
19
|
-
activesupport (~> 3.0.0)
|
13
|
+
activesupport (3.0.3)
|
14
|
+
arel (2.0.4)
|
20
15
|
builder (2.1.2)
|
21
|
-
cucumber (0.9.
|
16
|
+
cucumber (0.9.4)
|
22
17
|
builder (~> 2.1.2)
|
23
18
|
diff-lcs (~> 1.1.2)
|
24
19
|
gherkin (~> 2.2.9)
|
@@ -28,40 +23,32 @@ GEM
|
|
28
23
|
gherkin (2.2.9)
|
29
24
|
json (~> 1.4.6)
|
30
25
|
term-ansicolor (~> 1.0.5)
|
31
|
-
gherkin (2.2.9-java)
|
32
|
-
json (~> 1.4.6)
|
33
|
-
term-ansicolor (~> 1.0.5)
|
34
26
|
i18n (0.4.2)
|
35
|
-
jdbc-sqlite3 (3.6.14.2.056-java)
|
36
27
|
json (1.4.6)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
rspec-
|
44
|
-
rspec-
|
45
|
-
|
46
|
-
rspec-
|
47
|
-
|
48
|
-
|
49
|
-
rspec-mocks (2.0.1)
|
50
|
-
rspec-core (~> 2.0.1)
|
51
|
-
rspec-expectations (~> 2.0.1)
|
28
|
+
meta_where (0.9.9.2)
|
29
|
+
activerecord (~> 3.0.2)
|
30
|
+
activesupport (~> 3.0.2)
|
31
|
+
arel (~> 2.0.2)
|
32
|
+
rspec (2.1.0)
|
33
|
+
rspec-core (~> 2.1.0)
|
34
|
+
rspec-expectations (~> 2.1.0)
|
35
|
+
rspec-mocks (~> 2.1.0)
|
36
|
+
rspec-core (2.1.0)
|
37
|
+
rspec-expectations (2.1.0)
|
38
|
+
diff-lcs (~> 1.1.2)
|
39
|
+
rspec-mocks (2.1.0)
|
52
40
|
sqlite3-ruby (1.3.2)
|
53
41
|
term-ansicolor (1.0.5)
|
54
42
|
tzinfo (0.3.23)
|
55
43
|
|
56
44
|
PLATFORMS
|
57
|
-
java
|
58
45
|
ruby
|
59
46
|
|
60
47
|
DEPENDENCIES
|
61
|
-
activerecord (~> 3.0.
|
48
|
+
activerecord (~> 3.0.3)
|
62
49
|
activerecord-jdbcsqlite3-adapter (~> 0.9.7)
|
63
50
|
cucumber (~> 0.9.3)
|
64
51
|
jdbc-sqlite3
|
65
52
|
meta_where
|
66
|
-
rspec (~> 2.0
|
53
|
+
rspec (~> 2.1.0)
|
67
54
|
sqlite3-ruby
|
data/HISTORY.txt
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -18,8 +18,8 @@ in the database. Predicates are provided for each symbol in the enumeration
|
|
18
18
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
19
19
|
the symbols may also be used when interacting with the underlying Arel attribute
|
20
20
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
21
|
-
Ruby 1.9.2, JRuby 1.5.
|
22
|
-
3.0.
|
21
|
+
Ruby 1.9.2, JRuby 1.5.5, Rubinius 1.1.0, ActiveRecord 2.3.10, and ActiveRecord
|
22
|
+
3.0.3.
|
23
23
|
EOF
|
24
24
|
gemspec.email = "greg@rujubu.com"
|
25
25
|
gemspec.homepage = "http://github.com/gregspurrier/has_enumeration"
|
data/TODO.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Release 1.1
|
2
|
+
* Support string assignment
|
3
|
+
* Intended for bulk assignment from form posts
|
4
|
+
* Unlike symbol assignment, will not raise an exception when a value
|
5
|
+
is invalid
|
6
|
+
* Invalid values will fail validation and add an error on the attribute
|
7
|
+
* Expose the accepted values for an enumeration via something like
|
8
|
+
`Car::Color.values`
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
@@ -55,7 +55,7 @@ end
|
|
55
55
|
|
56
56
|
When /^I query for objects with the value :([a-z_]+) via arel$/ do |value|
|
57
57
|
@desired_color = value.to_sym
|
58
|
-
arel_attr = @model_class.arel_table
|
58
|
+
arel_attr = @model_class.arel_table[:color]
|
59
59
|
@results = @model_class.where(arel_attr.eq(@desired_color)).order(:id)
|
60
60
|
end
|
61
61
|
|
@@ -3,7 +3,7 @@ module HasEnumeration
|
|
3
3
|
module TableExtensions
|
4
4
|
def self.included(base)
|
5
5
|
base.class_eval do
|
6
|
-
alias_method_chain :
|
6
|
+
alias_method_chain :columns, :has_enumeration
|
7
7
|
|
8
8
|
def self.has_enumeration_mappings
|
9
9
|
@has_enumeration_mappings ||= Hash.new {|h,k| h[k] = Hash.new}
|
@@ -11,17 +11,17 @@ module HasEnumeration
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
return @
|
16
|
-
|
14
|
+
def columns_with_has_enumeration
|
15
|
+
return @columns if @columns
|
16
|
+
columns = columns_without_has_enumeration
|
17
17
|
mappings = self.class.has_enumeration_mappings[name]
|
18
18
|
if mappings
|
19
19
|
mappings.each do |attr_name, mapping|
|
20
|
-
attr =
|
20
|
+
attr = columns.detect {|c| c.name.to_s == attr_name.to_s}
|
21
21
|
install_has_enumeration_attribute_mapping(attr, mapping)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
columns
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
@@ -45,18 +45,16 @@ module HasEnumeration
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
::Arel::
|
49
|
-
|
48
|
+
predicates = ::Arel::Predications.instance_methods.map &:to_sym
|
49
|
+
predicates.each do |method_name|
|
50
50
|
arity = ::Arel::Attribute.instance_method(method_name).arity
|
51
51
|
case arity
|
52
52
|
when 1
|
53
53
|
define_method method_name do |arg|
|
54
54
|
super(map_enumeration_arg(arg))
|
55
55
|
end
|
56
|
-
when
|
57
|
-
|
58
|
-
super(map_enumeration_arg(args))
|
59
|
-
end
|
56
|
+
when 0
|
57
|
+
# No-op
|
60
58
|
else
|
61
59
|
raise "Unexpected arity #{arity} for #{method_name}"
|
62
60
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Support for Arel 1. Will eventually be removed once most people are on
|
2
|
+
# Arel 2.
|
3
|
+
module HasEnumeration
|
4
|
+
module Arel
|
5
|
+
module TableExtensionsArelOne
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
alias_method_chain :attributes, :has_enumeration
|
9
|
+
|
10
|
+
def self.has_enumeration_mappings
|
11
|
+
@has_enumeration_mappings ||= Hash.new {|h,k| h[k] = Hash.new}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def attributes_with_has_enumeration
|
17
|
+
return @attributes if @attributes
|
18
|
+
attrs = attributes_without_has_enumeration
|
19
|
+
mappings = self.class.has_enumeration_mappings[name]
|
20
|
+
if mappings
|
21
|
+
mappings.each do |attr_name, mapping|
|
22
|
+
attr = attrs[attr_name]
|
23
|
+
install_has_enumeration_attribute_mapping(attr, mapping)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
attrs
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def install_has_enumeration_attribute_mapping(arel_attr, mapping)
|
31
|
+
# For this attribute only, override all of the methods defined
|
32
|
+
# in Arel::Attribute::PREDICATES so that they will perform the
|
33
|
+
# symbol-to-underlying-value mapping before proceeding with their work.
|
34
|
+
(class <<arel_attr;self;end).class_eval do
|
35
|
+
define_method :map_enumeration_arg do |arg|
|
36
|
+
if arg.is_a?(Symbol)
|
37
|
+
unless mapping.has_key?(arg)
|
38
|
+
raise ArgumentError.new(
|
39
|
+
"#{arg.inspect} is not one of {#{mapping.keys.map(&:inspect).sort.join(', ')}}"
|
40
|
+
)
|
41
|
+
end
|
42
|
+
mapping[arg]
|
43
|
+
elsif arg.is_a?(Array)
|
44
|
+
arg.map {|a| map_enumeration_arg(a)}
|
45
|
+
else
|
46
|
+
arg
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
::Arel::Attribute::PREDICATES.each do |method_name|
|
51
|
+
# Preserve the arity of the method we are overriding
|
52
|
+
arity = ::Arel::Attribute.instance_method(method_name).arity
|
53
|
+
case arity
|
54
|
+
when 1
|
55
|
+
define_method method_name do |arg|
|
56
|
+
super(map_enumeration_arg(arg))
|
57
|
+
end
|
58
|
+
when -1
|
59
|
+
define_method method_name do |*args|
|
60
|
+
super(map_enumeration_arg(args))
|
61
|
+
end
|
62
|
+
else
|
63
|
+
raise "Unexpected arity #{arity} for #{method_name}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/has_enumeration.rb
CHANGED
@@ -7,8 +7,13 @@ ActiveRecord::Base.extend(HasEnumeration::ClassMethods)
|
|
7
7
|
# For ActiveRecord 3, extend Arel::Table, otherwise we'll need
|
8
8
|
# our specialization of aggregate_conditions_override
|
9
9
|
if ActiveRecord::VERSION::MAJOR >= 3
|
10
|
-
|
11
|
-
|
10
|
+
if Arel::VERSION >= '2.0.0'
|
11
|
+
require 'has_enumeration/arel/table_extensions'
|
12
|
+
Arel::Table.send(:include, HasEnumeration::Arel::TableExtensions)
|
13
|
+
else
|
14
|
+
require 'has_enumeration/arel/table_extensions_arel_one'
|
15
|
+
Arel::Table.send(:include, HasEnumeration::Arel::TableExtensionsArelOne)
|
16
|
+
end
|
12
17
|
else
|
13
18
|
require 'has_enumeration/aggregate_conditions_override'
|
14
19
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_enumeration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Spurrier
|
@@ -15,10 +15,115 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-18 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
name: activerecord
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 1
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
- 3
|
35
|
+
version: 3.0.3
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
name: meta_where
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
type: :runtime
|
53
|
+
prerelease: false
|
54
|
+
name: rspec
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 11
|
61
|
+
segments:
|
62
|
+
- 2
|
63
|
+
- 1
|
64
|
+
- 0
|
65
|
+
version: 2.1.0
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
name: cucumber
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 61
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
- 9
|
80
|
+
- 3
|
81
|
+
version: 0.9.3
|
82
|
+
requirement: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
type: :runtime
|
85
|
+
prerelease: false
|
86
|
+
name: sqlite3-ruby
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirement: *id005
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
type: :runtime
|
99
|
+
prerelease: false
|
100
|
+
name: jdbc-sqlite3
|
101
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
requirement: *id006
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
type: :runtime
|
113
|
+
prerelease: false
|
114
|
+
name: activerecord-jdbcsqlite3-adapter
|
115
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ~>
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 53
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
- 9
|
124
|
+
- 7
|
125
|
+
version: 0.9.7
|
126
|
+
requirement: *id007
|
22
127
|
description: |
|
23
128
|
Extends ActiveRecord with the has_enumeration method allowing a symbolic
|
24
129
|
enumeration to be stored in an ActiveRecord attribute. The enumeration is
|
@@ -27,8 +132,8 @@ description: |
|
|
27
132
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
28
133
|
the symbols may also be used when interacting with the underlying Arel attribute
|
29
134
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
30
|
-
Ruby 1.9.2, JRuby 1.5.
|
31
|
-
3.0.
|
135
|
+
Ruby 1.9.2, JRuby 1.5.5, Rubinius 1.1.0, ActiveRecord 2.3.10, and ActiveRecord
|
136
|
+
3.0.3.
|
32
137
|
|
33
138
|
email: greg@rujubu.com
|
34
139
|
executables: []
|
@@ -45,6 +150,7 @@ files:
|
|
45
150
|
- LICENSE.txt
|
46
151
|
- README.md
|
47
152
|
- Rakefile
|
153
|
+
- TODO.md
|
48
154
|
- VERSION
|
49
155
|
- features/arel_attributes.feature
|
50
156
|
- features/explicitly_mapped_enumeration.feature
|
@@ -59,6 +165,7 @@ files:
|
|
59
165
|
- lib/has_enumeration.rb
|
60
166
|
- lib/has_enumeration/aggregate_conditions_override.rb
|
61
167
|
- lib/has_enumeration/arel/table_extensions.rb
|
168
|
+
- lib/has_enumeration/arel/table_extensions_arel_one.rb
|
62
169
|
- lib/has_enumeration/class_methods.rb
|
63
170
|
- spec/has_enumeration/has_enumeration_spec.rb
|
64
171
|
- spec/spec_helper.rb
|
@@ -67,8 +174,8 @@ homepage: http://github.com/gregspurrier/has_enumeration
|
|
67
174
|
licenses: []
|
68
175
|
|
69
176
|
post_install_message:
|
70
|
-
rdoc_options:
|
71
|
-
|
177
|
+
rdoc_options: []
|
178
|
+
|
72
179
|
require_paths:
|
73
180
|
- lib
|
74
181
|
required_ruby_version: !ruby/object:Gem::Requirement
|