Module: AwesomePrint::NoBrainer

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


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

def self.included(base)
  base.send :alias_method, :cast_without_no_brainer, :cast
  base.send :alias_method, :cast, :cast_with_no_brainer
end

Instance Method Details

#awesome_no_brainer_class(object) ⇒ Object

Format NoBrainer class object.



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

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

  # We want id first
  data = object.fields.sort_by { |key| key[0] == :id ? '_id' : key[0].to_s }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
    hash[c[0]] = :object
    hash
  end
  "class #{object} < #{object.superclass} " << awesome_hash(data)
end

#awesome_no_brainer_document(object) ⇒ Object

Format NoBrainer Document object.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/awesome_print/ext/no_brainer.rb', line 43

def awesome_no_brainer_document(object)
  return object.inspect if !defined?(::ActiveSupport::OrderedHash)

  data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
    hash[c[0].to_sym] = c[1]
    hash
  end
  if !object.errors.empty?
    data = {:errors => object.errors, :attributes => data}
  end
  "#{object} #{awesome_hash(data)}"
end

#cast_with_no_brainer(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/no_brainer.rb', line 16

def cast_with_no_brainer(object, type)
  cast = cast_without_no_brainer(object, type)
  if defined?(::NoBrainer::Document)
    if object.is_a?(Class) && object.ancestors.include?(::NoBrainer::Document)
      cast = :no_brainer_class
    elsif object.class.ancestors.include?(::NoBrainer::Document)
      cast = :no_brainer_document
    end
  end
  cast
end