Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo mapper associations #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ Default options:
:index => true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML.
:sorted_hash_keys => false, # Do not sort hash keys.
:mongo_mapper => {
:show_associations => false, # Display association data for MongoMapper documents and classes
:inline_embedded => false # Display embedded associations inline with MongoMapper documents
},
:color => {
:array => :white,
:assoc => :greenish,
:bignum => :blue,
:class => :yellow,
:date => :greenish,
Expand Down
8 changes: 7 additions & 1 deletion lib/ap/awesome_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ def initialize(options = {})
:index => true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML.
:sorted_hash_keys => false, # Do not sort hash keys.
:mongo_mapper => {
:show_associations => false, # Display association data for MongoMapper documents and classes
:inline_embedded => false # Display embedded associations inline with MongoMapper documents
},
:color => {
:array => :white,
:assoc => :greenish,
:bigdecimal => :blue,
:class => :yellow,
:date => :greenish,
Expand Down Expand Up @@ -307,9 +312,10 @@ def outdent
@outdent = ' ' * (@indentation - @options[:indent].abs)
end

# Update @options by first merging the :color hash and then the remaining keys.
# Update @options by first merging the :mongo_mapper and :color hash and then the remaining keys.
#------------------------------------------------------------------------------
def merge_options!(options = {})
@options[:mongo_mapper].merge!(options.delete(:mongo_mapper) || {})
@options[:color].merge!(options.delete(:color) || {})
@options.merge!(options)
end
Expand Down
39 changes: 38 additions & 1 deletion lib/ap/mixin/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def printable_with_mongo_mapper(object)
if printable == :self
if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
printable = :mongo_mapper_instance
elsif object.is_a?(MongoMapper::Plugins::Associations::Base)
printable = :mongo_mapper_association
end
elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
printable = :mongo_mapper_class
Expand All @@ -35,7 +37,34 @@ def awesome_mongo_mapper_instance(object)
hash[name] = object[name]
hash
end
"#{object} " + awesome_hash(data)

# Add in associations
if @options[:mongo_mapper][:show_associations]
object.associations.each do |name, assoc|
if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
data[name.to_s] = object.send(name)
else
data[name.to_s] = assoc
end
end
end

label = object.to_s
label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(MongoMapper::EmbeddedDocument)

"#{label} #{awesome_hash(data)}"
end

# Format MongoMapper association object.
#------------------------------------------------------------------------------
def awesome_mongo_mapper_association(object)
return object.inspect if !defined?(ActiveSupport::OrderedHash)

association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'')
association = "embeds #{association}" if object.embeddable?
class_name = object.class_name

"#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
end

# Format MongoMapper class object.
Expand All @@ -47,6 +76,14 @@ def awesome_mongo_mapper_class(object)
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
hash
end

# Add in associations
if @options[:mongo_mapper][:show_associations]
object.associations.each do |name, assoc|
data[name.to_s] = assoc
end
end

"class #{object} < #{object.superclass} " << awesome_hash(data)
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def world(a,b); end
end

describe "object.protected_methods" do
before do
stub_dotfile!
end

it "index: should handle object.protected_methods" do
class Hello
protected
Expand Down Expand Up @@ -354,6 +358,7 @@ def m1(a, b = nil, &blk); end # m1(a, *b, &blk)
end

it "obj1.methods - obj2.methods should be awesome printed" do
stub_dotfile!
class Hello
def self.m1; end
end
Expand All @@ -362,6 +367,7 @@ def self.m1; end
end

it "obj1.methods & obj2.methods should be awesome printed" do
stub_dotfile!
class Hello
def self.m1; end
def self.m2; end
Expand All @@ -374,6 +380,7 @@ def self.m1; end
end

it "obj1.methods.grep(pattern) should be awesome printed" do
stub_dotfile!
class Hello
def self.m1; end
def self.m2; end
Expand All @@ -396,6 +403,7 @@ def self.m_two; end
end

it "obj1.methods.grep(pattern, &block) should be awesome printed" do
stub_dotfile!
class Hello
def self.m0; end
def self.none; end
Expand Down
111 changes: 111 additions & 0 deletions spec/mongo_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MongoUser
end

before :each do
stub_dotfile!
@ap = AwesomePrint.new(:plain => true)
end

Expand Down Expand Up @@ -56,6 +57,116 @@ class Chamelion < Object {
}
EOS
end

context "with associations" do
before :all do
class Child
include MongoMapper::EmbeddedDocument
key :data
end

class Sibling
include MongoMapper::Document
key :title
end

class Parent
include MongoMapper::Document
key :name

one :child
one :sibling
end
end

context "with show associations turned off (default)" do
it "should render the class as normal" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined
}
EOS
end

it "should render an instance as normal" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test"
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end

context "with show associations turned on and inline embedded turned off" do
before :each do
@ap = AwesomePrint.new(:plain => true,
:mongo_mapper => {
:show_associations => true })
end

it "should render the class with associations shown" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined,
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
end

it "should render an instance with associations shown" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end

context "with show associations turned on and inline embedded turned on" do
before :each do
@ap = AwesomePrint.new(:plain => true,
:mongo_mapper => {
:show_associations => true,
:inline_embedded => true })
end

it "should render an instance with associations shown and embeds there" do
parent = Parent.new(:name => 'test', :child => Child.new(:data => 5))
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embedded #<Child:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"data" => 5
},
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
end
end

rescue LoadError
Expand Down