Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for between queries on MoneyRange
- Loading branch information
1 parent
51571da
commit 0ccddf6
Showing
9 changed files
with
171 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# encoding: utf-8 | ||
module Origin | ||
|
||
# An origin selectable is selectable, in that it has the ability to select | ||
# document from the database. The selectable module brings all functionality | ||
# to the selectable that has to do with building MongoDB selectors. | ||
module Selectable | ||
|
||
private | ||
|
||
# Create the standard expression query. | ||
# | ||
# @api private | ||
# | ||
# @example Create the selection. | ||
# selectable.expr_query(age: 50) | ||
# | ||
# @param [ Hash ] criterion The field/value pairs. | ||
# | ||
# @return [ Selectable ] The cloned selectable. | ||
# | ||
# @since 1.0.0 | ||
def expr_query(criterion) | ||
selection(criterion) do |selector, field, value| | ||
if (field.is_a? Key) && custom_serialization?(field.name, field.operator) | ||
specified = custom_specify(field.name, field.operator, value) | ||
else | ||
specified = field.specify(value.__expand_complex__, negating?) | ||
end | ||
selector.merge!(specified) | ||
end | ||
end | ||
|
||
def between(criterion = nil) | ||
selection(criterion) do |selector, field, value| | ||
expr = custom_between?(field, value) ? custom_between(field, value) : { "$gte" => value.min, "$lte" => value.max } | ||
selector.store( | ||
field, expr | ||
) | ||
end | ||
end | ||
|
||
def custom_between? name, value | ||
serializer = @serializers[name.to_s] | ||
serializer && serializer.type.respond_to?(:custom_between?) && serializer.type.custom_between?(name, value) | ||
end | ||
|
||
def custom_between(name, value) | ||
serializer = @serializers[name.to_s] | ||
raise RuntimeError, "No Serializer found for field #{name}" unless serializer | ||
serializer.type.custom_between(name, value, serializer.options) | ||
end | ||
|
||
|
||
def custom_serialization?(name, operator) | ||
serializer = @serializers[name.to_s] | ||
serializer && serializer.type.respond_to?(:custom_serialization?) && serializer.type.custom_serialization?(operator) | ||
end | ||
|
||
def custom_specify(name, operator, value) | ||
serializer = @serializers[name.to_s] | ||
raise RuntimeError, "No Serializer found for field #{name}" unless serializer | ||
serializer.type.custom_specify(name, operator, value, serializer.options) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require 'money/mongoid/core_ext/string' | ||
require 'money/mongoid/core_ext/array' | ||
require 'money/mongoid/core_ext/range' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Range | ||
def dollars | ||
::MoneyRange.new self, 'usd' | ||
end | ||
|
||
def euros | ||
::MoneyRange.new self, 'eur' | ||
end | ||
|
||
def to_currency iso_code | ||
::MoneyRange.new self, iso_code | ||
end | ||
end | ||
|
||
class Money | ||
def self.range range, iso_code | ||
::MoneyRange.new range, iso_code | ||
end | ||
end | ||
|
||
# http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html | ||
class DelegateDecorator | ||
def initialize(subject) | ||
subject.public_methods(false).each do |meth| | ||
(class << self; self; end).class_eval do | ||
define_method meth do |*args| | ||
subject.send meth, *args | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class MoneyRange < DelegateDecorator | ||
attr_reader :iso_code, :range | ||
|
||
def initialize range, iso_code | ||
super(range) | ||
@range = range | ||
@iso_code = iso_code | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters