Class: Visitors::ToHash

Inherits:
Visitor show all
Includes:
RablRails::Helpers
Defined in:
lib/rabl-rails/visitors/to_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RablRails::Helpers

#collection?

Methods inherited from Visitor

#visit

Constructor Details

#initialize(view_context, resource = nil) ⇒ ToHash

Returns a new instance of ToHash.



7
8
9
10
11
12
13
# File 'lib/rabl-rails/visitors/to_hash.rb', line 7

def initialize(view_context, resource = nil)
  @_context  = view_context
  @_result   = {}
  @_resource = resource

  copy_instance_variables_from_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

If a method is called inside a ‘node’ property or a ‘if’ lambda it will be passed to context if it exists or treated as a standard missing method.



82
83
84
# File 'lib/rabl-rails/visitors/to_hash.rb', line 82

def method_missing(name, *args, &block)
  @_context.respond_to?(name) ? @_context.send(name, *args, &block) : super
end

Instance Attribute Details

#_resourceObject (readonly)

Returns the value of attribute _resource.



5
6
7
# File 'lib/rabl-rails/visitors/to_hash.rb', line 5

def _resource
  @_resource
end

Instance Method Details

#reset_for(resource) ⇒ Object



15
16
17
18
# File 'lib/rabl-rails/visitors/to_hash.rb', line 15

def reset_for(resource)
  @_resource = resource
  @_result = {}
end

#resultObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rabl-rails/visitors/to_hash.rb', line 60

def result
  case RablRails.configuration.result_flags
  when 0
    @_result
  when 1
    @_result.each { |k, v| @_result[k] = '' if v == nil }
  when 2, 3
    @_result.each { |k, v| @_result[k] = nil if v == '' }
  when 4, 5
    @_result.delete_if { |_, v| v == nil }
  when 6
    @_result.delete_if { |_, v| v == nil || v == '' }
  end
end

#visit_Array(n) ⇒ Object



20
21
22
# File 'lib/rabl-rails/visitors/to_hash.rb', line 20

def visit_Array n
  n.each { |i| visit i }
end

#visit_Attribute(n) ⇒ Object



24
25
26
# File 'lib/rabl-rails/visitors/to_hash.rb', line 24

def visit_Attribute n
  n.each { |k, v| @_result[k] = _resource.send(v) }
end

#visit_Child(n) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rabl-rails/visitors/to_hash.rb', line 28

def visit_Child n
  object = object_from_data(_resource, n.data, n.instance_variable_data?)

  @_result[n.name] = if object
    collection?(object) ? object.map { |o| sub_visit(o, n.nodes) } : sub_visit(object, n.nodes)
  else
    nil
  end
end

#visit_Code(n) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rabl-rails/visitors/to_hash.rb', line 38

def visit_Code n
  if !n.condition || instance_exec(_resource, &(n.condition))
    result = instance_exec _resource, &(n.block)

    if n.merge?
      raise RablRails::Renderer::PartialError, '`merge` block should return a hash' unless result.is_a?(Hash)
      @_result.merge!(result)
    else
      @_result[n.name] = result
    end
  end
end

#visit_Condition(n) ⇒ Object



51
52
53
# File 'lib/rabl-rails/visitors/to_hash.rb', line 51

def visit_Condition n
  @_result.merge!(sub_visit(_resource, n.nodes)) if instance_exec _resource, &(n.condition)
end

#visit_Glue(n) ⇒ Object



55
56
57
58
# File 'lib/rabl-rails/visitors/to_hash.rb', line 55

def visit_Glue n
  object = object_from_data(_resource, n.data, n.instance_variable_data?)
  @_result.merge! sub_visit(object, n.template.nodes)
end