-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathobject.rb
More file actions
173 lines (156 loc) · 5.05 KB
/
Copy pathobject.rb
File metadata and controls
173 lines (156 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# frozen_string_literal: true
# rubocop:todo all
module Mongoid
class Criteria
module Queryable
module Extensions
# Adds query type-casting behavior to Object class.
module Object
# Combine the two objects using the add strategy.
#
# @example Add the object to the array.
# [ 1, 2, 3 ].__add__(4)
#
# @param [ Object ] object The object to add.
#
# @return [ Object ] The result of the add.
def __add__(object)
(object == self) ? self : [ self, object ].flatten.uniq
end
# Merge this object into the provided array.
#
# @example Merge the object into the array.
# 4.__add_from_array__([ 1, 2 ])
#
# @param [ Array ] array The array to add to.
#
# @return [ Array ] The merged object.
def __add_from_array__(array)
array.concat(Array(self)).uniq
end
# Combine the two objects using the intersect strategy.
#
# @example Add the object to the array.
# [ 1, 2, 3 ].__intersect__(4)
#
# @param [ Object ] object The object to intersect.
#
# @return [ Array ] The result of the intersect.
def __intersect__(object)
object.__intersect_from_object__(self)
end
# Merge this object into the provided array.
#
# @example Merge the object into the array.
# 4.__intersect_from_array__([ 1, 2 ])
#
# @param [ Array ] array The array to intersect to.
#
# @return [ Array ] The merged object.
def __intersect_from_array__(array)
array & Array(self)
end
# Merge this object into the provided array.
#
# @example Merge the object into the array.
# 4.__intersect_from_object__([ 1, 2 ])
#
# @param [ Object ] object The value to intersect to.
#
# @return [ Array ] The merged object.
def __intersect_from_object__(object)
Array(object) & Array(self)
end
# Combine the two objects using the union strategy.
#
# @example Add the object to the array.
# [ 1, 2, 3 ].__union__(4)
#
# @param [ Object ] object The object to union.
#
# @return [ Array ] The result of the union.
def __union__(object)
object.__union_from_object__(self)
end
# Merge this object into the provided array.
#
# @example Merge the object into the array.
# 4.__union_from_object__([ 1, 2 ])
#
# @param [ Object ] object The value to union to.
#
# @return [ Array ] The merged object.
def __union_from_object__(object)
(Array(object) + Array(self)).uniq
end
# Deep copy the object. This is for API compatibility, but needs to be
# overridden.
#
# @example Deep copy the object.
# 1.__deep_copy__
#
# @return [ Object ] self.
def __deep_copy__; self; end
# Get the object as an array.
#
# @example Get the object as an array.
# 4.__array__
#
# @return [ Array ] The wrapped object.
def __array__
[ self ]
end
# Get the object as expanded.
#
# @example Get the object expanded.
# obj.__expand_complex__
#
# @return [ Object ] self.
def __expand_complex__
self
end
module ClassMethods
# Evolve the object.
#
# @note This is here for API compatibility.
#
# @example Evolve an object.
# Object.evolve("test")
#
# @return [ Object ] The provided object.
def evolve(object)
object
end
private
# Evolve the object.
#
# @api private
#
# @todo Durran refactor out case statement.
#
# @example Evolve an object and yield.
# Object.evolve("test") do |obj|
# obj.to_s
# end
#
# @return [ Object ] The evolved object.
def __evolve__(object)
return nil if object.nil?
case object
when ::Array
object.map{ |obj| evolve(obj) }
when ::Range
object.__evolve_range__
else
res = yield(object)
res.nil? ? object : res
end
end
end
end
end
end
end
end
::Object.__send__(:include, Mongoid::Criteria::Queryable::Extensions::Object)
::Object.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Object::ClassMethods)