origin 0.0.0.alpha → 1.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/Rakefile +3 -0
  2. data/lib/origin.rb +3 -4
  3. data/lib/origin/extensions.rb +25 -0
  4. data/lib/origin/extensions/array.rb +153 -0
  5. data/lib/origin/extensions/big_decimal.rb +33 -0
  6. data/lib/origin/extensions/boolean.rb +30 -0
  7. data/lib/origin/extensions/date.rb +59 -0
  8. data/lib/origin/extensions/date_time.rb +44 -0
  9. data/lib/origin/extensions/hash.rb +180 -0
  10. data/lib/origin/extensions/nil_class.rb +82 -0
  11. data/lib/origin/extensions/numeric.rb +86 -0
  12. data/lib/origin/extensions/object.rb +182 -0
  13. data/lib/origin/extensions/range.rb +66 -0
  14. data/lib/origin/extensions/regexp.rb +41 -0
  15. data/lib/origin/extensions/set.rb +28 -0
  16. data/lib/origin/extensions/string.rb +100 -0
  17. data/lib/origin/extensions/symbol.rb +74 -0
  18. data/lib/origin/extensions/time.rb +44 -0
  19. data/lib/origin/extensions/time_with_zone.rb +50 -0
  20. data/lib/origin/forwardable.rb +57 -0
  21. data/lib/origin/key.rb +74 -0
  22. data/lib/origin/macroable.rb +23 -0
  23. data/lib/origin/mergeable.rb +226 -0
  24. data/lib/origin/optional.rb +314 -29
  25. data/lib/origin/options.rb +64 -1
  26. data/lib/origin/queryable.rb +55 -12
  27. data/lib/origin/selectable.rb +613 -0
  28. data/lib/origin/selector.rb +140 -1
  29. data/lib/origin/smash.rb +85 -0
  30. data/lib/origin/version.rb +1 -1
  31. metadata +94 -62
  32. data/lib/origin/ext.rb +0 -5
  33. data/lib/origin/ext/array.rb +0 -21
  34. data/lib/origin/ext/hash.rb +0 -38
  35. data/lib/origin/ext/nil.rb +0 -9
  36. data/lib/origin/ext/object.rb +0 -25
  37. data/lib/origin/optional/batch_size.rb +0 -11
  38. data/lib/origin/optional/hint.rb +0 -15
  39. data/lib/origin/optional/limit.rb +0 -11
  40. data/lib/origin/optional/max_scan.rb +0 -11
  41. data/lib/origin/optional/no_timeout.rb +0 -11
  42. data/lib/origin/optional/only.rb +0 -15
  43. data/lib/origin/optional/read.rb +0 -11
  44. data/lib/origin/optional/return_key.rb +0 -11
  45. data/lib/origin/optional/show_disk_loc.rb +0 -11
  46. data/lib/origin/optional/skip.rb +0 -11
  47. data/lib/origin/optional/slice.rb +0 -17
  48. data/lib/origin/optional/snapshot.rb +0 -13
  49. data/lib/origin/optional/transformer.rb +0 -11
  50. data/lib/origin/optional/without.rb +0 -15
  51. data/lib/origin/selection.rb +0 -59
  52. data/lib/origin/selection/all.rb +0 -18
  53. data/lib/origin/selection/and.rb +0 -11
  54. data/lib/origin/selection/between.rb +0 -16
  55. data/lib/origin/selection/elem_match.rb +0 -18
  56. data/lib/origin/selection/exists.rb +0 -18
  57. data/lib/origin/selection/gt.rb +0 -18
  58. data/lib/origin/selection/gte.rb +0 -18
  59. data/lib/origin/selection/in.rb +0 -18
  60. data/lib/origin/selection/key.rb +0 -20
  61. data/lib/origin/selection/lt.rb +0 -18
  62. data/lib/origin/selection/lte.rb +0 -18
  63. data/lib/origin/selection/max_distance.rb +0 -11
  64. data/lib/origin/selection/mod.rb +0 -18
  65. data/lib/origin/selection/ne.rb +0 -18
  66. data/lib/origin/selection/near.rb +0 -18
  67. data/lib/origin/selection/near_sphere.rb +0 -18
  68. data/lib/origin/selection/nin.rb +0 -18
  69. data/lib/origin/selection/nor.rb +0 -11
  70. data/lib/origin/selection/or.rb +0 -11
  71. data/lib/origin/selection/size.rb +0 -18
  72. data/lib/origin/selection/strategies.rb +0 -40
  73. data/lib/origin/selection/strategies/add.rb +0 -18
  74. data/lib/origin/selection/strategies/expanded.rb +0 -15
  75. data/lib/origin/selection/strategies/intersect.rb +0 -22
  76. data/lib/origin/selection/strategies/multi.rb +0 -21
  77. data/lib/origin/selection/strategies/override.rb +0 -19
  78. data/lib/origin/selection/strategies/union.rb +0 -22
  79. data/lib/origin/selection/type.rb +0 -18
  80. data/lib/origin/selection/where.rb +0 -29
  81. data/lib/origin/selection/within_box.rb +0 -18
  82. data/lib/origin/selection/within_circle.rb +0 -18
  83. data/lib/origin/selection/within_spherical_circle.rb +0 -18
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Extensions
4
+
5
+ # This module contains additional nil behaviour.
6
+ module NilClass
7
+
8
+ # Add this object to nil.
9
+ #
10
+ # @example Add the object to a nil value.
11
+ # nil.__add__([ 1, 2, 3 ])
12
+ #
13
+ # @param [ Object ] object The object to add.
14
+ #
15
+ # @return [ Object ] The provided object.
16
+ #
17
+ # @since 1.0.0
18
+ def __add__(object); object; end
19
+
20
+ # Add this object to nil.
21
+ #
22
+ # @example Add the object to a nil value.
23
+ # nil.__expanded__([ 1, 2, 3 ])
24
+ #
25
+ # @param [ Object ] object The object to expanded.
26
+ #
27
+ # @return [ Object ] The provided object.
28
+ #
29
+ # @since 1.0.0
30
+ def __expanded__(object); object; end
31
+
32
+ # Evolve the nil into a date or time.
33
+ #
34
+ # @example Evolve the nil.
35
+ # nil.__evolve_time__
36
+ #
37
+ # @return [ nil ] nil.
38
+ #
39
+ # @since 1.0.0
40
+ def __evolve_time__; self; end
41
+ alias :__evolve_date__ :__evolve_time__
42
+
43
+ # Add this object to nil.
44
+ #
45
+ # @example Add the object to a nil value.
46
+ # nil.__intersect__([ 1, 2, 3 ])
47
+ #
48
+ # @param [ Object ] object The object to intersect.
49
+ #
50
+ # @return [ Object ] The provided object.
51
+ #
52
+ # @since 1.0.0
53
+ def __intersect__(object); object; end
54
+
55
+ # Add this object to nil.
56
+ #
57
+ # @example Add the object to a nil value.
58
+ # nil.__override__([ 1, 2, 3 ])
59
+ #
60
+ # @param [ Object ] object The object to override.
61
+ #
62
+ # @return [ Object ] The provided object.
63
+ #
64
+ # @since 1.0.0
65
+ def __override__(object); object; end
66
+
67
+ # Add this object to nil.
68
+ #
69
+ # @example Add the object to a nil value.
70
+ # nil.__union__([ 1, 2, 3 ])
71
+ #
72
+ # @param [ Object ] object The object to union.
73
+ #
74
+ # @return [ Object ] The provided object.
75
+ #
76
+ # @since 1.0.0
77
+ def __union__(object); object; end
78
+ end
79
+ end
80
+ end
81
+
82
+ ::NilClass.__send__(:include, Origin::Extensions::NilClass)
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Extensions
4
+
5
+ # This module contains additional numeric behaviour.
6
+ module Numeric
7
+
8
+ # Evolve the numeric value into a mongo friendly date, aka UTC time at
9
+ # midnight.
10
+ #
11
+ # @example Evolve to a date.
12
+ # 125214512412.1123.__evolve_date__
13
+ #
14
+ # @return [ Time ] The time representation at UTC midnight.
15
+ #
16
+ # @since 1.0.0
17
+ def __evolve_date__
18
+ time = ::Time.at(self)
19
+ ::Time.utc(time.year, time.month, time.day, 0, 0, 0, 0)
20
+ end
21
+
22
+ # Evolve the numeric value into a mongo friendly time.
23
+ #
24
+ # @example Evolve to a time.
25
+ # 125214512412.1123.__evolve_time__
26
+ #
27
+ # @return [ Time ] The time representation.
28
+ #
29
+ # @since 1.0.0
30
+ def __evolve_time__
31
+ ::Time.at(self).utc
32
+ end
33
+
34
+ # Get the integer as a sort direction.
35
+ #
36
+ # @example Get the integer as a sort direction.
37
+ # 1.to_direction
38
+ #
39
+ # @return [ Integer ] self.
40
+ #
41
+ # @since 1.0.0
42
+ def to_direction; self; end
43
+
44
+ module ClassMethods
45
+
46
+ # Get the object as a numeric.
47
+ #
48
+ # @api private
49
+ #
50
+ # @example Get the object as numeric.
51
+ # Object.__numeric__("1.442")
52
+ #
53
+ # @param [ Object ] object The object to convert.
54
+ #
55
+ # @return [ Object ] The converted number.
56
+ #
57
+ # @since 1.0.0
58
+ def __numeric__(object)
59
+ object.to_s =~ /(^[-+]?[0-9]+$)|(\.0+)$/ ? object.to_i : Float(object)
60
+ end
61
+
62
+ # Evolve the object to an integer.
63
+ #
64
+ # @example Evolve to integers.
65
+ # Integer.evolve("1")
66
+ #
67
+ # @param [ Object ] object The object to evolve.
68
+ #
69
+ # @return [ Integer ] The evolved object.
70
+ #
71
+ # @since 1.0.0
72
+ def evolve(object)
73
+ __evolve__(object) do |obj|
74
+ __numeric__(obj) rescue obj
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ ::Integer.__send__(:include, Origin::Extensions::Numeric)
83
+ ::Integer.__send__(:extend, Origin::Extensions::Numeric::ClassMethods)
84
+
85
+ ::Float.__send__(:include, Origin::Extensions::Numeric)
86
+ ::Float.__send__(:extend, Origin::Extensions::Numeric::ClassMethods)
@@ -0,0 +1,182 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Extensions
4
+
5
+ # This module contains additional object behaviour.
6
+ module Object
7
+
8
+ # Combine the two objects using the add strategy.
9
+ #
10
+ # @example Add the object to the array.
11
+ # [ 1, 2, 3 ].__add__(4)
12
+ #
13
+ # @param [ Object ] object The object to add.
14
+ #
15
+ # @return [ Object ] The result of the add.
16
+ #
17
+ # @since 1.0.0
18
+ def __add__(object)
19
+ (object == self) ? self : [ self, object ].flatten.uniq
20
+ end
21
+
22
+ # Merge this object into the provided array.
23
+ #
24
+ # @example Merge the object into the array.
25
+ # 4.__add_from_array__([ 1, 2 ])
26
+ #
27
+ # @param [ Array ] value The array to add to.
28
+ #
29
+ # @return [ Array ] The merged object.
30
+ #
31
+ # @since 1.0.0
32
+ def __add_from_array__(array)
33
+ array.concat(Array(self)).uniq
34
+ end
35
+
36
+ # Combine the two objects using the intersect strategy.
37
+ #
38
+ # @example Add the object to the array.
39
+ # [ 1, 2, 3 ].__intersect__(4)
40
+ #
41
+ # @param [ Object ] object The object to intersect.
42
+ #
43
+ # @return [ Array ] The result of the intersect.
44
+ #
45
+ # @since 1.0.0
46
+ def __intersect__(object)
47
+ object.__intersect_from_object__(self)
48
+ end
49
+
50
+ # Merge this object into the provided array.
51
+ #
52
+ # @example Merge the object into the array.
53
+ # 4.__intersect_from_array__([ 1, 2 ])
54
+ #
55
+ # @param [ Array ] value The array to intersect to.
56
+ #
57
+ # @return [ Array ] The merged object.
58
+ #
59
+ # @since 1.0.0
60
+ def __intersect_from_array__(array)
61
+ array & Array(self)
62
+ end
63
+
64
+ # Merge this object into the provided array.
65
+ #
66
+ # @example Merge the object into the array.
67
+ # 4.__intersect_from_object__([ 1, 2 ])
68
+ #
69
+ # @param [ Object ] value The value to intersect to.
70
+ #
71
+ # @return [ Array ] The merged object.
72
+ #
73
+ # @since 1.0.0
74
+ def __intersect_from_object__(object)
75
+ Array(object) & Array(self)
76
+ end
77
+
78
+ # Combine the two objects using the union strategy.
79
+ #
80
+ # @example Add the object to the array.
81
+ # [ 1, 2, 3 ].__union__(4)
82
+ #
83
+ # @param [ Object ] object The object to union.
84
+ #
85
+ # @return [ Array ] The result of the union.
86
+ #
87
+ # @since 1.0.0
88
+ def __union__(object)
89
+ object.__union_from_object__(self)
90
+ end
91
+
92
+ # Merge this object into the provided array.
93
+ #
94
+ # @example Merge the object into the array.
95
+ # 4.__union_from_object__([ 1, 2 ])
96
+ #
97
+ # @param [ Object ] value The value to union to.
98
+ #
99
+ # @return [ Array ] The merged object.
100
+ #
101
+ # @since 1.0.0
102
+ def __union_from_object__(object)
103
+ (Array(object) + Array(self)).uniq
104
+ end
105
+
106
+ # Deep copy the object. This is for API compatibility, but needs to be
107
+ # overridden.
108
+ #
109
+ # @example Deep copy the object.
110
+ # 1.__deep_copy__
111
+ #
112
+ # @return [ Object ] self.
113
+ #
114
+ # @since 1.0.0
115
+ def __deep_copy__; self; end
116
+
117
+ # Get the object as an array.
118
+ #
119
+ # @example Get the object as an array.
120
+ # 4.__array__
121
+ #
122
+ # @return [ Array ] The wrapped object.
123
+ #
124
+ # @since 1.0.0
125
+ def __array__
126
+ [ self ]
127
+ end
128
+
129
+ def regexp?
130
+ false
131
+ end
132
+
133
+ module ClassMethods
134
+
135
+ # Evolve the object.
136
+ #
137
+ # @note This is here for API compatibility.
138
+ #
139
+ # @example Evolve an object.
140
+ # Object.evolve("test")
141
+ #
142
+ # @return [ Object ] The provided object.
143
+ #
144
+ # @since 1.0.0
145
+ def evolve(object)
146
+ object
147
+ end
148
+
149
+ private
150
+
151
+ # Evolve the object.
152
+ #
153
+ # @api private
154
+ #
155
+ # @todo Durran refactor out case statement.
156
+ #
157
+ # @example Evolve an object and yield.
158
+ # Object.evolve("test") do |obj|
159
+ # obj.to_s
160
+ # end
161
+ #
162
+ # @return [ Object ] The evolved object.
163
+ #
164
+ # @since 1.0.0
165
+ def __evolve__(object)
166
+ return nil if object.nil?
167
+ case object
168
+ when ::Array
169
+ object.map!{ |obj| evolve(obj) }
170
+ when ::Range
171
+ { "$gte" => evolve(object.min), "$lte" => evolve(object.max) }
172
+ else
173
+ yield(object)
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ ::Object.__send__(:include, Origin::Extensions::Object)
182
+ ::Object.__send__(:extend, Origin::Extensions::Object::ClassMethods)
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Extensions
4
+
5
+ # This module contains additional range behaviour.
6
+ module Range
7
+
8
+ # Get the range as an array.
9
+ #
10
+ # @example Get the range as an array.
11
+ # 1...3.__array__
12
+ #
13
+ # @return [ Array ] The range as an array.
14
+ #
15
+ # @since 1.0.0
16
+ def __array__
17
+ to_a
18
+ end
19
+
20
+ # Convert the range to a min/max mongo friendly query for dates.
21
+ #
22
+ # @example Evolve the range.
23
+ # (11231312..213123131).__evolve_date__
24
+ #
25
+ # @return [ Hash ] The min/max range query with times at midnight.
26
+ #
27
+ # @since 1.0.0
28
+ def __evolve_date__
29
+ { "$gte" => min.__evolve_date__, "$lte" => max.__evolve_date__ }
30
+ end
31
+
32
+ # Convert the range to a min/max mongo friendly query for times.
33
+ #
34
+ # @example Evolve the range.
35
+ # (11231312..213123131).__evolve_date__
36
+ #
37
+ # @return [ Hash ] The min/max range query with times.
38
+ #
39
+ # @since 1.0.0
40
+ def __evolve_time__
41
+ { "$gte" => min.__evolve_time__, "$lte" => max.__evolve_time__ }
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ # Evolve the range. This will transform it into a $gte/$lte selection.
47
+ #
48
+ # @example Evolve the range.
49
+ # Range.evolve(1..3)
50
+ #
51
+ # @param [ Range ] object The range to evolve.
52
+ #
53
+ # @return [ Hash ] The range as a gte/lte criteria.
54
+ #
55
+ # @since 1.0.0
56
+ def evolve(object)
57
+ return object unless object.is_a?(::Range)
58
+ { "$gte" => object.min, "$lte" => object.max }
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ ::Range.__send__(:include, Origin::Extensions::Range)
66
+ ::Range.__send__(:extend, Origin::Extensions::Range::ClassMethods)
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Extensions
4
+
5
+ # This module contains additional regex behaviour.
6
+ module Regexp
7
+
8
+ # Is the object a regexp?
9
+ #
10
+ # @example Is the object a regex?
11
+ # /^[123]/.regexp?
12
+ #
13
+ # @return [ true ] Always true.
14
+ #
15
+ # @since 1.0.0
16
+ def regexp?; true; end
17
+
18
+ module ClassMethods
19
+
20
+ # Evolve the object into a regex.
21
+ #
22
+ # @example Evolve the object to a regex.
23
+ # Regexp.evolve("^[123]")
24
+ #
25
+ # @param [ Regexp, String ] object The object to evolve.
26
+ #
27
+ # @return [ Regexp ] The evolved regex.
28
+ #
29
+ # @since 1.0.0
30
+ def evolve(object)
31
+ __evolve__(object) do |obj|
32
+ ::Regexp.new(obj)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ ::Regexp.__send__(:include, Origin::Extensions::Regexp)
41
+ ::Regexp.__send__(:extend, Origin::Extensions::Regexp::ClassMethods)