Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
7 lib/plucky.rb
@@ -13,6 +13,11 @@ module Plucky
# Array of finder DSL methods to delegate
Methods = Plucky::Query::DSL.instance_methods.sort.map(&:to_sym)
+ # Public: Converts value to object id if possible
+ #
+ # value - The value to attempt converation of
+ #
+ # Returns BSON::ObjectId or value
def self.to_object_id(value)
return value if value.is_a?(BSON::ObjectId)
return nil if value.nil? || (value.respond_to?(:empty?) && value.empty?)
@@ -24,8 +29,10 @@ def self.to_object_id(value)
end
end
+ # Private
ModifierString = '$'
+ # Internal
def self.modifier?(key)
key.to_s[0, 1] == ModifierString
end
View
25 lib/plucky/criteria_hash.rb
@@ -5,7 +5,11 @@
module Plucky
class CriteriaHash
- attr_reader :source, :options
+ # Private: The Hash that stores query criteria
+ attr_reader :source
+
+ # Private: The Hash that stores options
+ attr_reader :options
# Internal: Used to determine if criteria keys match simple id lookup.
SimpleIdQueryKeys = [:_id].to_set
@@ -18,6 +22,7 @@ class CriteriaHash
# criteria hash is simple.
SimpleQueryMaxSize = [SimpleIdQueryKeys.size, SimpleIdAndTypeQueryKeys.size].max
+ # Public
def initialize(hash={}, options={})
@source, @options = {}, options
hash.each { |key, value| self[key] = value }
@@ -32,10 +37,13 @@ def initialize_copy(original)
end
end
+ # Public
def [](key)
@source[key]
end
+ # Public
+ # The contents of this make me sad...need to clean it up
def []=(key, value)
normalized_key = normalized_key(key)
@@ -54,18 +62,22 @@ def []=(key, value)
end
end
+ # Public
def keys
@source.keys
end
+ # Public
def ==(other)
@source == other.source
end
+ # Public
def to_hash
@source
end
+ # Public and completely disgusting
def merge(other)
target = @source.dup
other.source.each_key do |key|
@@ -105,6 +117,7 @@ def merge(other)
self.class.new(target)
end
+ # Public
def merge!(other)
merge(other).to_hash.each do |key, value|
self[key] = value
@@ -112,18 +125,22 @@ def merge!(other)
self
end
+ # Private
def object_ids
@options[:object_ids] ||= []
end
+ # Private
def object_ids=(value)
raise ArgumentError unless value.is_a?(Array)
@options[:object_ids] = value.flatten
end
- # The definition of simple is querying by only _id or _id and _type.
+ # Public: The definition of simple is querying by only _id or _id and _type.
# If this is the case, you can use IdentityMap in library to not perform
# query and instead just return from map.
+ #
+ # Returns true or false
def simple?
return false if keys.size > SimpleQueryMaxSize
key_set = keys.to_set
@@ -134,20 +151,24 @@ def object_id?(key)
object_ids.include?(key.to_sym)
end
+ # Private
def normalized_key(key)
key_normalizer.call(key)
end
+ # Private
def key_normalizer
@key_normalizer ||= @options.fetch(:key_normalizer) {
Normalizers::CriteriaHashKey.new
}
end
+ # Private
def normalized_value(parent_key, key, value)
value_normalizer.call(parent_key, key, value)
end
+ # Private
def value_normalizer
@value_normalizer ||= @options.fetch(:value_normalizer) {
Normalizers::CriteriaHashValue.new(self)
View
6 lib/plucky/normalizers/integer.rb
@@ -1,6 +1,12 @@
module Plucky
module Normalizers
class Integer
+
+ # Public: Returns value coerced to integer or nil
+ #
+ # value - The value to normalize to an integer
+ #
+ # Returns an Integer or nil
def call(value)
if value.nil?
nil
View
6 lib/plucky/normalizers/options_hash_key.rb
@@ -2,6 +2,7 @@ module Plucky
module Normalizers
class OptionsHashKey
+ # Internal: Keys with values that they should normalize to
NormalizedKeys = {
:order => :sort,
:select => :fields,
@@ -9,6 +10,11 @@ class OptionsHashKey
:id => :_id,
}
+ # Public: Normalizes an options hash key
+ #
+ # key - The key to normalize
+ #
+ # Returns a Symbol.
def call(key)
NormalizedKeys.fetch key.to_sym, key
end
View
31 lib/plucky/normalizers/options_hash_value.rb
@@ -5,6 +5,29 @@
module Plucky
module Normalizers
class OptionsHashValue
+
+ # Public: Initialize an OptionsHashValue.
+ #
+ # args - The hash of arguments (default: {})
+ # :key_normalizer - The key normalizer to use, must respond to call
+ # :value_normalizers - Hash where key is name of options hash key
+ # to normalize and value is what should be used
+ # to normalize the value accordingly (must respond
+ # to call). Allows adding normalizers for new keys
+ # and overriding existing default normalizers.
+ #
+ #
+ # Examples
+ #
+ # Plucky::Normalizers::OptionsHashValue.new({
+ # :key_normalizer => lambda { |key| key}, # key normalizer must responds to call
+ # :value_normalizers => {
+ # :new_key => lambda { |key| key.to_s.upcase }, # add normalizer for :new_key
+ # :fields => lambda { |key| key }, # override normalizer for fields to one that does nothing
+ # }
+ # })
+ #
+ # Returns the duplicated String.
def initialize(args = {})
@key_normalizer = args.fetch(:key_normalizer) {
raise ArgumentError, "Missing required key :key_normalizer"
@@ -23,10 +46,10 @@ def initialize(args = {})
end
# Public: Returns value normalized for Mongo
- #
- # key -
-The name of the key whose value is being normalized
- # value -
+ #
+ # key - The name of the key whose value is being normalized
+ # value - The value to normalize
+ #
# Returns value normalized for Mongo.
def call(key, value)
if (value_normalizer = @value_normalizers[key])
View
10 lib/plucky/normalizers/sort_value.rb
@@ -2,8 +2,16 @@ module Plucky
module Normalizers
class SortValue
+ # Public: Initializes a Plucky::Normalizers::SortValue
+ #
+ # args - The hash of arguments
+ # :key_normalizer - What to use to normalize keys, must
+ # respond to call.
+ #
def initialize(args = {})
- @key_normalizer = args.fetch(:key_normalizer) { raise ArgumentError, "Missing required key :key_normalizer" }
+ @key_normalizer = args.fetch(:key_normalizer) {
+ raise ArgumentError, "Missing required key :key_normalizer"
+ }
end
# Public: Given a value returns it normalized for Mongo's sort option
View
15 lib/plucky/options_hash.rb
@@ -6,8 +6,13 @@
module Plucky
class OptionsHash
- attr_reader :source, :options
+ # Private: The Hash that stores the query options
+ attr_reader :source
+ # Private: The Hash that stores instance options
+ attr_reader :options
+
+ # Public
def initialize(hash={}, options={})
@source = {}
@options = options
@@ -22,35 +27,43 @@ def initialize_copy(original)
end
end
+ # Public
def [](key)
@source[key]
end
+ # Public
def []=(key, value)
key = normalized_key(key)
@source[key] = normalized_value(key, value)
end
+ # Public
def keys
@source.keys
end
+ # Public
def ==(other)
@source == other.source
end
+ # Public
def to_hash
@source
end
+ # Public
def fields?
!self[:fields].nil?
end
+ # Public
def merge(other)
self.class.new(to_hash.merge(other.to_hash))
end
+ # Public
def merge!(other)
other.to_hash.each { |key, value| self[key] = value }
self
View
5 lib/plucky/pagination/decorator.rb
@@ -8,9 +8,10 @@ module Decorator
:total_entries, :total_pages,
:current_page, :per_page,
:previous_page, :next_page,
- :skip, :limit,
+ :skip, :limit,
:offset, :out_of_bounds?
+ # Public
def paginator(p=nil)
return @paginator if p.nil?
@paginator = p
@@ -18,4 +19,4 @@ def paginator(p=nil)
end
end
end
-end
+end
View
21 lib/plucky/pagination/paginator.rb
@@ -4,34 +4,43 @@ module Pagination
class Paginator
attr_reader :total_entries, :current_page, :per_page
+ # Public
def initialize(total, page, per_page=nil)
@total_entries = total.to_i
@current_page = [page.to_i, 1].max
@per_page = (per_page || 25).to_i
end
+ # Public
def total_pages
- (total_entries / per_page.to_f).ceil
+ (@total_entries / @per_page.to_f).ceil
end
+ # Public
def out_of_bounds?
- current_page > total_pages
+ @current_page > total_pages
end
+ # Public
def previous_page
- current_page > 1 ? (current_page - 1) : nil
+ @current_page > 1 ? (@current_page - 1) : nil
end
+ # Public
def next_page
- current_page < total_pages ? (current_page + 1) : nil
+ @current_page < total_pages ? (@current_page + 1) : nil
end
+ # Public
def skip
- (current_page - 1) * per_page
+ (@current_page - 1) * @per_page
end
+ # Public
alias :limit :per_page
+
+ # Public
alias :offset :skip
end
end
-end
+end
View
31 lib/plucky/query.rb
@@ -7,6 +7,7 @@ class Query
include Enumerable
extend Forwardable
+ # Private
OptionKeys = Set[
:select, :offset, :order, # MM
:fields, :skip, :limit, :sort, :hint, :snapshot, # Ruby Driver
@@ -17,10 +18,11 @@ class Query
attr_reader :criteria, :options, :collection
- def_delegator :criteria, :simple?
- def_delegator :options, :fields?
+ def_delegator :@criteria, :simple?
+ def_delegator :@options, :fields?
def_delegators :to_a, :include?
+ # Public
def initialize(collection, query_options = {})
@collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
query_options.each { |key, value| self[key] = value }
@@ -32,9 +34,10 @@ def initialize_copy(original)
@options = @options.dup
end
+ # Public
def object_ids(*keys)
- return criteria.object_ids if keys.empty?
- criteria.object_ids = *keys
+ return @criteria.object_ids if keys.empty?
+ @criteria.object_ids = *keys
self
end
@@ -171,8 +174,8 @@ def empty?
count.zero?
end
- def exists?(options={})
- !count(options).zero?
+ def exists?(query_options={})
+ !count(query_options).zero?
end
alias :exist? :exists?
@@ -205,8 +208,8 @@ def []=(key, value)
end
def merge(other)
- merged_criteria = criteria.merge(other.criteria).to_hash
- merged_options = options.merge(other.options).to_hash
+ merged_criteria = @criteria.merge(other.criteria).to_hash
+ merged_options = @options.merge(other.options).to_hash
clone.amend(merged_criteria).amend(merged_options)
end
@@ -215,7 +218,7 @@ def to_hash
end
def explain
- collection.find(criteria_hash, options_hash).explain
+ @collection.find(criteria_hash, options_hash).explain
end
def inspect
@@ -226,23 +229,25 @@ def inspect
end
def criteria_hash
- criteria.to_hash
+ @criteria.to_hash
end
def options_hash
- options.to_hash
+ @options.to_hash
end
def cursor
- collection.find(criteria_hash, options_hash)
+ @collection.find(criteria_hash, options_hash)
end
private
+ # Private
def hash_for_key(key)
options_key?(key) ? @options : @criteria
end
+ # Private
def symbolized_key(key)
if key.respond_to?(:to_sym)
key.to_sym
@@ -251,10 +256,12 @@ def symbolized_key(key)
end
end
+ # Private
def options_key?(key)
OptionKeys.include?(key)
end
+ # Private
def set_field_inclusion(fields, value)
fields_option = {}
fields.each { |field| fields_option[symbolized_key(field)] = value }

No commit comments for this range

Something went wrong with that request. Please try again.