activerecord 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +776 -1330
  3. data/README.rdoc +15 -10
  4. data/lib/active_record/aggregations.rb +12 -8
  5. data/lib/active_record/association_relation.rb +4 -0
  6. data/lib/active_record/associations/alias_tracker.rb +14 -13
  7. data/lib/active_record/associations/association.rb +2 -2
  8. data/lib/active_record/associations/association_scope.rb +83 -43
  9. data/lib/active_record/associations/belongs_to_association.rb +15 -5
  10. data/lib/active_record/associations/builder/association.rb +15 -4
  11. data/lib/active_record/associations/builder/belongs_to.rb +7 -29
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +9 -6
  13. data/lib/active_record/associations/builder/has_many.rb +1 -1
  14. data/lib/active_record/associations/builder/has_one.rb +2 -2
  15. data/lib/active_record/associations/builder/singular_association.rb +8 -1
  16. data/lib/active_record/associations/collection_association.rb +66 -29
  17. data/lib/active_record/associations/collection_proxy.rb +22 -26
  18. data/lib/active_record/associations/has_many_association.rb +65 -18
  19. data/lib/active_record/associations/has_many_through_association.rb +55 -27
  20. data/lib/active_record/associations/has_one_association.rb +0 -1
  21. data/lib/active_record/associations/join_dependency/join_association.rb +19 -15
  22. data/lib/active_record/associations/join_dependency/join_part.rb +0 -1
  23. data/lib/active_record/associations/join_dependency.rb +20 -12
  24. data/lib/active_record/associations/preloader/association.rb +34 -11
  25. data/lib/active_record/associations/preloader/through_association.rb +4 -3
  26. data/lib/active_record/associations/preloader.rb +49 -59
  27. data/lib/active_record/associations/singular_association.rb +25 -4
  28. data/lib/active_record/associations/through_association.rb +23 -14
  29. data/lib/active_record/associations.rb +171 -42
  30. data/lib/active_record/attribute.rb +149 -0
  31. data/lib/active_record/attribute_assignment.rb +18 -10
  32. data/lib/active_record/attribute_decorators.rb +66 -0
  33. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -2
  34. data/lib/active_record/attribute_methods/dirty.rb +98 -44
  35. data/lib/active_record/attribute_methods/primary_key.rb +14 -8
  36. data/lib/active_record/attribute_methods/query.rb +1 -1
  37. data/lib/active_record/attribute_methods/read.rb +22 -59
  38. data/lib/active_record/attribute_methods/serialization.rb +37 -147
  39. data/lib/active_record/attribute_methods/time_zone_conversion.rb +34 -28
  40. data/lib/active_record/attribute_methods/write.rb +14 -21
  41. data/lib/active_record/attribute_methods.rb +67 -94
  42. data/lib/active_record/attribute_set/builder.rb +86 -0
  43. data/lib/active_record/attribute_set.rb +77 -0
  44. data/lib/active_record/attributes.rb +139 -0
  45. data/lib/active_record/autosave_association.rb +45 -38
  46. data/lib/active_record/base.rb +10 -20
  47. data/lib/active_record/callbacks.rb +7 -7
  48. data/lib/active_record/coders/json.rb +13 -0
  49. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +78 -52
  50. data/lib/active_record/connection_adapters/abstract/database_statements.rb +38 -59
  51. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -0
  52. data/lib/active_record/connection_adapters/abstract/quoting.rb +59 -55
  53. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +46 -5
  54. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +126 -54
  55. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -34
  56. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +198 -64
  57. data/lib/active_record/connection_adapters/abstract/transaction.rb +126 -114
  58. data/lib/active_record/connection_adapters/abstract_adapter.rb +154 -55
  59. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +240 -135
  60. data/lib/active_record/connection_adapters/column.rb +28 -239
  61. data/lib/active_record/connection_adapters/connection_specification.rb +16 -25
  62. data/lib/active_record/connection_adapters/mysql2_adapter.rb +20 -22
  63. data/lib/active_record/connection_adapters/mysql_adapter.rb +65 -149
  64. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +15 -27
  65. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  66. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +39 -27
  67. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +99 -0
  68. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  69. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  70. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +14 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  73. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +27 -0
  74. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +17 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  78. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  79. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  80. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  81. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  82. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  83. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +97 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid.rb +29 -374
  93. data/lib/active_record/connection_adapters/postgresql/quoting.rb +55 -135
  94. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +4 -4
  95. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  96. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +127 -38
  97. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  98. data/lib/active_record/connection_adapters/postgresql_adapter.rb +220 -466
  99. data/lib/active_record/connection_adapters/schema_cache.rb +14 -28
  100. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +66 -61
  101. data/lib/active_record/connection_handling.rb +3 -3
  102. data/lib/active_record/core.rb +143 -32
  103. data/lib/active_record/counter_cache.rb +60 -7
  104. data/lib/active_record/enum.rb +10 -11
  105. data/lib/active_record/errors.rb +49 -27
  106. data/lib/active_record/explain.rb +1 -1
  107. data/lib/active_record/fixtures.rb +56 -70
  108. data/lib/active_record/gem_version.rb +2 -2
  109. data/lib/active_record/inheritance.rb +35 -10
  110. data/lib/active_record/integration.rb +4 -4
  111. data/lib/active_record/locking/optimistic.rb +35 -17
  112. data/lib/active_record/log_subscriber.rb +1 -1
  113. data/lib/active_record/migration/command_recorder.rb +19 -2
  114. data/lib/active_record/migration/join_table.rb +1 -1
  115. data/lib/active_record/migration.rb +52 -49
  116. data/lib/active_record/model_schema.rb +49 -57
  117. data/lib/active_record/nested_attributes.rb +7 -7
  118. data/lib/active_record/null_relation.rb +19 -5
  119. data/lib/active_record/persistence.rb +50 -31
  120. data/lib/active_record/query_cache.rb +3 -3
  121. data/lib/active_record/querying.rb +10 -7
  122. data/lib/active_record/railtie.rb +14 -11
  123. data/lib/active_record/railties/databases.rake +56 -54
  124. data/lib/active_record/readonly_attributes.rb +0 -1
  125. data/lib/active_record/reflection.rb +286 -102
  126. data/lib/active_record/relation/batches.rb +0 -1
  127. data/lib/active_record/relation/calculations.rb +39 -31
  128. data/lib/active_record/relation/delegation.rb +2 -2
  129. data/lib/active_record/relation/finder_methods.rb +80 -36
  130. data/lib/active_record/relation/merger.rb +25 -30
  131. data/lib/active_record/relation/predicate_builder/array_handler.rb +31 -13
  132. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  133. data/lib/active_record/relation/predicate_builder.rb +11 -10
  134. data/lib/active_record/relation/query_methods.rb +141 -55
  135. data/lib/active_record/relation/spawn_methods.rb +3 -0
  136. data/lib/active_record/relation.rb +69 -30
  137. data/lib/active_record/result.rb +18 -7
  138. data/lib/active_record/sanitization.rb +12 -2
  139. data/lib/active_record/schema.rb +0 -1
  140. data/lib/active_record/schema_dumper.rb +58 -26
  141. data/lib/active_record/schema_migration.rb +11 -0
  142. data/lib/active_record/scoping/default.rb +8 -7
  143. data/lib/active_record/scoping/named.rb +4 -0
  144. data/lib/active_record/serializers/xml_serializer.rb +3 -7
  145. data/lib/active_record/statement_cache.rb +95 -10
  146. data/lib/active_record/store.rb +19 -10
  147. data/lib/active_record/tasks/database_tasks.rb +73 -7
  148. data/lib/active_record/tasks/mysql_database_tasks.rb +3 -2
  149. data/lib/active_record/tasks/postgresql_database_tasks.rb +1 -1
  150. data/lib/active_record/tasks/sqlite_database_tasks.rb +5 -1
  151. data/lib/active_record/timestamp.rb +11 -9
  152. data/lib/active_record/transactions.rb +37 -21
  153. data/lib/active_record/type/big_integer.rb +13 -0
  154. data/lib/active_record/type/binary.rb +50 -0
  155. data/lib/active_record/type/boolean.rb +30 -0
  156. data/lib/active_record/type/date.rb +46 -0
  157. data/lib/active_record/type/date_time.rb +43 -0
  158. data/lib/active_record/type/decimal.rb +40 -0
  159. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  160. data/lib/active_record/type/decorator.rb +14 -0
  161. data/lib/active_record/type/float.rb +19 -0
  162. data/lib/active_record/type/hash_lookup_type_map.rb +17 -0
  163. data/lib/active_record/type/integer.rb +55 -0
  164. data/lib/active_record/type/mutable.rb +16 -0
  165. data/lib/active_record/type/numeric.rb +36 -0
  166. data/lib/active_record/type/serialized.rb +56 -0
  167. data/lib/active_record/type/string.rb +36 -0
  168. data/lib/active_record/type/text.rb +11 -0
  169. data/lib/active_record/type/time.rb +26 -0
  170. data/lib/active_record/type/time_value.rb +38 -0
  171. data/lib/active_record/type/type_map.rb +64 -0
  172. data/lib/active_record/type/unsigned_integer.rb +15 -0
  173. data/lib/active_record/type/value.rb +101 -0
  174. data/lib/active_record/type.rb +23 -0
  175. data/lib/active_record/validations/associated.rb +5 -3
  176. data/lib/active_record/validations/presence.rb +6 -4
  177. data/lib/active_record/validations/uniqueness.rb +11 -17
  178. data/lib/active_record/validations.rb +25 -19
  179. data/lib/active_record.rb +3 -0
  180. data/lib/rails/generators/active_record/migration/migration_generator.rb +8 -4
  181. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +4 -1
  182. data/lib/rails/generators/active_record/migration/templates/migration.rb +6 -0
  183. data/lib/rails/generators/active_record/model/templates/model.rb +1 -1
  184. metadata +65 -10
  185. data/lib/active_record/connection_adapters/postgresql/cast.rb +0 -168
@@ -1,7 +1,7 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= class_name %> < <%= parent_class_name.classify %>
3
3
  <% attributes.select(&:reference?).each do |attribute| -%>
4
- belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
4
+ belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
5
5
  <% end -%>
6
6
  <% if attributes.any?(&:password_digest?) -%>
7
7
  has_secure_password
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-08 00:00:00.000000000 Z
11
+ date: 2014-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.0
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.0
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 4.1.0
33
+ version: 4.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 4.1.0
40
+ version: 4.2.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: arel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 5.0.0
47
+ version: '6.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 5.0.0
54
+ version: '6.0'
55
55
  description: Databases on Rails. Build a persistent domain model by mapping database
56
56
  tables to Ruby classes. Strong conventions for associations, validations, aggregations,
57
57
  migrations, and testing come baked-in.
@@ -104,7 +104,9 @@ files:
104
104
  - lib/active_record/associations/preloader/through_association.rb
105
105
  - lib/active_record/associations/singular_association.rb
106
106
  - lib/active_record/associations/through_association.rb
107
+ - lib/active_record/attribute.rb
107
108
  - lib/active_record/attribute_assignment.rb
109
+ - lib/active_record/attribute_decorators.rb
108
110
  - lib/active_record/attribute_methods.rb
109
111
  - lib/active_record/attribute_methods/before_type_cast.rb
110
112
  - lib/active_record/attribute_methods/dirty.rb
@@ -114,9 +116,13 @@ files:
114
116
  - lib/active_record/attribute_methods/serialization.rb
115
117
  - lib/active_record/attribute_methods/time_zone_conversion.rb
116
118
  - lib/active_record/attribute_methods/write.rb
119
+ - lib/active_record/attribute_set.rb
120
+ - lib/active_record/attribute_set/builder.rb
121
+ - lib/active_record/attributes.rb
117
122
  - lib/active_record/autosave_association.rb
118
123
  - lib/active_record/base.rb
119
124
  - lib/active_record/callbacks.rb
125
+ - lib/active_record/coders/json.rb
120
126
  - lib/active_record/coders/yaml_column.rb
121
127
  - lib/active_record/connection_adapters/abstract/connection_pool.rb
122
128
  - lib/active_record/connection_adapters/abstract/database_limits.rb
@@ -136,12 +142,39 @@ files:
136
142
  - lib/active_record/connection_adapters/mysql2_adapter.rb
137
143
  - lib/active_record/connection_adapters/mysql_adapter.rb
138
144
  - lib/active_record/connection_adapters/postgresql/array_parser.rb
139
- - lib/active_record/connection_adapters/postgresql/cast.rb
145
+ - lib/active_record/connection_adapters/postgresql/column.rb
140
146
  - lib/active_record/connection_adapters/postgresql/database_statements.rb
141
147
  - lib/active_record/connection_adapters/postgresql/oid.rb
148
+ - lib/active_record/connection_adapters/postgresql/oid/array.rb
149
+ - lib/active_record/connection_adapters/postgresql/oid/bit.rb
150
+ - lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb
151
+ - lib/active_record/connection_adapters/postgresql/oid/bytea.rb
152
+ - lib/active_record/connection_adapters/postgresql/oid/cidr.rb
153
+ - lib/active_record/connection_adapters/postgresql/oid/date.rb
154
+ - lib/active_record/connection_adapters/postgresql/oid/date_time.rb
155
+ - lib/active_record/connection_adapters/postgresql/oid/decimal.rb
156
+ - lib/active_record/connection_adapters/postgresql/oid/enum.rb
157
+ - lib/active_record/connection_adapters/postgresql/oid/float.rb
158
+ - lib/active_record/connection_adapters/postgresql/oid/hstore.rb
159
+ - lib/active_record/connection_adapters/postgresql/oid/inet.rb
160
+ - lib/active_record/connection_adapters/postgresql/oid/infinity.rb
161
+ - lib/active_record/connection_adapters/postgresql/oid/integer.rb
162
+ - lib/active_record/connection_adapters/postgresql/oid/json.rb
163
+ - lib/active_record/connection_adapters/postgresql/oid/jsonb.rb
164
+ - lib/active_record/connection_adapters/postgresql/oid/money.rb
165
+ - lib/active_record/connection_adapters/postgresql/oid/point.rb
166
+ - lib/active_record/connection_adapters/postgresql/oid/range.rb
167
+ - lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb
168
+ - lib/active_record/connection_adapters/postgresql/oid/time.rb
169
+ - lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb
170
+ - lib/active_record/connection_adapters/postgresql/oid/uuid.rb
171
+ - lib/active_record/connection_adapters/postgresql/oid/vector.rb
172
+ - lib/active_record/connection_adapters/postgresql/oid/xml.rb
142
173
  - lib/active_record/connection_adapters/postgresql/quoting.rb
143
174
  - lib/active_record/connection_adapters/postgresql/referential_integrity.rb
175
+ - lib/active_record/connection_adapters/postgresql/schema_definitions.rb
144
176
  - lib/active_record/connection_adapters/postgresql/schema_statements.rb
177
+ - lib/active_record/connection_adapters/postgresql/utils.rb
145
178
  - lib/active_record/connection_adapters/postgresql_adapter.rb
146
179
  - lib/active_record/connection_adapters/schema_cache.rb
147
180
  - lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -212,6 +245,28 @@ files:
212
245
  - lib/active_record/timestamp.rb
213
246
  - lib/active_record/transactions.rb
214
247
  - lib/active_record/translation.rb
248
+ - lib/active_record/type.rb
249
+ - lib/active_record/type/big_integer.rb
250
+ - lib/active_record/type/binary.rb
251
+ - lib/active_record/type/boolean.rb
252
+ - lib/active_record/type/date.rb
253
+ - lib/active_record/type/date_time.rb
254
+ - lib/active_record/type/decimal.rb
255
+ - lib/active_record/type/decimal_without_scale.rb
256
+ - lib/active_record/type/decorator.rb
257
+ - lib/active_record/type/float.rb
258
+ - lib/active_record/type/hash_lookup_type_map.rb
259
+ - lib/active_record/type/integer.rb
260
+ - lib/active_record/type/mutable.rb
261
+ - lib/active_record/type/numeric.rb
262
+ - lib/active_record/type/serialized.rb
263
+ - lib/active_record/type/string.rb
264
+ - lib/active_record/type/text.rb
265
+ - lib/active_record/type/time.rb
266
+ - lib/active_record/type/time_value.rb
267
+ - lib/active_record/type/type_map.rb
268
+ - lib/active_record/type/unsigned_integer.rb
269
+ - lib/active_record/type/value.rb
215
270
  - lib/active_record/validations.rb
216
271
  - lib/active_record/validations/associated.rb
217
272
  - lib/active_record/validations/presence.rb
@@ -247,7 +302,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
302
  version: '0'
248
303
  requirements: []
249
304
  rubyforge_project:
250
- rubygems_version: 2.2.0
305
+ rubygems_version: 2.2.2
251
306
  signing_key:
252
307
  specification_version: 4
253
308
  summary: Object-relational mapper framework (part of Rails).
@@ -1,168 +0,0 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- class PostgreSQLColumn < Column
4
- module Cast
5
- def point_to_string(point)
6
- "(#{point[0]},#{point[1]})"
7
- end
8
-
9
- def string_to_point(string)
10
- if string[0] == '(' && string[-1] == ')'
11
- string = string[1...-1]
12
- end
13
- string.split(',').map{ |v| Float(v) }
14
- end
15
-
16
- def string_to_time(string)
17
- return string unless String === string
18
-
19
- case string
20
- when 'infinity'; Float::INFINITY
21
- when '-infinity'; -Float::INFINITY
22
- when / BC$/
23
- super("-" + string.sub(/ BC$/, ""))
24
- else
25
- super
26
- end
27
- end
28
-
29
- def string_to_bit(value)
30
- case value
31
- when /^0x/i
32
- value[2..-1].hex.to_s(2) # Hexadecimal notation
33
- else
34
- value # Bit-string notation
35
- end
36
- end
37
-
38
- def hstore_to_string(object, array_member = false)
39
- if Hash === object
40
- string = object.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(',')
41
- string = escape_hstore(string) if array_member
42
- string
43
- else
44
- object
45
- end
46
- end
47
-
48
- def string_to_hstore(string)
49
- if string.nil?
50
- nil
51
- elsif String === string
52
- Hash[string.scan(HstorePair).map { |k, v|
53
- v = v.upcase == 'NULL' ? nil : v.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
54
- k = k.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
55
- [k, v]
56
- }]
57
- else
58
- string
59
- end
60
- end
61
-
62
- def json_to_string(object)
63
- if Hash === object || Array === object
64
- ActiveSupport::JSON.encode(object)
65
- else
66
- object
67
- end
68
- end
69
-
70
- def array_to_string(value, column, adapter)
71
- casted_values = value.map do |val|
72
- if String === val
73
- if val == "NULL"
74
- "\"#{val}\""
75
- else
76
- quote_and_escape(adapter.type_cast(val, column, true))
77
- end
78
- else
79
- adapter.type_cast(val, column, true)
80
- end
81
- end
82
- "{#{casted_values.join(',')}}"
83
- end
84
-
85
- def range_to_string(object)
86
- from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
87
- to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
88
- "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
89
- end
90
-
91
- def string_to_json(string)
92
- if String === string
93
- ActiveSupport::JSON.decode(string)
94
- else
95
- string
96
- end
97
- end
98
-
99
- def string_to_cidr(string)
100
- if string.nil?
101
- nil
102
- elsif String === string
103
- begin
104
- IPAddr.new(string)
105
- rescue ArgumentError
106
- nil
107
- end
108
- else
109
- string
110
- end
111
- end
112
-
113
- def cidr_to_string(object)
114
- if IPAddr === object
115
- "#{object.to_s}/#{object.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
116
- else
117
- object
118
- end
119
- end
120
-
121
- def string_to_array(string, oid)
122
- parse_pg_array(string).map {|val| type_cast_array(oid, val)}
123
- end
124
-
125
- private
126
-
127
- HstorePair = begin
128
- quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
129
- unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
130
- /(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
131
- end
132
-
133
- def escape_hstore(value)
134
- if value.nil?
135
- 'NULL'
136
- else
137
- if value == ""
138
- '""'
139
- else
140
- '"%s"' % value.to_s.gsub(/(["\\])/, '\\\\\1')
141
- end
142
- end
143
- end
144
-
145
- ARRAY_ESCAPE = "\\" * 2 * 2 # escape the backslash twice for PG arrays
146
-
147
- def quote_and_escape(value)
148
- case value
149
- when "NULL", Numeric
150
- value
151
- else
152
- value = value.gsub(/\\/, ARRAY_ESCAPE)
153
- value.gsub!(/"/,"\\\"")
154
- "\"#{value}\""
155
- end
156
- end
157
-
158
- def type_cast_array(oid, value)
159
- if ::Array === value
160
- value.map {|item| type_cast_array(oid, item)}
161
- else
162
- oid.type_cast value
163
- end
164
- end
165
- end
166
- end
167
- end
168
- end