Skip to content

Commit

Permalink
Merge pull request #3 from robertomiranda/refactoring
Browse files Browse the repository at this point in the history
[WIP] Complete refactoring
  • Loading branch information
guilleiguaran committed Oct 26, 2012
2 parents 6d7fd22 + 1216ede commit 5b176e1
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 93 deletions.
5 changes: 4 additions & 1 deletion lib/mini_mongo.rb
@@ -1,9 +1,12 @@
require 'active_support/all'
module MiniMongo
autoload :Connection, 'mini_mongo/connection'
autoload :MiniMongoError, 'mini_mongo/errors'
autoload :DocumentNotFound, 'mini_mongo/errors'
autoload :Mapper, 'mini_mongo/mapper'
autoload :Base, 'mini_mongo/base'
autoload :Core, 'mini_mongo/core'
autoload :AttributeAssignment, 'mini_mongo/attribute_assignment'
autoload :Comparable, 'mini_mongo/comparable'
autoload :Serialization, 'mini_mongo/serialization'
extend Connection
end
28 changes: 28 additions & 0 deletions lib/mini_mongo/attribute_assignment.rb
@@ -0,0 +1,28 @@
module MiniMongo
module AttributeAssignment
def assign_attributes(attributes)
@attributes["id"] = attributes.delete("_id").to_s || attributes.delete(:_id).to_s
attributes.each do |key, value|
@attributes[key] = value
define_accessors(key) unless self.respond_to? :key
end
end

alias_method :attributes=, :assign_attributes

def id
@attributes["id"]
end

private
def define_accessors(key)
define_singleton_method(key) do
@attributes[key]
end

define_singleton_method("#{key}=") do |value|
@attributes[key] = value
end
end
end
end
95 changes: 4 additions & 91 deletions lib/mini_mongo/base.rb
@@ -1,96 +1,9 @@
require "json"
module MiniMongo
class Base
include MiniMongo::Core
include MiniMongo::Mapper
include Comparable

attr_reader :attributes

def initialize(attrs = {})
attributes = attrs
attributes["id"] = attributes.delete("_id").to_s || attributes.delete(:_id).to_s
@attributes = attributes
process_attributes!
end

# Default comparison is via the string version of the id.
#
# @example Compare two documents.
# person <=> other_person
#
# @param [ Document ] other The document to compare with.
#
# @return [ Integer ] -1, 0, 1.
def <=>(other)
attributes["id"].to_s <=> other.attributes["id"].to_s
end

# Performs class equality checking.
#
# @example Compare the classes.
# document === other
#
# @param [ Document, Object ] other The other object to compare with.
#
# @return [ true, false ] True if the classes are equal, false if not.
def ===(other)
other.class == Class ? self.class === other : self == other
end

# Delegates to ==. Used when needing checks in hashes.
#
# @example Perform equality checking.
# document.eql?(other)
#
# @param [ Document, Object ] other The object to check against.
#
# @return [ true, false ] True if equal, false if not.
def eql?(other)
self == (other)
end

# Return an array with this +Document+ only in it.
#
# @example Return the document in an array.
# document.to_a
#
def to_a
attributes.to_a
end


def inspect
inspection = if attributes
attributes.collect { |key, value|
"#{key}: #{value_for_inspect(value)}"
}.compact.join(", ")
else
"not initialized"
end
"#<#{self.class} #{inspection}>"
end

private

def process_attributes!
attributes.each do |key, value|
instance_variable_set("@#{key}", value)
self.instance_eval("def #{key};@#{key};end")
self.instance_eval("def #{key}=(val);@#{key}=val;end")
end
self.instance_eval("def to_json;#{attributes}.to_json;end")
self.instance_eval("def to_h;#{attributes};end")
end

def value_for_inspect(value)
if value.is_a?(String) && value.length > 50
"#{value[0..50]}...".inspect
elsif value.is_a?(Date) || value.is_a?(Time)
%("#{value.to_s(:db)}")
else
value.inspect
end
end

include MiniMongo::AttributeAssignment
include MiniMongo::Comparable
include MiniMongo::Serialization
end
end
41 changes: 41 additions & 0 deletions lib/mini_mongo/comparable.rb
@@ -0,0 +1,41 @@
module MiniMongo
module Comparable
include ::Comparable

# Default comparison is via the string version of the id.
#
# @example Compare two documents.
# person <=> other_person
#
# @param [ Document ] other The document to compare with.
#
# @return [ Integer ] -1, 0, 1.
def <=>(other)
attributes["id"] <=> other.attributes["id"]
end

# Performs class equality checking.
#
# @example Compare the classes.
# document === other
#
# @param [ Document, Object ] other The other object to compare with.
#
# @return [ true, false ] True if the classes are equal, false if not.
def ===(other)
other.class == Class ? self.class === other : self == other
end

# Delegates to ==. Used when needing checks in hashes.
#
# @example Perform equality checking.
# document.eql?(other)
#
# @param [ Document, Object ] other The object to check against.
#
# @return [ true, false ] True if equal, false if not.
def eql?(other)
self == (other)
end
end
end
1 change: 1 addition & 0 deletions lib/mini_mongo/connection.rb
@@ -1,5 +1,6 @@
require "uri"
require "mongo"

module MiniMongo
module Connection

Expand Down
47 changes: 47 additions & 0 deletions lib/mini_mongo/core.rb
@@ -0,0 +1,47 @@
module MiniMongo
module Core

attr_reader :attributes

alias_method :to_hash, :attributes

def initialize(attributes = {})
@attributes = {}
assign_attributes(attributes.dup)
end

# Return an array with this +Document+ only in it.
#
# @example Return the document in an array.
# document.to_a
#
def to_a
attributes.to_a
end


def inspect
inspection = if attributes
attributes.collect { |key, value|
"#{key}: #{value_for_inspect(value)}"
}.compact.join(", ")
else
"not initialized"
end
"#<#{self.class} #{inspection}>"
end

private

def value_for_inspect(value)
if value.is_a?(String) && value.length > 50
"#{value[0..50]}...".inspect
elsif value.is_a?(Date) || value.is_a?(Time)
%("#{value.to_s(:db)}")
else
value.inspect
end
end

end
end
4 changes: 3 additions & 1 deletion lib/mini_mongo/mapper.rb
@@ -1,5 +1,7 @@
require "uri"
require "mongo"
require "active_support/core_ext/class/attribute_accessors"

module MiniMongo
module Mapper
def self.included(base)
Expand All @@ -26,7 +28,7 @@ def insert(attrs={})
doc = {}
doc["_id"] = self.collection.insert(attrs).to_s
doc.merge!(attrs)
self.class_eval("new(#{doc})")
self.new(doc)
end

def update(id, attrs={})
Expand Down
7 changes: 7 additions & 0 deletions lib/mini_mongo/serialization.rb
@@ -0,0 +1,7 @@
module MiniMongo
module Serialization
def to_json
to_hash.to_json
end
end
end

0 comments on commit 5b176e1

Please sign in to comment.