has_enumeration 0.1.0 → 0.2.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/README.txt CHANGED
@@ -23,6 +23,10 @@ obj.color.green? # => false
23
23
  # Querying
24
24
  TestObject.where(:color => :red)
25
25
 
26
+ # Querying with arel predicates
27
+ attr = TestObject.arel_table[:color]
28
+ TestObject.where(attr.not_in([:red, :green]))
29
+
26
30
  == Getting the Latest Version
27
31
  The repository for has_enumeration is hosted at GitHub:
28
32
  Web page: http://github.com/gregspurrier/has_enumeration
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -49,3 +49,16 @@ Feature:
49
49
  | :red |
50
50
  | :green |
51
51
  | :blue |
52
+
53
+ Scenario Outline: find objects with a specific enumerated value via arel
54
+ Given a model with an explicitly-mapped enumeration of red, green, and blue
55
+ And a set of objects with a variety of values for the enumeration
56
+ When I query for objects with the value <value> via arel
57
+ Then I should get all of the objects having that value
58
+ And I should not get any objects having other values
59
+
60
+ Scenarios: querying various values
61
+ | value |
62
+ | :red |
63
+ | :green |
64
+ | :blue |
@@ -49,3 +49,16 @@ Feature:
49
49
  | :red |
50
50
  | :green |
51
51
  | :blue |
52
+
53
+ Scenario Outline: find objects with a specific enumerated value via arel
54
+ Given a model with an implicitly-mapped enumeration of red, green, and blue
55
+ And a set of objects with a variety of values for the enumeration
56
+ When I query for objects with the value <value> via arel
57
+ Then I should get all of the objects having that value
58
+ And I should not get any objects having other values
59
+
60
+ Scenarios: querying various values
61
+ | value |
62
+ | :red |
63
+ | :green |
64
+ | :blue |
@@ -49,6 +49,12 @@ When /^I query for objects with the value :([a-z_]+)$/ do |value|
49
49
  @results = @model_class.where(:color => @desired_color).order(:id)
50
50
  end
51
51
 
52
+ When /^I query for objects with the value :([a-z_]+) via arel$/ do |value|
53
+ @desired_color = value.to_sym
54
+ arel_attr = @model_class.arel_table.attributes[:color]
55
+ @results = @model_class.where(arel_attr.eq(@desired_color)).order(:id)
56
+ end
57
+
52
58
  Then /^I should get all of the objects having that value$/ do
53
59
  @results.should == @all_objects.select {|x| x.color.value == @desired_color}
54
60
  end
@@ -31,6 +31,12 @@ module HasEnumeration
31
31
  extend HasEnumeration::AggregateConditionsOverride
32
32
  @aggregate_conditions_override_installed = true
33
33
  end
34
+
35
+ if respond_to?(:arel_table)
36
+ # Patch the enumeration's underlying arel attribute to make it
37
+ # aware of the mapping.
38
+ install_arel_attribute_mapping(attribute, mapping)
39
+ end
34
40
  end
35
41
 
36
42
  private
@@ -63,5 +69,41 @@ module HasEnumeration
63
69
  end
64
70
  end
65
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::Predications.instance_methods.each do |method_name|
90
+ # Preserve the arity of the method we are overriding
91
+ arity = Arel::Attribute::Predications.
92
+ instance_method(method_name).arity
93
+ case arity
94
+ when 1
95
+ define_method method_name do |arg|
96
+ super(map_enumeration_arg(arg))
97
+ end
98
+ when -1
99
+ define_method method_name do |*args|
100
+ super(map_enumeration_arg(args))
101
+ end
102
+ else
103
+ raise "Unexpected arity #{arity} for #{method_name}"
104
+ end
105
+ end
106
+ end
107
+ end
66
108
  end
67
109
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_enumeration
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 1
8
+ - 2
8
9
  - 0
9
- version: 0.1.0
10
+ version: 0.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Greg Spurrier
@@ -56,6 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
57
  requirements:
57
58
  - - ">="
58
59
  - !ruby/object:Gem::Version
60
+ hash: 3
59
61
  segments:
60
62
  - 0
61
63
  version: "0"
@@ -64,6 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  requirements:
65
67
  - - ">="
66
68
  - !ruby/object:Gem::Version
69
+ hash: 3
67
70
  segments:
68
71
  - 0
69
72
  version: "0"