Class: Praxis::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis-blueprints/renderer.rb

Defined Under Namespace

Classes: CircularRenderingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_nil: false) ⇒ Renderer

Returns a new instance of Renderer.



22
23
24
25
26
27
28
# File 'lib/praxis-blueprints/renderer.rb', line 22

def initialize(include_nil: false)
  @cache = Hash.new do |hash, key|
    hash[key] = {}
  end

  @include_nil = include_nil
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5
6
7
# File 'lib/praxis-blueprints/renderer.rb', line 5

def cache
  @cache
end

#include_nilObject (readonly)

Returns the value of attribute include_nil.



4
5
6
# File 'lib/praxis-blueprints/renderer.rb', line 4

def include_nil
  @include_nil
end

Instance Method Details

#_render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/praxis-blueprints/renderer.rb', line 58

def _render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
  if fields == true
    return case object
           when Attributor::Dumpable
             object.dump
           else
             object
           end
  end

  notification_payload = {
    blueprint: object,
    fields: fields,
    view: view
  }

  ActiveSupport::Notifications.instrument 'praxis.blueprint.render', notification_payload do
    fields.each_with_object({}) do |(key, subfields), hash|
      begin
        value = object._get_attr(key)
      rescue => e
        raise Attributor::DumpError, context: context, name: key, type: object.class, original_exception: e
      end

      if value.nil?
        hash[key] = nil if self.include_nil
        next
      end

      if subfields == true
        hash[key] = case value
                    when Attributor::Dumpable
                      value.dump
                    else
                      value
                    end
      else
        new_context = context + [key]
        hash[key] = render(value, subfields, context: new_context)
      end
    end
  end
end

#render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object

Renders an object using a given list of fields.

Parameters:

  • object (Object)

    the object to render

  • fields (Hash)

    the correct set of fields, as from FieldExpander



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/praxis-blueprints/renderer.rb', line 42

def render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
  if fields.is_a? Array
    sub_fields = fields[0]
    object.each_with_index.collect do |sub_object, i|
      sub_context = context + ["at(#{i})"]
      render(sub_object, sub_fields, view, context: sub_context)
    end
  elsif object.is_a? Praxis::Blueprint
    @cache[object.object_id][fields.object_id] ||= _render(object, fields, view, context: context)
  else
    _render(object, fields, view, context: context)
  end
rescue SystemStackError
  raise CircularRenderingError.new(object, context)
end

#render_collection(collection, member_fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object

Renders an a collection using a given list of per-member fields.

Parameters:

  • object (Object)

    the object to render

  • fields (Hash)

    the set of fields, as from FieldExpander, to apply to each member of the collection.



34
35
36
# File 'lib/praxis-blueprints/renderer.rb', line 34

def render_collection(collection, member_fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
  render(collection, [member_fields], view, context: context)
end