module Bob::Compiler::Editable
Public Class Methods
new(*)
click to toggle source
Calls superclass method
# File lib/bob/compiler/editable.rb, line 7 def initialize(*) super @next_editable_id = Hash.new(0) end
Private Instance Methods
after_element(node, root)
click to toggle source
Calls superclass method
# File lib/bob/compiler/editable.rb, line 13 def after_element(node, root) push_editable(node) if is_editable?(node) super end
editable_name_for(node, type)
click to toggle source
# File lib/bob/compiler/editable.rb, line 43 def editable_name_for(node, type) identifier_from_attribute(node, "bob-editable-name", false) || generate_editable_name(type) end
editable_options_for(node)
click to toggle source
# File lib/bob/compiler/editable.rb, line 47 def editable_options_for(node) node.attribute_nodes.each_with_object({}) do |attr, options| if attr.name.start_with?("bob-option-") options[ attr.name[11..-1] ] = parse_option_value( attr.value ) end end end
editable_type_for(node)
click to toggle source
# File lib/bob/compiler/editable.rb, line 39 def editable_type_for(node) identifier_from_attribute(node, "bob-editable-type", false) || identifier_from_attribute(node, "bob-editable") end
generate_editable_id(type)
click to toggle source
# File lib/bob/compiler/editable.rb, line 59 def generate_editable_id(type) id = @next_editable_id[type] @next_editable_id[type] += 1 id end
generate_editable_name(type)
click to toggle source
# File lib/bob/compiler/editable.rb, line 55 def generate_editable_name(type) "#{type}#{generate_editable_id(type)}" end
is_editable?(node)
click to toggle source
# File lib/bob/compiler/editable.rb, line 18 def is_editable?(node) node.has_attribute?("bob-editable") || node.has_attribute?("bob-editable-type") end
parse_option_value(value)
click to toggle source
# File lib/bob/compiler/editable.rb, line 65 def parse_option_value(value) JSON.parse(value, quirks_mode: true) rescue value end
push_editable(node)
click to toggle source
# File lib/bob/compiler/editable.rb, line 22 def push_editable(node) type = editable_type_for(node) if type name = editable_name_for(node, type) options = editable_options_for(node) @buffer << "descriptor.editables.push({" @buffer << " type: #{quote type}," @buffer << " name: #{quote name}," @buffer << " element: dom.getElement()," @buffer << " options: " << quote_options(options, node) @buffer << "});" end end
quote_options(options, node)
click to toggle source
# File lib/bob/compiler/editable.rb, line 69 def quote_options(options, node) buffer = Buffer.new(2) buffer << "{" buffer.indented do options.each do |(k, v)| buffer << quote(k) << ": " << (String === v ? quote_and_substitute(v, node) : quote(v)) << "," end end buffer << "}" buffer.to_s end