Module: AmazingPrint::Mongoid
- Included in:
- Formatter
- Defined in:
- lib/amazing_print/ext/mongoid.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#awesome_mongoid_bson_id(object) ⇒ Object
Format BSON::ObjectId ——————————————————————————.
-
#awesome_mongoid_class(object) ⇒ Object
Format Mongoid class object.
-
#awesome_mongoid_document(object) ⇒ Object
Format Mongoid Document object.
-
#cast_with_mongoid(object, type) ⇒ Object
Add Mongoid class names to the dispatcher pipeline.
Class Method Details
.included(base) ⇒ Object
10 11 12 13 |
# File 'lib/amazing_print/ext/mongoid.rb', line 10 def self.included(base) base.send :alias_method, :cast_without_mongoid, :cast base.send :alias_method, :cast, :cast_with_mongoid end |
Instance Method Details
#awesome_mongoid_bson_id(object) ⇒ Object
Format BSON::ObjectId
72 73 74 |
# File 'lib/amazing_print/ext/mongoid.rb', line 72 def awesome_mongoid_bson_id(object) object.inspect end |
#awesome_mongoid_class(object) ⇒ Object
Format Mongoid class object.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/amazing_print/ext/mongoid.rb', line 33 def awesome_mongoid_class(object) return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) aliases = object.aliased_fields.invert data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash| name = c[1].name alias_name = aliases[name] unless name == '_id' printed_name = alias_name ? "#{alias_name}(#{name})" : name hash[printed_name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern hash 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_mongoid_document(object) ⇒ Object
Format Mongoid Document object.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/amazing_print/ext/mongoid.rb', line 54 def awesome_mongoid_document(object) return object.inspect unless defined?(::ActiveSupport::OrderedHash) aliases = object.aliased_fields.invert data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash| name = c[0] alias_name = aliases[name] unless name == '_id' printed_name = alias_name ? "#{alias_name}(#{name})" : name hash[printed_name.to_sym] = c[1] hash end data = { errors: object.errors, attributes: data } unless object.errors.empty? "#{object} #{awesome_hash(data)}" end |
#cast_with_mongoid(object, type) ⇒ Object
Add Mongoid class names to the dispatcher pipeline.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/amazing_print/ext/mongoid.rb', line 17 def cast_with_mongoid(object, type) cast = cast_without_mongoid(object, type) if defined?(::Mongoid::Document) if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document) cast = :mongoid_class elsif object.class.ancestors.include?(::Mongoid::Document) cast = :mongoid_document elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::BSON::ObjectId)) cast = :mongoid_bson_id end end cast end |