origin 0.0.0.alpha

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.
Files changed (60) hide show
  1. data/Rakefile +32 -0
  2. data/lib/origin.rb +5 -0
  3. data/lib/origin/ext.rb +5 -0
  4. data/lib/origin/ext/array.rb +21 -0
  5. data/lib/origin/ext/hash.rb +38 -0
  6. data/lib/origin/ext/nil.rb +9 -0
  7. data/lib/origin/ext/object.rb +25 -0
  8. data/lib/origin/optional.rb +35 -0
  9. data/lib/origin/optional/batch_size.rb +11 -0
  10. data/lib/origin/optional/hint.rb +15 -0
  11. data/lib/origin/optional/limit.rb +11 -0
  12. data/lib/origin/optional/max_scan.rb +11 -0
  13. data/lib/origin/optional/no_timeout.rb +11 -0
  14. data/lib/origin/optional/only.rb +15 -0
  15. data/lib/origin/optional/read.rb +11 -0
  16. data/lib/origin/optional/return_key.rb +11 -0
  17. data/lib/origin/optional/show_disk_loc.rb +11 -0
  18. data/lib/origin/optional/skip.rb +11 -0
  19. data/lib/origin/optional/slice.rb +17 -0
  20. data/lib/origin/optional/snapshot.rb +13 -0
  21. data/lib/origin/optional/transformer.rb +11 -0
  22. data/lib/origin/optional/without.rb +15 -0
  23. data/lib/origin/options.rb +5 -0
  24. data/lib/origin/queryable.rb +31 -0
  25. data/lib/origin/selection.rb +59 -0
  26. data/lib/origin/selection/all.rb +18 -0
  27. data/lib/origin/selection/and.rb +11 -0
  28. data/lib/origin/selection/between.rb +16 -0
  29. data/lib/origin/selection/elem_match.rb +18 -0
  30. data/lib/origin/selection/exists.rb +18 -0
  31. data/lib/origin/selection/gt.rb +18 -0
  32. data/lib/origin/selection/gte.rb +18 -0
  33. data/lib/origin/selection/in.rb +18 -0
  34. data/lib/origin/selection/key.rb +20 -0
  35. data/lib/origin/selection/lt.rb +18 -0
  36. data/lib/origin/selection/lte.rb +18 -0
  37. data/lib/origin/selection/max_distance.rb +11 -0
  38. data/lib/origin/selection/mod.rb +18 -0
  39. data/lib/origin/selection/ne.rb +18 -0
  40. data/lib/origin/selection/near.rb +18 -0
  41. data/lib/origin/selection/near_sphere.rb +18 -0
  42. data/lib/origin/selection/nin.rb +18 -0
  43. data/lib/origin/selection/nor.rb +11 -0
  44. data/lib/origin/selection/or.rb +11 -0
  45. data/lib/origin/selection/size.rb +18 -0
  46. data/lib/origin/selection/strategies.rb +40 -0
  47. data/lib/origin/selection/strategies/add.rb +18 -0
  48. data/lib/origin/selection/strategies/expanded.rb +15 -0
  49. data/lib/origin/selection/strategies/intersect.rb +22 -0
  50. data/lib/origin/selection/strategies/multi.rb +21 -0
  51. data/lib/origin/selection/strategies/override.rb +19 -0
  52. data/lib/origin/selection/strategies/union.rb +22 -0
  53. data/lib/origin/selection/type.rb +18 -0
  54. data/lib/origin/selection/where.rb +29 -0
  55. data/lib/origin/selection/within_box.rb +18 -0
  56. data/lib/origin/selection/within_circle.rb +18 -0
  57. data/lib/origin/selection/within_spherical_circle.rb +18 -0
  58. data/lib/origin/selector.rb +5 -0
  59. data/lib/origin/version.rb +4 -0
  60. metadata +126 -0
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require "rake"
2
+ require "rspec"
3
+ require "rspec/core/rake_task"
4
+
5
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
6
+ require "origin/version"
7
+
8
+ task :gem => :build
9
+ task :build do
10
+ system "gem build origin.gemspec"
11
+ end
12
+
13
+ task :install => :build do
14
+ system "sudo gem install origin-#{Origin::VERSION}.gem"
15
+ end
16
+
17
+ task :release => :build do
18
+ system "git tag -a v#{Origin::VERSION} -m 'Tagging #{Origin::VERSION}'"
19
+ system "git push --tags"
20
+ system "gem push origin-#{Origin::VERSION}.gem"
21
+ end
22
+
23
+ RSpec::Core::RakeTask.new("spec") do |spec|
24
+ spec.pattern = "spec/**/*_spec.rb"
25
+ end
26
+
27
+ RSpec::Core::RakeTask.new("spec:progress") do |spec|
28
+ spec.rspec_opts = %w(--format progress)
29
+ spec.pattern = "spec/**/*_spec.rb"
30
+ end
31
+
32
+ task :default => :spec
data/lib/origin.rb ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ autoload :Queryable, "origin/queryable"
4
+ autoload :Version, "origin/version"
5
+ end
data/lib/origin/ext.rb ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require "origin/ext/array"
3
+ require "origin/ext/hash"
4
+ require "origin/ext/nil"
5
+ require "origin/ext/object"
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ class Array
3
+
4
+ def _add(object)
5
+ if object.is_a?(Hash)
6
+ object[object.keys.first] = _add(object.values.first)
7
+ object
8
+ else
9
+ concat(Array(object)).uniq
10
+ end
11
+ end
12
+
13
+ def _intersect(object)
14
+ if object.is_a?(Hash)
15
+ object[object.keys.first] = _intersect(object.values.first)
16
+ object
17
+ else
18
+ self & Array(object)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ class Hash
3
+
4
+ def _add(object)
5
+ apply_strategy(:_add, object)
6
+ end
7
+
8
+ def _intersect(object)
9
+ apply_strategy(:_intersect, object)
10
+ end
11
+
12
+ def _union(object)
13
+ apply_strategy(:_union, object)
14
+ end
15
+
16
+ def deep_copy
17
+ inject({}) do |copy, (key, value)|
18
+ copy.tap do |clone|
19
+ case value
20
+ when Array, String then clone.store(key, value.dup)
21
+ when Hash then clone.store(key, value.deep_copy)
22
+ else
23
+ clone.store(key, value)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def apply_strategy(strategy, object)
32
+ tap do |hash|
33
+ object.each_pair do |key, value|
34
+ hash.store(key, hash[key].send(strategy, value))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ class NilClass
3
+
4
+ def _add(object); object end
5
+
6
+ def _intersect(object); object end
7
+
8
+ def _union(object); object end
9
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ class Object
3
+
4
+ def _add(object)
5
+ object == self ? self : [ self, object ].flatten.uniq
6
+ end
7
+
8
+ def _intersect(object)
9
+ if object.is_a?(Hash)
10
+ object[object.keys.first] = _intersect(object.values.first)
11
+ object
12
+ else
13
+ Array(self) & Array(object)
14
+ end
15
+ end
16
+
17
+ def _union(object)
18
+ if object.is_a?(Hash)
19
+ object[object.keys.first] = _union(object.values.first)
20
+ object
21
+ else
22
+ (Array(self) + Array(object)).uniq
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+
5
+ autoload :BatchSize, "origin/optional/batch_size"
6
+ autoload :Hint, "origin/optional/hint"
7
+ autoload :Limit, "origin/optional/limit"
8
+ autoload :MaxScan, "origin/optional/max_scan"
9
+ autoload :NoTimeout, "origin/optional/no_timeout"
10
+ autoload :Only, "origin/optional/only"
11
+ autoload :Read, "origin/optional/read"
12
+ autoload :ReturnKey, "origin/optional/return_key"
13
+ autoload :ShowDiskLoc, "origin/optional/show_disk_loc"
14
+ autoload :Skip, "origin/optional/skip"
15
+ autoload :Slice, "origin/optional/slice"
16
+ autoload :Snapshot, "origin/optional/snapshot"
17
+ autoload :Transformer, "origin/optional/transformer"
18
+ autoload :Without, "origin/optional/without"
19
+
20
+ include BatchSize
21
+ include Hint
22
+ include Limit
23
+ include MaxScan
24
+ include NoTimeout
25
+ include Only
26
+ include Read
27
+ include ReturnKey
28
+ include ShowDiskLoc
29
+ include Skip
30
+ include Slice
31
+ include Snapshot
32
+ include Transformer
33
+ include Without
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module BatchSize
5
+
6
+ def batch_size(value = nil)
7
+ option(value) { |options| options.store(:batch_size, value) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Hint
5
+
6
+ def hint(*args)
7
+ option(*args) do |options|
8
+ options.store(
9
+ :hint, args.inject({}){ |sub, field| sub.tap { sub[field] = 1 }}
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Limit
5
+
6
+ def limit(value = nil)
7
+ option(value) { |options| options.store(:limit, value) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module MaxScan
5
+
6
+ def max_scan(value = nil)
7
+ option(value) { |options| options.store(:max_scan, value) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module NoTimeout
5
+
6
+ def no_timeout
7
+ clone.tap { |query| query.options.store(:timeout, false) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Only
5
+
6
+ def only(*args)
7
+ option(*args) do |options|
8
+ options.store(
9
+ :fields, args.inject({}){ |sub, field| sub.tap { sub[field] = 1 }}
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Read
5
+
6
+ def read(node = nil)
7
+ option(node) { |options| options.store(:read, node) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module ReturnKey
5
+
6
+ def return_key
7
+ clone.tap { |query| query.options.store(:return_key, true) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module ShowDiskLoc
5
+
6
+ def show_disk_loc
7
+ clone.tap { |query| query.options.store(:show_disk_loc, true) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Skip
5
+
6
+ def skip(value = nil)
7
+ option(value) { |options| options.store(:skip, value) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Slice
5
+
6
+ def slice(criterion = nil)
7
+ option(criterion) do |options|
8
+ options._union(
9
+ :fields => criterion.inject({}) do |option, (field, val)|
10
+ option.tap { |opt| opt.store(field, { "$slice" => val }) }
11
+ end
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Snapshot
5
+
6
+ def snapshot
7
+ clone.tap do |query|
8
+ query.options.store(:snapshot, true)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Transformer
5
+
6
+ def transformer(block = nil)
7
+ option(block) { |options| options.store(:transformer, block) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+ module Without
5
+
6
+ def without(*args)
7
+ option(*args) do |options|
8
+ options.store(
9
+ :fields, args.inject({}){ |sub, field| sub.tap { sub[field] = -1 }}
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ class Options < Hash
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require "origin/ext"
3
+
4
+ module Origin
5
+
6
+ autoload :Optional, "origin/optional"
7
+ autoload :Options, "origin/options"
8
+ autoload :Selection, "origin/selection"
9
+ autoload :Selector, "origin/selector"
10
+
11
+ module Queryable
12
+ include Optional
13
+ include Selection
14
+
15
+ attr_reader :options, :selector
16
+
17
+ def ==(other)
18
+ return false unless other.is_a?(Query)
19
+ selector == other.selector && options == other.options
20
+ end
21
+
22
+ def initialize
23
+ @options, @selector = Options.new, Selector.new
24
+ yield(self) if block_given?
25
+ end
26
+
27
+ def initialize_copy(other)
28
+ @options, @selector = other.options.deep_copy, other.selector.deep_copy
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+
5
+ autoload :All, "origin/selection/all"
6
+ autoload :And, "origin/selection/and"
7
+ autoload :Between, "origin/selection/between"
8
+ autoload :ElemMatch, "origin/selection/elem_match"
9
+ autoload :Exists, "origin/selection/exists"
10
+ autoload :Gt, "origin/selection/gt"
11
+ autoload :Gte, "origin/selection/gte"
12
+ autoload :In, "origin/selection/in"
13
+ autoload :Key, "origin/selection/key"
14
+ autoload :Lt, "origin/selection/lt"
15
+ autoload :Lte, "origin/selection/lte"
16
+ autoload :MaxDistance, "origin/selection/max_distance"
17
+ autoload :Mod, "origin/selection/mod"
18
+ autoload :Ne, "origin/selection/ne"
19
+ autoload :Near, "origin/selection/near"
20
+ autoload :NearSphere, "origin/selection/near_sphere"
21
+ autoload :Nin, "origin/selection/nin"
22
+ autoload :Nor, "origin/selection/nor"
23
+ autoload :Or, "origin/selection/or"
24
+ autoload :Size, "origin/selection/size"
25
+ autoload :Strategies, "origin/selection/strategies"
26
+ autoload :Type, "origin/selection/type"
27
+ autoload :Where, "origin/selection/where"
28
+ autoload :WithinBox, "origin/selection/within_box"
29
+ autoload :WithinCircle, "origin/selection/within_circle"
30
+ autoload :WithinSphericalCircle, "origin/selection/within_spherical_circle"
31
+
32
+ include All
33
+ include And
34
+ include Between
35
+ include ElemMatch
36
+ include Exists
37
+ include Gt
38
+ include Gte
39
+ include In
40
+ include Lt
41
+ include Lte
42
+ include MaxDistance
43
+ include Mod
44
+ include Ne
45
+ include Near
46
+ include NearSphere
47
+ include Nin
48
+ include Nor
49
+ include Or
50
+ include Size
51
+ include Strategies
52
+ include Type
53
+ include Where
54
+ include WithinBox
55
+ include WithinCircle
56
+ #include WithinPolygon
57
+ include WithinSphericalCircle
58
+ end
59
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module All
5
+
6
+ def all(criterion = nil)
7
+ send(strategy || :_override, criterion, "$all")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def all
13
+ Key.new(self, "$all")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module And
5
+
6
+ def and(*criterion)
7
+ multi!(criterion, "$and")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Between
5
+
6
+ def between(criterion = nil)
7
+ selection(criterion) do |selector, field, value|
8
+ selector.store(
9
+ field,
10
+ { "$gte" => value.min, "$lte" => value.max }
11
+ )
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module ElemMatch
5
+
6
+ def elem_match(criterion = nil)
7
+ _override(criterion, "$elemMatch")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def elem_match
13
+ Key.new(self, "$elemMatch")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Exists
5
+
6
+ def exists(criterion = nil)
7
+ _override(criterion, "$exists")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def exists
13
+ Key.new(self, "$exists")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Gt
5
+
6
+ def gt(criterion = nil)
7
+ _override(criterion, "$gt")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def gt
13
+ Key.new(self, "$gt")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Gte
5
+
6
+ def gte(criterion = nil)
7
+ _override(criterion, "$gte")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def gte
13
+ Key.new(self, "$gte")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module In
5
+
6
+ def in(criterion = nil)
7
+ send(strategy || :_intersect, criterion, "$in")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def in
13
+ Key.new(self, "$in")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ class Key
5
+ attr_reader :name, :operator, :expanded
6
+
7
+ def initialize(name, operator, expanded = nil)
8
+ @name, @operator, @expanded = name, operator, expanded
9
+ end
10
+
11
+ def specify(value)
12
+ if expanded
13
+ { name => { operator => { expanded => value }}}
14
+ else
15
+ { name => { operator => value }}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Lt
5
+
6
+ def lt(criterion = nil)
7
+ _override(criterion, "$lt")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def lt
13
+ Key.new(self, "$lt")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Lte
5
+
6
+ def lte(criterion = nil)
7
+ _override(criterion, "$lte")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def lte
13
+ Key.new(self, "$lte")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module MaxDistance
5
+
6
+ def max_distance(criterion = nil)
7
+ _add(criterion, "$maxDistance")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Mod
5
+
6
+ def mod(criterion = nil)
7
+ _override(criterion, "$mod")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def mod
13
+ Key.new(self, "$mod")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Ne
5
+
6
+ def ne(criterion = nil)
7
+ _override(criterion, "$ne")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def ne
13
+ Key.new(self, "$ne")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Near
5
+
6
+ def near(criterion = nil)
7
+ _override(criterion, "$near")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def near
13
+ Key.new(self, "$near")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module NearSphere
5
+
6
+ def near_sphere(criterion = nil)
7
+ _override(criterion, "$nearSphere")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def near_sphere
13
+ Key.new(self, "$nearSphere")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Nin
5
+
6
+ def nin(criterion = nil)
7
+ send(strategy || :_intersect, criterion, "$nin")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def nin
13
+ Key.new(self, "$nin")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Nor
5
+
6
+ def nor(*criterion)
7
+ multi!(criterion, "$nor")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Or
5
+
6
+ def or(*criterion)
7
+ multi!(criterion, "$or")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Size
5
+
6
+ def with_size(criterion = nil)
7
+ _override(criterion, "$size")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def with_size
13
+ Key.new(self, "$size")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ require "origin/selection/strategies/add"
3
+ require "origin/selection/strategies/expanded"
4
+ require "origin/selection/strategies/intersect"
5
+ require "origin/selection/strategies/multi"
6
+ require "origin/selection/strategies/override"
7
+ require "origin/selection/strategies/union"
8
+
9
+ module Origin
10
+ module Selection
11
+ module Strategies
12
+ include Add
13
+ include Expanded
14
+ include Intersect
15
+ include Multi
16
+ include Override
17
+ include Union
18
+
19
+ attr_accessor :strategy
20
+
21
+ def selection(criterion = nil)
22
+ clone.tap do |query|
23
+ if criterion
24
+ criterion.each_pair do |field, value|
25
+ yield(query.selector, field, value)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def option(*args)
32
+ clone.tap do |query|
33
+ unless args.compact.empty?
34
+ yield(query.options)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Add
6
+
7
+ def _add(criterion, operator)
8
+ selection(criterion) do |selector, field, value|
9
+ selector.store(
10
+ field,
11
+ selector[field]._add({ operator => value })
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Expanded
6
+
7
+ def _expanded(criterion, outer, inner)
8
+ selection(criterion) do |selector, field, value|
9
+ selector.store(field, { outer => { inner => value }})
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Intersect
6
+
7
+ def intersect
8
+ tap { |query| query.strategy = :_intersect }
9
+ end
10
+
11
+ def _intersect(criterion, operator)
12
+ selection(criterion) do |selector, field, value|
13
+ selector.store(
14
+ field,
15
+ selector[field]._intersect({ operator => value })
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Multi
6
+
7
+ private
8
+
9
+ def multi!(criterion, operator)
10
+ clone.tap do |query|
11
+ sel = query.selector
12
+ criterion.each do |expr|
13
+ next unless expr
14
+ sel.fetch(operator){ |key| sel.store(operator, []) }.push(expr)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Override
6
+
7
+ def override
8
+ tap { |query| query.strategy = :_override }
9
+ end
10
+
11
+ def _override(criterion, operator)
12
+ selection(criterion) do |selector, field, value|
13
+ selector.store(field, { operator => value })
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Strategies
5
+ module Union
6
+
7
+ def union
8
+ tap { |query| query.strategy = :_union }
9
+ end
10
+
11
+ def _union(criterion, operator)
12
+ selection(criterion) do |selector, field, value|
13
+ selector.store(
14
+ field,
15
+ selector[field]._union({ operator => value })
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Type
5
+
6
+ def type(criterion = nil)
7
+ _override(criterion, "$type")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def type
13
+ Key.new(self, "$type")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module Where
5
+
6
+ def where(criterion = nil)
7
+ criterion.is_a?(String) ? js_query(criterion) : expr_query(criterion)
8
+ end
9
+
10
+ private
11
+
12
+ def expr_query(criterion)
13
+ selection(criterion) do |selector, field, value|
14
+ if field.is_a?(Key)
15
+ selector.merge!(field.specify(value))
16
+ else
17
+ selector.store(field, value)
18
+ end
19
+ end
20
+ end
21
+
22
+ def js_query(criterion)
23
+ clone.tap do |query|
24
+ query.selector.merge!("$where" => criterion)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module WithinBox
5
+
6
+ def within_box(criterion = nil)
7
+ _expanded(criterion, "$within", "$box")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def within_box
13
+ Key.new(self, "$within", "$box")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module WithinCircle
5
+
6
+ def within_circle(criterion = nil)
7
+ _expanded(criterion, "$within", "$center")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def within_circle
13
+ Key.new(self, "$within", "$center")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Selection
4
+ module WithinSphericalCircle
5
+
6
+ def within_spherical_circle(criterion = nil)
7
+ _expanded(criterion, "$within", "$centerSphere")
8
+ end
9
+
10
+ ::Symbol.class_eval do
11
+
12
+ def within_spherical_circle
13
+ Key.new(self, "$within", "$centerSphere")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ class Selector < Hash
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ VERSION = "0.0.0.alpha"
4
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: origin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Durran Jordan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70272334463460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70272334463460
25
+ - !ruby/object:Gem::Dependency
26
+ name: watchr
27
+ requirement: &70272334462920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70272334462920
36
+ description: Origin is a simple DSL for generating MongoDB selectors and options
37
+ email:
38
+ - durran@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/origin/ext/array.rb
44
+ - lib/origin/ext/hash.rb
45
+ - lib/origin/ext/nil.rb
46
+ - lib/origin/ext/object.rb
47
+ - lib/origin/ext.rb
48
+ - lib/origin/optional/batch_size.rb
49
+ - lib/origin/optional/hint.rb
50
+ - lib/origin/optional/limit.rb
51
+ - lib/origin/optional/max_scan.rb
52
+ - lib/origin/optional/no_timeout.rb
53
+ - lib/origin/optional/only.rb
54
+ - lib/origin/optional/read.rb
55
+ - lib/origin/optional/return_key.rb
56
+ - lib/origin/optional/show_disk_loc.rb
57
+ - lib/origin/optional/skip.rb
58
+ - lib/origin/optional/slice.rb
59
+ - lib/origin/optional/snapshot.rb
60
+ - lib/origin/optional/transformer.rb
61
+ - lib/origin/optional/without.rb
62
+ - lib/origin/optional.rb
63
+ - lib/origin/options.rb
64
+ - lib/origin/queryable.rb
65
+ - lib/origin/selection/all.rb
66
+ - lib/origin/selection/and.rb
67
+ - lib/origin/selection/between.rb
68
+ - lib/origin/selection/elem_match.rb
69
+ - lib/origin/selection/exists.rb
70
+ - lib/origin/selection/gt.rb
71
+ - lib/origin/selection/gte.rb
72
+ - lib/origin/selection/in.rb
73
+ - lib/origin/selection/key.rb
74
+ - lib/origin/selection/lt.rb
75
+ - lib/origin/selection/lte.rb
76
+ - lib/origin/selection/max_distance.rb
77
+ - lib/origin/selection/mod.rb
78
+ - lib/origin/selection/ne.rb
79
+ - lib/origin/selection/near.rb
80
+ - lib/origin/selection/near_sphere.rb
81
+ - lib/origin/selection/nin.rb
82
+ - lib/origin/selection/nor.rb
83
+ - lib/origin/selection/or.rb
84
+ - lib/origin/selection/size.rb
85
+ - lib/origin/selection/strategies/add.rb
86
+ - lib/origin/selection/strategies/expanded.rb
87
+ - lib/origin/selection/strategies/intersect.rb
88
+ - lib/origin/selection/strategies/multi.rb
89
+ - lib/origin/selection/strategies/override.rb
90
+ - lib/origin/selection/strategies/union.rb
91
+ - lib/origin/selection/strategies.rb
92
+ - lib/origin/selection/type.rb
93
+ - lib/origin/selection/where.rb
94
+ - lib/origin/selection/within_box.rb
95
+ - lib/origin/selection/within_circle.rb
96
+ - lib/origin/selection/within_spherical_circle.rb
97
+ - lib/origin/selection.rb
98
+ - lib/origin/selector.rb
99
+ - lib/origin/version.rb
100
+ - lib/origin.rb
101
+ - Rakefile
102
+ homepage: http://mongoid.org
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: 1.3.6
120
+ requirements: []
121
+ rubyforge_project: origin
122
+ rubygems_version: 1.8.10
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Simple DSL for MongoDB query generation
126
+ test_files: []