origin-selectable_ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'origin'
4
+
5
+ group :development do
6
+ gem 'rspec'
7
+ gem "bundler", ">= 1.0.0"
8
+ gem "jeweler", ">= 1.8.4"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ origin (1.0.9)
13
+ rake (0.9.2.2)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.11.0)
17
+ rspec-core (~> 2.11.0)
18
+ rspec-expectations (~> 2.11.0)
19
+ rspec-mocks (~> 2.11.0)
20
+ rspec-core (2.11.1)
21
+ rspec-expectations (2.11.3)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.11.3)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (>= 1.0.0)
30
+ jeweler (>= 1.8.4)
31
+ origin
32
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Origin Selectable extensions
2
+
3
+ For generating more dynamic queries with Mongoid :)
4
+
5
+ # Usage
6
+
7
+ `gem 'origin-selectable_ext'`
8
+
9
+ Or include in `.gemspec` file as dependency ;)
10
+
11
+ ## Example usage
12
+
13
+ From the `money-mongoid` gem (also used in [Timespan](https://github.com/kristianmandrup/timespan))
14
+
15
+ ```ruby
16
+ class Money
17
+ class << self
18
+ def custom_serialization?(operator)
19
+ return false unless operator
20
+ case operator
21
+ when "$gte", "$gt", "$lt", "$lte"
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ def custom_specify(name, operator, value, options = {})
29
+ money = value.__evolve_to_money__
30
+ case operator
31
+ when "$gte", "$gt", "$lt", "$lte"
32
+ specify_with_multiple_currencies(name, operator, money, options)
33
+ else
34
+ raise RuntimeError, "Unsupported operator"
35
+ end
36
+ end
37
+
38
+ def custom_between(field, value, options = {})
39
+ { "$gte" => ::Money.new(value.min, value.iso_code), "$lte" => ::Money.new(value.max, value.iso_code) }
40
+ end
41
+
42
+ def custom_between? field, value
43
+ true
44
+ end
45
+
46
+ private
47
+
48
+ def specify_with_multiple_currencies(name, operator, value, options)
49
+ currencies = [value.currency.iso_code]
50
+ currencies.concat options[:compare_using] if options && options[:compare_using]
51
+ multiple_money_values = value.exchange_to_currencies(currencies.to_set)
52
+ subconditions = multiple_money_values.collect {|money| specify_with_single_currency(name, operator, money)}
53
+ if subconditions.count > 0
54
+ {"$or" => subconditions}
55
+ else
56
+ subconditions[0]
57
+ end
58
+ end
59
+
60
+ def specify_with_single_currency(name, operator, value)
61
+ {"#{name}.cents" => {operator => value.cents}, "#{name}.currency_iso" => value.currency.iso_code}
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ See https://github.com/mongoid/origin/issues/47 for more details.
68
+
69
+ ## Contributing to origin-selectable_ext
70
+
71
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
72
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
73
+ * Fork the project.
74
+ * Start a feature/bugfix branch.
75
+ * Commit and push until you are happy with your contribution.
76
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
77
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
78
+
79
+ ## Copyright
80
+
81
+ Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
82
+ further details.
83
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "origin-selectable_ext"
18
+ gem.homepage = "http://github.com/kristianmandrup/origin-selectable_ext"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Extensions to Mongoid Origin selectable in order to support more dynamic queries}
21
+ gem.description = %Q{Mongoid Origin query extensions for dynamic query generation}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ ## Extracted from @nessche monkey-patch for use with mongoid-money
4
+ module Origin
5
+
6
+ # An origin selectable is selectable, in that it has the ability to select
7
+ # document from the database. The selectable module brings all functionality
8
+ # to the selectable that has to do with building MongoDB selectors.
9
+ module Selectable
10
+
11
+ private
12
+
13
+ # Create the standard expression query.
14
+ #
15
+ # @api private
16
+ #
17
+ # @example Create the selection.
18
+ # selectable.expr_query(age: 50)
19
+ #
20
+ # @param [ Hash ] criterion The field/value pairs.
21
+ #
22
+ # @return [ Selectable ] The cloned selectable.
23
+ #
24
+ # @since 1.0.0
25
+ def expr_query(criterion)
26
+ selection(criterion) do |selector, field, value|
27
+ if (field.is_a? Key) && custom_serialization?(field.name, field.operator)
28
+ specified = custom_specify(field.name, field.operator, value)
29
+ else
30
+ specified = field.specify(value.__expand_complex__, negating?)
31
+ end
32
+ selector.merge!(specified)
33
+ end
34
+ end
35
+
36
+ def between(criterion = nil)
37
+ selection(criterion) do |selector, field, value|
38
+ expr = custom_between?(field, value) ? custom_between(field, value) : { "$gte" => value.min, "$lte" => value.max }
39
+ selector.store(
40
+ field, expr
41
+ )
42
+ end
43
+ end
44
+
45
+ def custom_between? name, value
46
+ serializer = @serializers[name.to_s]
47
+ serializer && serializer.type.respond_to?(:custom_between?) && serializer.type.custom_between?(name, value)
48
+ end
49
+
50
+ def custom_between(name, value)
51
+ serializer = @serializers[name.to_s]
52
+ raise RuntimeError, "No Serializer found for field #{name}" unless serializer
53
+ serializer.type.custom_between(name, value, serializer.options)
54
+ end
55
+
56
+ def custom_serialization?(name, operator)
57
+ serializer = @serializers[name.to_s]
58
+ serializer && serializer.type.respond_to?(:custom_serialization?) && serializer.type.custom_serialization?(operator)
59
+ end
60
+
61
+ def custom_specify(name, operator, value)
62
+ serializer = @serializers[name.to_s]
63
+ raise RuntimeError, "No Serializer found for field #{name}" unless serializer
64
+ serializer.type.custom_specify(name, operator, value, serializer.options)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "OriginSelectableExt" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'origin-selectable_ext'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: origin-selectable_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: origin
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ description: Mongoid Origin query extensions for dynamic query generation
79
+ email: kmandrup@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.md
85
+ files:
86
+ - .document
87
+ - .rspec
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - VERSION
94
+ - lib/origin-selectable_ext.rb
95
+ - spec/origin-selectable_ext_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: http://github.com/kristianmandrup/origin-selectable_ext
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 4551202408148928307
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Extensions to Mongoid Origin selectable in order to support more dynamic
125
+ queries
126
+ test_files: []