File: to_hash.rb

package info (click to toggle)
ruby-rabl-rails 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 288 kB
  • sloc: ruby: 1,630; makefile: 4
file content (176 lines) | stat: -rw-r--r-- 4,706 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module Visitors
  class ToHash < Visitor
    include RablRails::Helpers

    attr_reader :_resource

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

      copy_instance_variables_from_context
    end

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

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

    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

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

    def visit_Fetch n
      hash   = object_from_data(_resource, n)
      key    = _resource.public_send(n.field)
      object = hash[key]

      @_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::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_Const n
      @_result[n.name] = n.value
    end

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

      @_result[n.name] = value
    end

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

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

    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

    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

    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

    def locals
      @_locals
    end

    #
    # Allow to use partial inside of node blocks (they are evaluated at
    # rendering time).
    #
    def partial(template_path, options = {})
      raise RablRails::PartialError.new("No object was given to partial #{template_path}") unless options[:object]
      object = options[:object]
      @_locals = options[:locals].freeze

      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
    ensure
      @_locals = {}
    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?('_'.freeze)
      }
    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, node)
      return resource if node.data == nil

      symbol = node.data
      if node.instance_variable_data?
        instance_variable_get(symbol)
      else
        resource.respond_to?(symbol) ? resource.send(symbol) : @_context.send(symbol)
      end
    end
  end
end