has_enumeration 0.3.1 → 0.4.0
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/Gemfile +2 -1
- data/Gemfile.lock +55 -0
- data/HISTORY.txt +4 -0
- data/README.txt +5 -2
- data/Rakefile +2 -2
- data/TODO +0 -1
- data/VERSION +1 -1
- data/features/meta_where_queries.feature +20 -0
- data/features/step_definitions/has_enumeration_steps.rb +5 -0
- data/features/support/env.rb +4 -0
- data/lib/has_enumeration/arel/table_extensions.rb +63 -0
- data/lib/has_enumeration/class_methods.rb +3 -38
- data/lib/has_enumeration.rb +7 -1
- metadata +9 -6
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.0)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
builder (2.1.2)
|
17
|
+
cucumber (0.8.5)
|
18
|
+
builder (~> 2.1.2)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
gherkin (~> 2.1.4)
|
21
|
+
json_pure (~> 1.4.3)
|
22
|
+
term-ansicolor (~> 1.0.4)
|
23
|
+
diff-lcs (1.1.2)
|
24
|
+
gherkin (2.1.5)
|
25
|
+
trollop (~> 1.16.2)
|
26
|
+
i18n (0.4.1)
|
27
|
+
json_pure (1.4.6)
|
28
|
+
meta_where (0.9.2)
|
29
|
+
activerecord (~> 3.0.0.rc2)
|
30
|
+
activesupport (~> 3.0.0.rc2)
|
31
|
+
arel (~> 1.0.0.rc1)
|
32
|
+
rspec (2.0.0.beta.20)
|
33
|
+
rspec-core (= 2.0.0.beta.20)
|
34
|
+
rspec-expectations (= 2.0.0.beta.20)
|
35
|
+
rspec-mocks (= 2.0.0.beta.20)
|
36
|
+
rspec-core (2.0.0.beta.20)
|
37
|
+
rspec-expectations (2.0.0.beta.20)
|
38
|
+
diff-lcs (>= 1.1.2)
|
39
|
+
rspec-mocks (2.0.0.beta.20)
|
40
|
+
sqlite3-ruby (1.3.1)
|
41
|
+
term-ansicolor (1.0.5)
|
42
|
+
trollop (1.16.2)
|
43
|
+
tzinfo (0.3.23)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
activerecord (= 3.0.0)
|
50
|
+
activerecord-jdbcsqlite3-adapter (~> 0.9.7)
|
51
|
+
cucumber (~> 0.8.5)
|
52
|
+
jdbc-sqlite3
|
53
|
+
meta_where
|
54
|
+
rspec (~> 2.0.0.beta.19)
|
55
|
+
sqlite3-ruby
|
data/HISTORY.txt
CHANGED
data/README.txt
CHANGED
@@ -27,6 +27,9 @@ TestObject.where(:color => :red)
|
|
27
27
|
attr = TestObject.arel_table[:color]
|
28
28
|
TestObject.where(attr.not_in([:red, :green]))
|
29
29
|
|
30
|
+
# Querying with MetaWhere (ActiveRecord 3 only, with MetaWhere gem)
|
31
|
+
TestObject.where(:color.not_in => [:red, :green])
|
32
|
+
|
30
33
|
== Installation
|
31
34
|
gem install has_enumeration
|
32
35
|
|
@@ -41,7 +44,7 @@ has_enumeration has been tested with all combinations for the following:
|
|
41
44
|
Rubies:
|
42
45
|
- 1.8.7-p302
|
43
46
|
- 1.9.2-p0
|
44
|
-
- JRuby 1.5.
|
47
|
+
- JRuby 1.5.2
|
45
48
|
ActiveRecord:
|
46
49
|
- 2.3.8
|
47
|
-
- 3.0.0
|
50
|
+
- 3.0.0
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ in the database. Predicates are provided for each symbol in the enumeration
|
|
15
15
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
16
16
|
the symbols may also be used when interacting with the underlying Arel attribute
|
17
17
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
18
|
-
Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
|
18
|
+
Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
|
19
19
|
EOF
|
20
20
|
gemspec.email = "greg@rujubu.com"
|
21
21
|
gemspec.homepage = "http://github.com/gregspurrier/has_enumeration"
|
@@ -37,7 +37,7 @@ namespace :features do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
Cucumber::Rake::Task.new(:rails3) do |t|
|
40
|
-
features = %w(arel_attributes)
|
40
|
+
features = %w(arel_attributes meta_where_queries)
|
41
41
|
feature_files = features.map {|f| "features/#{f}.feature"}.join(' ')
|
42
42
|
t.cucumber_opts = feature_files
|
43
43
|
end
|
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Feature:
|
2
|
+
|
3
|
+
As a programmer using has_enumeration in Rails 3
|
4
|
+
I want to be able to refer to the enumeration symbolically when working with
|
5
|
+
the MetaWhere queries for a model
|
6
|
+
So that I can write pretty, concise code.
|
7
|
+
|
8
|
+
|
9
|
+
Scenario Outline: find objects with a specific enumerated value via MetaWhere
|
10
|
+
Given a model with an explicitly-mapped enumeration of red, green, and blue
|
11
|
+
And a set of objects with a variety of values for the enumeration
|
12
|
+
When I query for objects with the value <value> via MetaWhere
|
13
|
+
Then I should get all of the objects having that value
|
14
|
+
And I should not get any objects having other values
|
15
|
+
|
16
|
+
Scenarios: querying various values
|
17
|
+
| value |
|
18
|
+
| :red |
|
19
|
+
| :green |
|
20
|
+
| :blue |
|
@@ -55,6 +55,11 @@ When /^I query for objects with the value :([a-z_]+) via arel$/ do |value|
|
|
55
55
|
@results = @model_class.where(arel_attr.eq(@desired_color)).order(:id)
|
56
56
|
end
|
57
57
|
|
58
|
+
When /^I query for objects with the value :([a-z_]+) via MetaWhere$/ do |value|
|
59
|
+
@desired_color = value.to_sym
|
60
|
+
@results = @model_class.where(:color.eq => @desired_color).order(:id)
|
61
|
+
end
|
62
|
+
|
58
63
|
Then /^I should get all of the objects having that value$/ do
|
59
64
|
@results.should == @all_objects.select {|x| x.color.value == @desired_color}
|
60
65
|
end
|
data/features/support/env.rb
CHANGED
@@ -6,6 +6,10 @@ require 'rubygems'
|
|
6
6
|
require 'bundler/setup'
|
7
7
|
require 'has_enumeration'
|
8
8
|
|
9
|
+
if ENV['AR_VERSION'] == '3.0'
|
10
|
+
require 'meta_where'
|
11
|
+
end
|
12
|
+
|
9
13
|
ActiveRecord::Base.establish_connection(
|
10
14
|
:adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3': 'sqlite3',
|
11
15
|
:database => File.expand_path('../database', __FILE__)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module HasEnumeration
|
2
|
+
module Arel
|
3
|
+
module TableExtensions
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :attributes, :has_enumeration
|
7
|
+
|
8
|
+
def self.has_enumeration_mappings
|
9
|
+
@has_enumeration_mappings ||= Hash.new {|h,k| h[k] = Hash.new}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes_with_has_enumeration
|
15
|
+
return @attributes if @attributes
|
16
|
+
attrs = attributes_without_has_enumeration
|
17
|
+
mappings = self.class.has_enumeration_mappings[name]
|
18
|
+
if mappings
|
19
|
+
mappings.each do |attr_name, mapping|
|
20
|
+
attr = attrs[attr_name]
|
21
|
+
install_has_enumeration_attribute_mapping(attr, mapping)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
attrs
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def install_has_enumeration_attribute_mapping(arel_attr, mapping)
|
29
|
+
# For this attribute only, override all of the methods defined
|
30
|
+
# in Arel::Attribute::PREDICATES so that they will perform the
|
31
|
+
# symbol-to-underlying-value mapping before proceeding with their work.
|
32
|
+
(class <<arel_attr;self;end).class_eval do
|
33
|
+
define_method :map_enumeration_arg do |arg|
|
34
|
+
if arg.is_a?(Symbol)
|
35
|
+
mapping[arg]
|
36
|
+
elsif arg.is_a?(Array)
|
37
|
+
arg.map {|a| map_enumeration_arg(a)}
|
38
|
+
else
|
39
|
+
arg
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
::Arel::Attribute::PREDICATES.each do |method_name|
|
44
|
+
# Preserve the arity of the method we are overriding
|
45
|
+
arity = ::Arel::Attribute.instance_method(method_name).arity
|
46
|
+
case arity
|
47
|
+
when 1
|
48
|
+
define_method method_name do |arg|
|
49
|
+
super(map_enumeration_arg(arg))
|
50
|
+
end
|
51
|
+
when -1
|
52
|
+
define_method method_name do |*args|
|
53
|
+
super(map_enumeration_arg(args))
|
54
|
+
end
|
55
|
+
else
|
56
|
+
raise "Unexpected arity #{arity} for #{method_name}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -33,9 +33,9 @@ module HasEnumeration
|
|
33
33
|
end
|
34
34
|
|
35
35
|
if respond_to?(:arel_table)
|
36
|
-
#
|
37
|
-
#
|
38
|
-
|
36
|
+
# Install this attributes mapping for use later when extending
|
37
|
+
# Arel attributes on the fly.
|
38
|
+
::Arel::Table.has_enumeration_mappings[table_name][attribute] = mapping
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -69,40 +69,5 @@ module HasEnumeration
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
73
|
-
def install_arel_attribute_mapping(attribute, mapping)
|
74
|
-
# For this attribute only, override all of the methods defined
|
75
|
-
# in Arel::Attribute::Predications so that they will perform the
|
76
|
-
# symbol-to-underlying-value mapping before proceeding with their work.
|
77
|
-
arel_attr = arel_table[attribute]
|
78
|
-
(class <<arel_attr;self;end).class_eval do
|
79
|
-
define_method :map_enumeration_arg do |arg|
|
80
|
-
if arg.is_a?(Symbol)
|
81
|
-
mapping[arg]
|
82
|
-
elsif arg.is_a?(Array)
|
83
|
-
arg.map {|a| map_enumeration_arg(a)}
|
84
|
-
else
|
85
|
-
arg
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
Arel::Attribute::PREDICATES.each do |method_name|
|
90
|
-
# Preserve the arity of the method we are overriding
|
91
|
-
arity = Arel::Attribute.instance_method(method_name).arity
|
92
|
-
case arity
|
93
|
-
when 1
|
94
|
-
define_method method_name do |arg|
|
95
|
-
super(map_enumeration_arg(arg))
|
96
|
-
end
|
97
|
-
when -1
|
98
|
-
define_method method_name do |*args|
|
99
|
-
super(map_enumeration_arg(args))
|
100
|
-
end
|
101
|
-
else
|
102
|
-
raise "Unexpected arity #{arity} for #{method_name}"
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
72
|
end
|
108
73
|
end
|
data/lib/has_enumeration.rb
CHANGED
@@ -3,5 +3,11 @@ require 'active_record'
|
|
3
3
|
require 'has_enumeration/class_methods'
|
4
4
|
require 'has_enumeration/aggregate_conditions_override'
|
5
5
|
|
6
|
-
# Install our
|
6
|
+
# Install our ActiveRecord extentions
|
7
7
|
ActiveRecord::Base.extend(HasEnumeration::ClassMethods)
|
8
|
+
|
9
|
+
# If Arel is present, extend it
|
10
|
+
if defined? Arel::Table
|
11
|
+
require 'has_enumeration/arel/table_extensions'
|
12
|
+
Arel::Table.send(:include, HasEnumeration::Arel::TableExtensions)
|
13
|
+
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: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Spurrier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-09-01 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -27,7 +27,7 @@ description: |
|
|
27
27
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
28
28
|
the symbols may also be used when interacting with the underlying Arel attribute
|
29
29
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
30
|
-
Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
|
30
|
+
Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
|
31
31
|
|
32
32
|
email: greg@rujubu.com
|
33
33
|
executables: []
|
@@ -40,6 +40,7 @@ extra_rdoc_files:
|
|
40
40
|
- TODO
|
41
41
|
files:
|
42
42
|
- Gemfile
|
43
|
+
- Gemfile.lock
|
43
44
|
- HISTORY.txt
|
44
45
|
- LICENSE.txt
|
45
46
|
- README.txt
|
@@ -49,12 +50,14 @@ files:
|
|
49
50
|
- features/arel_attributes.feature
|
50
51
|
- features/explicitly_mapped_enumeration.feature
|
51
52
|
- features/implicitly_mapped_enumeration.feature
|
53
|
+
- features/meta_where_queries.feature
|
52
54
|
- features/step_definitions/has_enumeration_steps.rb
|
53
55
|
- features/support/env.rb
|
54
56
|
- features/support/explicitly_mapped_model.rb
|
55
57
|
- features/support/implicitly_mapped_model.rb
|
56
58
|
- lib/has_enumeration.rb
|
57
59
|
- lib/has_enumeration/aggregate_conditions_override.rb
|
60
|
+
- lib/has_enumeration/arel/table_extensions.rb
|
58
61
|
- lib/has_enumeration/class_methods.rb
|
59
62
|
has_rdoc: true
|
60
63
|
homepage: http://github.com/gregspurrier/has_enumeration
|