Module: AwesomePrint::NoBrainer

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

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.




30
31
32
33
34
35
36
37
38
39
40
# File 'lib/awesome_print/ext/nobrainer.rb', line 30

def awesome_nobrainer_class(object)
  name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
  data = Hash[object.fields.map do |field, options|
    [field, (options[:type] || Object).to_s.underscore.to_sym]
  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_nobrainer_document(object) ⇒ Object

Format NoBrainer Document object.




44
45
46
47
48
# File 'lib/awesome_print/ext/nobrainer.rb', line 44

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.




16
17
18
19
20
21
22
23
24
25
26
# File 'lib/awesome_print/ext/nobrainer.rb', line 16

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