Skip to content

Commit

Permalink
Minor: whitespace, cleanup and such. Tiny changes to FinderOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Oct 28, 2009
1 parent cbeac78 commit e85830e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/mongo_mapper/associations.rb
@@ -1,12 +1,12 @@
module MongoMapper
module Associations
module ClassMethods
def belongs_to(association_id, options = {})
def belongs_to(association_id, options={})
create_association(:belongs_to, association_id, options)
self
end

def many(association_id, options = {})
def many(association_id, options={})
create_association(:many, association_id, options)
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/associations/base.rb
Expand Up @@ -3,7 +3,7 @@ module Associations
class Base
attr_reader :type, :name, :options

def initialize(type, name, options = {})
def initialize(type, name, options={})
@type, @name, @options = type, name, options
end

Expand Down
@@ -1,6 +1,6 @@
module MongoMapper
module Associations
class ManyEmbeddedPolymorphicProxy < Proxy
class ManyEmbeddedPolymorphicProxy < Proxy
def replace(v)
@_values = v.map do |doc_or_hash|
if doc_or_hash.kind_of?(EmbeddedDocument)
Expand Down Expand Up @@ -30,4 +30,4 @@ def polymorphic_class(doc)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongo_mapper/associations/many_polymorphic_proxy.rb
@@ -1,6 +1,6 @@
module MongoMapper
module Associations
class ManyPolymorphicProxy < ManyDocumentsProxy
class ManyPolymorphicProxy < ManyDocumentsProxy
private
def apply_scope(doc)
doc.send("#{@association.type_key_name}=", doc.class.name)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/dynamic_finder.rb
Expand Up @@ -14,7 +14,7 @@ def found?
end

protected
def match
def match
case method.to_s
when /^find_(all_by|by)_([_a-zA-Z]\w*)$/
@finder = :all if $1 == 'all_by'
Expand Down
69 changes: 34 additions & 35 deletions lib/mongo_mapper/finder_options.rb
@@ -1,56 +1,55 @@
module MongoMapper
class FinderOptions
OptionKeys = [:fields, :select, :skip, :offset, :limit, :sort, :order]

def initialize(model, options)
raise ArgumentError, "FinderOptions must be a hash" unless options.is_a?(Hash)
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
options.symbolize_keys!

@model, @options, @conditions = model, {}, options.delete(:conditions) || {}

@model = model
@options = {}
@conditions = options.delete(:conditions) || {}

options.each_pair do |key, value|
if OptionKeys.include?(key)
@options[key] = value
else
@conditions[key] = value
end
end

add_sci_scope
end

def criteria
to_mongo_criteria(@conditions)
end

def options
options = @options.dup
{
:fields => to_mongo_fields(options.delete(:fields) || options.delete(:select)),
:skip => (options.delete(:skip) || options.delete(:offset) || 0).to_i,
:limit => (options.delete(:limit) || 0).to_i,
:sort => options.delete(:sort) || to_mongo_sort(options.delete(:order))
}

fields = options.delete(:fields) || options.delete(:select)
skip = options.delete(:skip) || options.delete(:offset) || 0
limit = options.delete(:limit) || 0
sort = options.delete(:sort) || convert_order_to_sort(options.delete(:order))

{:fields => to_mongo_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
end

def to_a
[criteria, options]
end

private
def to_mongo_criteria(conditions, parent_key=nil)
criteria = {}

conditions.each_pair do |field, value|
field = field_normalized(field)
field = normalized_field(field)
case value
when Array
operator_present = field.to_s =~ /^\$/
criteria[field] = if operator_present
value
else
{'$in' => value}
end
operator_present = field.to_s =~ /^\$/
criteria[field] = operator?(field) ? value : {'$in' => value}
when Hash
criteria[field] = to_mongo_criteria(value, field)
else
Expand All @@ -60,13 +59,13 @@ def to_mongo_criteria(conditions, parent_key=nil)

criteria
end
def field_normalized(field)
if field.to_s == 'id'
:_id
else
field
end

def operator?(field)
field.to_s =~ /^\$/
end

def normalized_field(field)
field.to_s == 'id' ? :_id : field
end

# adds _type single collection inheritance scope for models that need it
Expand All @@ -75,28 +74,28 @@ def add_sci_scope
@conditions[:_type] = @model.to_s
end
end

def to_mongo_fields(fields)
return if fields.blank?

if fields.is_a?(String)
fields.split(',').map { |field| field.strip }
else
fields.flatten.compact
end
end
def to_mongo_sort(sort)

def convert_order_to_sort(sort)
return if sort.blank?
pieces = sort.split(',')
pieces.map { |s| to_mongo_sort_piece(s) }
end

def to_mongo_sort_piece(str)
field, direction = str.strip.split(' ')
direction ||= 'ASC'
direction = direction.upcase == 'ASC' ? 1 : -1
[field, direction]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongo_mapper/serialization.rb
Expand Up @@ -5,7 +5,7 @@ module Serialization
class Serializer #:nodoc:
attr_reader :options

def initialize(record, options = {})
def initialize(record, options={})
@record, @options = record, options.dup
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/serializers/json_serializer.rb
Expand Up @@ -49,7 +49,7 @@ def self.included(base)
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
# "created_at": "2006/08/01", "awesome": true,
# "permalink": "1-konata-izumi"}
def to_json(options = {})
def to_json(options={})
apply_to_json_defaults(options)

if include_root_in_json
Expand Down

0 comments on commit e85830e

Please sign in to comment.