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
14
# File 'lib/rabl-rails/visitors/to_hash.rb', line 7

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

  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.



85
86
87
# File 'lib/rabl-rails/visitors/to_hash.rb', line 85

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



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

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

#resultObject



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

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



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

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

#visit_Attribute(n) ⇒ Object



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

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

#visit_Child(n) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rabl-rails/visitors/to_hash.rb', line 31

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



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rabl-rails/visitors/to_hash.rb', line 41

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



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

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

#visit_Glue(n) ⇒ Object



58
59
60
61
# File 'lib/rabl-rails/visitors/to_hash.rb', line 58

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