Module: AwesomePrint::MongoMapper

Defined in:
lib/awesome_print/ext/mongo_mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 9

def self.included(base)
  base.send :alias_method, :cast_without_mongo_mapper, :cast
  base.send :alias_method, :cast, :cast_with_mongo_mapper
end

Instance Method Details

#awesome_mongo_mapper_association(object) ⇒ Object

Format MongoMapper association object.




91
92
93
94
95
96
97
98
99
100
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 91

def awesome_mongo_mapper_association(object)
  return object.inspect if !defined?(::ActiveSupport::OrderedHash)
  return awesome_object(object) if @options[:raw]

  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

#awesome_mongo_mapper_bson_id(object) ⇒ Object

Format BSON::ObjectId




104
105
106
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 104

def awesome_mongo_mapper_bson_id(object)
  object.inspect
end

#awesome_mongo_mapper_class(object) ⇒ Object

Format MongoMapper class object.




36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 36

def awesome_mongo_mapper_class(object)
  return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)

  data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
    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

#awesome_mongo_mapper_instance(object) ⇒ Object

Format MongoMapper instance object.

NOTE: by default only instance attributes (i.e. keys) are shown. To format MongoMapper instance as regular object showing its instance variables and accessors use :raw => true option:

ap record, :raw => true




63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 63

def awesome_mongo_mapper_instance(object)
  return object.inspect if !defined?(::ActiveSupport::OrderedHash)
  return awesome_object(object) if @options[:raw]

  data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
    hash[name] = object[name]
    hash
  end

  # 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

#cast_with_mongo_mapper(object, type) ⇒ Object

Add MongoMapper class names to the dispatcher pipeline.




16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 16

def cast_with_mongo_mapper(object, type)
  cast = cast_without_mongo_mapper(object, type)

  if defined?(::MongoMapper::Document) 
    if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
      cast = :mongo_mapper_class
    elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
      cast = :mongo_mapper_instance
    elsif object.is_a?(::MongoMapper::Plugins::Associations::Base)
      cast = :mongo_mapper_association
    elsif object.is_a?(::BSON::ObjectId)
      cast = :mongo_mapper_bson_id
    end
  end

  cast
end