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.




95
96
97
98
99
100
101
102
103
104
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 95

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




108
109
110
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 108

def awesome_mongo_mapper_bson_id(object)
  object.inspect
end

#awesome_mongo_mapper_class(object) ⇒ Object

Format MongoMapper class object.




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

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

  name = "class #{awesome_simple(object.to_s, :class)}"
  base = "< #{awesome_simple(object.superclass.to_s, :class)}"

  [name, base, awesome_hash(data)].join(' ')
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




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

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|
      data[name.to_s] = if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
                          object.send(name)
                        else
                          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
33
# File 'lib/awesome_print/ext/mongo_mapper.rb', line 16

def cast_with_mongo_mapper(object, type)
  apply_default_mongo_mapper_options
  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