Module: AmazingPrint::MongoMapper

Included in:
Formatter
Defined in:
lib/amazing_print/ext/mongo_mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
# File 'lib/amazing_print/ext/mongo_mapper.rb', line 10

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/amazing_print/ext/mongo_mapper.rb', line 95

def awesome_mongo_mapper_association(object)
  return object.inspect unless 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/amazing_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.




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

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

  data = object.keys.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
    hash[c.first] = (c.last.type || 'undefined').to_s.underscore.intern
  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




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/amazing_print/ext/mongo_mapper.rb', line 68

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

  data = object.keys.keys.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
    hash[name] = object[name]
  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] && 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)].join(' ')
end

#cast_with_mongo_mapper(object, type) ⇒ Object

Add MongoMapper class names to the dispatcher pipeline.




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

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]).empty?
      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