ar_pg_array 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.5
1
+ 0.9.6
@@ -1,80 +1,169 @@
1
- module Arel
2
- module Predicates
3
- class ArrayAny < Binary
4
- def eval(row)
5
- !(operand1.eval(row) & operand2.eval(row)).empty?
1
+ if Arel::VERSION >= '2.0'
2
+ module Arel
3
+ module Nodes
4
+ class ArrayAny < Arel::Nodes::Binary
6
5
  end
7
-
8
- def predicate_sql
9
- "&&"
6
+
7
+ class ArrayAll < Arel::Nodes::Binary
8
+ end
9
+
10
+ class ArrayIncluded < Arel::Nodes::Binary
10
11
  end
11
12
  end
12
-
13
- class ArrayAll < Binary
14
- def eval(row)
15
- (operand2.eval(row) - operand1.eval(row)).empty?
13
+
14
+ module Predications
15
+ def ar_any other
16
+ Nodes::ArrayAny.new self, other
16
17
  end
17
-
18
- def predicate_sql
19
- "@>"
18
+
19
+ def ar_all other
20
+ Nodes::ArrayAll.new self, other
20
21
  end
21
- end
22
-
23
- class ArrayIncluded < Binary
24
- def eval(row)
25
- (operand1.eval(row) - operand2.eval(row)).empty?
22
+
23
+ def ar_included other
24
+ Nodes::ArrayIncluded.new self, other
26
25
  end
27
-
28
- def predicate_sql
29
- "<@"
26
+ end
27
+
28
+ module Visitors
29
+ class PostgreSQL
30
+ def visit_Arel_Nodes_ArrayAny o
31
+ "#{visit o.left} && #{visit o.right}"
32
+ end
33
+
34
+ def visit_Arel_Nodes_ArrayAll o
35
+ "#{visit o.left} @> #{visit o.right}"
36
+ end
37
+
38
+ def visit_Arel_Nodes_ArrayIncluded o
39
+ "#{visit o.left} <@ #{visit o.right}"
40
+ end
41
+
42
+ def visit_PGArrays_PgArray o
43
+ @connection.quote_array_for_arel_by_base_type(o, o.base_type)
44
+ end
45
+
46
+ alias :visit_PGArrays_PgAny :visit_PGArrays_PgArray
47
+ alias :visit_PGArrays_PgAll :visit_PGArrays_PgArray
48
+ alias :visit_PGArrays_PgIncluded :visit_PGArrays_PgArray
30
49
  end
31
50
  end
32
51
  end
33
-
34
- class Attribute
35
- methods = lambda do
36
- def ar_any(other)
37
- Predicates::ArrayAny.new(self, other)
38
- end
52
+ else
53
+ module Arel
54
+ module Predicates
55
+ class ArrayAny < Binary
56
+ def eval(row)
57
+ !(operand1.eval(row) & operand2.eval(row)).empty?
58
+ end
39
59
 
40
- def ar_all(other)
41
- Predicates::ArrayAll.new(self, other)
60
+ def predicate_sql
61
+ "&&"
62
+ end
42
63
  end
43
64
 
44
- def ar_included(other)
45
- Predicates::ArrayIncluded.new(self, other)
65
+ class ArrayAll < Binary
66
+ def eval(row)
67
+ (operand2.eval(row) - operand1.eval(row)).empty?
68
+ end
69
+
70
+ def predicate_sql
71
+ "@>"
72
+ end
46
73
  end
47
74
 
48
- def in(other)
49
- case other
50
- when ::PGArrays::PgAny
51
- ar_any(other)
52
- when ::PGArrays::PgAll
53
- ar_all(other)
54
- when ::PGArrays::PgIncludes
55
- ar_included(other)
56
- else
57
- Predicates::In.new(self, other)
75
+ class ArrayIncluded < Binary
76
+ def eval(row)
77
+ (operand1.eval(row) - operand2.eval(row)).empty?
78
+ end
79
+
80
+ def predicate_sql
81
+ "<@"
58
82
  end
59
83
  end
60
84
  end
61
- if defined? PREDICATES
62
- PREDICATES.concat [:ar_any, :ar_all, :ar_included]
63
- class_exec &methods
64
- else
65
- Predications.class_exec &methods
85
+
86
+ class Attribute
87
+ methods = lambda do
88
+ def ar_any(other)
89
+ Predicates::ArrayAny.new(self, other)
90
+ end
91
+
92
+ def ar_all(other)
93
+ Predicates::ArrayAll.new(self, other)
94
+ end
95
+
96
+ def ar_included(other)
97
+ Predicates::ArrayIncluded.new(self, other)
98
+ end
99
+ end
100
+ if defined? PREDICATES
101
+ PREDICATES.concat [:ar_any, :ar_all, :ar_included]
102
+ class_exec &methods
103
+ else
104
+ Predications.class_exec &methods
105
+ end
66
106
  end
67
107
  end
68
- end
69
108
 
70
- module PGArrays
71
- class PgArray
72
- def to_sql( formatter = nil )
73
- formatter.engine.connection.quote_array_for_arel_by_base_type(self, base_type)
109
+ module PGArrays
110
+ class PgArray
111
+ def to_sql( formatter = nil )
112
+ formatter.engine.connection.quote_array_for_arel_by_base_type(self, base_type)
113
+ end
114
+
115
+ def to_a
116
+ self
117
+ end
74
118
  end
75
-
76
- def to_a
77
- self
119
+ end
120
+ end
121
+
122
+ module ActiveRecord
123
+ class PredicateBuilder
124
+ def build_from_hash(attributes, default_table)
125
+ predicates = attributes.map do |column, value|
126
+ table = default_table
127
+
128
+ if value.is_a?(Hash)
129
+ table = Arel::Table.new(column, :engine => @engine)
130
+ build_from_hash(value, table)
131
+ else
132
+ column = column.to_s
133
+
134
+ if column.include?('.')
135
+ table_name, column = column.split('.', 2)
136
+ table = Arel::Table.new(table_name, :engine => @engine)
137
+ end
138
+
139
+ attribute = table[column] || Arel::Attribute.new(table, column)
140
+
141
+ case value
142
+ when PGArrays::PgAny
143
+ attribute.ar_any(value)
144
+ when PGArrays::PgAll
145
+ attribute.ar_all(value)
146
+ when PGArrays::PgIncludes
147
+ attribute.ar_included(value)
148
+ when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation
149
+ values = value.to_a.map { |x|
150
+ x.is_a?(ActiveRecord::Base) ? x.id : x
151
+ }
152
+ attribute.in(values)
153
+ when Range, Arel::Relation
154
+ attribute.in(value)
155
+ when ActiveRecord::Base
156
+ attribute.eq(value.id)
157
+ when Class
158
+ # FIXME: I think we need to deprecate this behavior
159
+ attribute.eq(value.name)
160
+ else
161
+ attribute.eq(value)
162
+ end
163
+ end
164
+ end
165
+
166
+ predicates.flatten
78
167
  end
79
168
  end
80
169
  end
@@ -65,26 +65,41 @@ module Arel
65
65
  end
66
66
  end
67
67
 
68
- module Sql
68
+ if Arel::VERSION < '2.0'
69
+ module Sql
70
+ module Attributes
71
+ class << self
72
+ def for_with_postgresql_arrays(column)
73
+ if column.type.to_s =~ /^(.+)_array$/
74
+ ('Arel::Sql::Attributes::' + for_without_postgresql_arrays(column.base_column).name.split('::').last + 'Array').constantize
75
+ else
76
+ for_without_postgresql_arrays(column)
77
+ end
78
+ end
79
+ alias_method_chain :for, :postgresql_arrays
80
+ end
81
+
82
+ %w{Integer Float Decimal Boolean String Time}.each do |basetype|
83
+ module_eval <<-"END"
84
+ class #{basetype}Array < Arel::Attributes::#{basetype}Array
85
+ include Attributes
86
+ end
87
+ END
88
+ end
89
+ end
90
+ end
91
+ else
69
92
  module Attributes
70
93
  class << self
71
94
  def for_with_postgresql_arrays(column)
72
95
  if column.type.to_s =~ /^(.+)_array$/
73
- ('Arel::Sql::Attributes::' + for_without_postgresql_arrays(column.base_column).name.split('::').last + 'Array').constantize
96
+ ('Arel::Attributes::' + for_without_postgresql_arrays(column.base_column).name.split('::').last + 'Array').constantize
74
97
  else
75
98
  for_without_postgresql_arrays(column)
76
99
  end
77
100
  end
78
101
  alias_method_chain :for, :postgresql_arrays
79
102
  end
80
-
81
- %w{Integer Float Decimal Boolean String Time}.each do |basetype|
82
- module_eval <<-"END"
83
- class #{basetype}Array < Arel::Attributes::#{basetype}Array
84
- include Attributes
85
- end
86
- END
87
- end
88
103
  end
89
104
  end
90
105
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_pg_array
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
5
- prerelease: false
4
+ hash: 55
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 5
10
- version: 0.9.5
9
+ - 6
10
+ version: 0.9.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sokolov Yura aka funny_falcon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-20 00:00:00 +03:00
18
+ date: 2011-04-13 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements: []
94
94
 
95
95
  rubyforge_project: ar-pg-array
96
- rubygems_version: 1.3.7
96
+ rubygems_version: 1.6.2
97
97
  signing_key:
98
98
  specification_version: 3
99
99
  summary: Use power of PostgreSQL Arrays in ActiveRecord