Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from robertomiranda/refactoring
[WIP] Complete refactoring
- Loading branch information
Showing
8 changed files
with
135 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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,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 |
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,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 |
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,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 |
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,5 +1,6 @@ | ||
require "uri" | ||
require "mongo" | ||
|
||
module MiniMongo | ||
module Connection | ||
|
||
|
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,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 |
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,7 @@ | ||
module MiniMongo | ||
module Serialization | ||
def to_json | ||
to_hash.to_json | ||
end | ||
end | ||
end |