Class: Volt::ComponentViewScope

Inherits:
ViewScope show all
Defined in:
lib/volt/server/html_parser/component_view_scope.rb

Instance Attribute Summary

Attributes inherited from ViewScope

#binding_number, #bindings, #html, #path

Instance Method Summary collapse

Methods inherited from ViewScope

#<<, #add_binding, #add_component, #add_content_binding, #add_each, #add_else, #add_if, #add_template, #add_textarea, #add_yield, #last_method_name, #parent_fetcher, #save_binding

Methods included from AttributeScope

#add_id_to_attributes, #add_multiple_attribute, #add_single_attribute, #add_string_template_renderer, #attribute_string, #binding_parts_and_count, #getter_to_setter, #process_attribute, #process_attributes, #process_event_binding

Constructor Details

#initialize(handler, path, tag_name, attributes, unary) ⇒ ComponentViewScope

The path passed in is the path used to lookup view’s. The path from the tag is passed in as tag_name



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
# File 'lib/volt/server/html_parser/component_view_scope.rb', line 5

def initialize(handler, path, tag_name, attributes, unary)
  super(handler, path)

  @binding_in_path = path

  component_name = tag_name[1..-1].tr(':', '/')

  data_hash = []
  attributes.each_pair do |name, value|
    name = name.tr('-', '_')
    parts, binding_count = binding_parts_and_count(value)

    # if this attribute has bindings
    if binding_count > 0
      if binding_count > 1
        # Multiple bindings
      elsif parts.size == 1 && binding_count == 1
        # A single binding
        getter = value[2...-2].strip
        data_hash << "#{name.inspect} => Proc.new { #{getter} }"

        setter = getter_to_setter(getter)
        data_hash << "#{(name + '=').inspect} => Proc.new { |val| #{setter} }"

        # Add an _parent fetcher.  Useful for things like volt-fields to get the parent model.
        parent = parent_fetcher(getter)

        # TODO: This adds some overhead, perhaps there is a way to compute this dynamically on the
        # front-end.
        data_hash << "#{(name + '_parent').inspect} => Proc.new { #{parent} }"

        # Add a _last_method property.  This is useful
        data_hash << "#{(name + '_last_method').inspect} => #{last_method_name(getter).inspect}"
      end
    else
      # String
      data_hash << "#{name.inspect} => #{value.inspect}"
    end
  end

  @arguments = "#{component_name.inspect}, { #{data_hash.join(',')} }"
end

Instance Method Details

#close_scopeObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/volt/server/html_parser/component_view_scope.rb', line 48

def close_scope
  binding_number                    = @handler.scope[-2].binding_number
  @handler.scope[-2].binding_number += 1
  @path                             += "/__template/#{binding_number}"

  super

  @handler.html << "<!-- $#{binding_number} --><!-- $/#{binding_number} -->"
  @handler.scope.last.save_binding(binding_number, "lambda { |__p, __t, __c, __id| Volt::ComponentBinding.new(__p, __t, __c, __id, #{@binding_in_path.inspect}, Proc.new { [#{@arguments}] }, #{@path.inspect}) }")
end