1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
module Visitors
class ToHash < Visitor
include RablRails::Helpers
attr_reader :_resource
def initialize(view_context, resource = nil)
@_context = view_context
@_result = {}
@_resource = resource
copy_instance_variables_from_context
end
def reset_for(resource)
@_resource = resource
@_result = {}
end
def visit_Array n
n.each { |i| visit i }
end
def visit_Attribute n
n.each { |k, v| @_result[k] = _resource.send(v) }
end
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
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
def visit_Condition n
@_result.merge!(sub_visit(_resource, n.nodes)) if instance_exec _resource, &(n.condition)
end
def visit_Glue n
object = object_from_data(_resource, n.data, n.instance_variable_data?)
@_result.merge! sub_visit(object, n.template.nodes)
end
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
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.
#
def method_missing(name, *args, &block)
@_context.respond_to?(name) ? @_context.send(name, *args, &block) : super
end
#
# Allow to use partial inside of node blocks (they are evaluated at
# rendering time).
#
def partial(template_path, options = {})
raise RablRails::Renderer::PartialError.new("No object was given to partial #{template_path}") unless options[:object]
object = options[:object]
return [] if object.respond_to?(:empty?) && object.empty?
template = RablRails::Library.instance.compile_template_from_path(template_path, @_context)
if object.respond_to?(:each)
object.map { |o| sub_visit o, template.nodes }
else
sub_visit object, template.nodes
end
end
private
def copy_instance_variables_from_context
@_context.instance_variable_get(:@_assigns).each_pair { |k, v|
instance_variable_set("@#{k}", v) unless k.to_s.start_with?('_')
}
end
def sub_visit(resource, nodes)
old_result, old_resource, @_result = @_result, @_resource, {}
reset_for resource
visit nodes
result
ensure
@_result, @_resource = old_result, old_resource
end
def object_from_data(resource, symbol, is_variable)
return resource if symbol == nil
if is_variable
instance_variable_get(symbol)
else
resource.respond_to?(symbol) ? resource.send(symbol) : @_context.send(symbol)
end
end
end
end
|