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, #visit_Array

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.



107
108
109
# File 'lib/rabl-rails/visitors/to_hash.rb', line 107

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rabl-rails/visitors/to_hash.rb', line 85

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

#visit_Attribute(n) ⇒ Object



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

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



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

def visit_Child n
  object = object_from_data(_resource, n)

  @_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



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

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

    if n.merge?
      raise RablRails::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



68
69
70
# File 'lib/rabl-rails/visitors/to_hash.rb', line 68

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

#visit_Const(n) ⇒ Object



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

def visit_Const n
  @_result[n.name] = n.value
end

#visit_Extend(n) ⇒ Object



72
73
74
75
76
77
# File 'lib/rabl-rails/visitors/to_hash.rb', line 72

def visit_Extend n
  @_locals = n.locals
  @_result.merge!(sub_visit(_resource, n.nodes))
ensure
  @_locals = {}
end

#visit_Glue(n) ⇒ Object



37
38
39
40
# File 'lib/rabl-rails/visitors/to_hash.rb', line 37

def visit_Glue n
  object = object_from_data(_resource, n)
  @_result.merge!(sub_visit(object, n.nodes)) if object
end

#visit_Lookup(n) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rabl-rails/visitors/to_hash.rb', line 59

def visit_Lookup n
  object  = object_from_data(nil, n)
  key     = _resource.public_send(n.field)
  value   = object[key]
  value   = !!value if n.cast_to_boolean?

  @_result[n.name] = value
end

#visit_Polymorphic(n) ⇒ Object



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

def visit_Polymorphic n
  template_path = n.template_lambda.call(_resource)
  template = RablRails::Library.instance.compile_template_from_path(template_path, @_context)
  @_result.merge!(sub_visit(_resource, template.nodes))
end