Module: AmazingPrint::NoBrainer

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
# File 'lib/amazing_print/ext/nobrainer.rb', line 12

def self.included(base)
  base.send :alias_method, :cast_without_nobrainer, :cast
  base.send :alias_method, :cast, :cast_with_nobrainer
end

Instance Method Details

#awesome_nobrainer_class(object) ⇒ Object

Format NoBrainer class object.




33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amazing_print/ext/nobrainer.rb', line 33

def awesome_nobrainer_class(object)
  name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
  data = object.fields.map do |field, options|
    [field, (options[:type] || Object).to_s.underscore.to_sym]
  end.to_h

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

  [name, base, awesome_hash(data)].join(' ')
end

#awesome_nobrainer_document(object) ⇒ Object

Format NoBrainer Document object.




47
48
49
50
51
# File 'lib/amazing_print/ext/nobrainer.rb', line 47

def awesome_nobrainer_document(object)
  data = object.inspectable_attributes.symbolize_keys
  data = { errors: object.errors, attributes: data } if object.errors.present?
  "#{object} #{awesome_hash(data)}"
end

#cast_with_nobrainer(object, type) ⇒ Object

Add NoBrainer class names to the dispatcher pipeline.




19
20
21
22
23
24
25
26
27
28
29
# File 'lib/amazing_print/ext/nobrainer.rb', line 19

def cast_with_nobrainer(object, type)
  cast = cast_without_nobrainer(object, type)
  if defined?(::NoBrainer::Document)
    if object.is_a?(Class) && object < ::NoBrainer::Document
      cast = :nobrainer_class
    elsif object.is_a?(::NoBrainer::Document)
      cast = :nobrainer_document
    end
  end
  cast
end