Class: Nokogiri::XML::Builder::NodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/builder.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(node, doc_builder) ⇒ NodeBuilder

Returns a new instance of NodeBuilder.



403
404
405
406
# File 'lib/nokogiri/xml/builder.rb', line 403

def initialize(node, doc_builder)
  @node = node
  @doc_builder = doc_builder
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/nokogiri/xml/builder.rb', line 416

def method_missing(method, *args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  case method.to_s
  when /^(.*)!$/
    @node["id"] = $1
    @node.content = args.first if args.first
  when /^(.*)=/
    @node[$1] = args.first
  else
    @node["class"] =
      ((@node["class"] || "").split(/\s/) + [method.to_s]).join(" ")
    @node.content = args.first if args.first
  end

  # Assign any extra options
  opts.each do |k, v|
    @node[k.to_s] = ((@node[k.to_s] || "").split(/\s/) + [v]).join(" ")
  end

  if block_given?
    old_parent = @doc_builder.parent
    @doc_builder.parent = @node
    value = @doc_builder.instance_eval(&block)
    @doc_builder.parent = old_parent
    return value
  end
  self
end

Instance Method Details

#[](k) ⇒ Object



412
413
414
# File 'lib/nokogiri/xml/builder.rb', line 412

def [](k)
  @node[k]
end

#[]=(k, v) ⇒ Object



408
409
410
# File 'lib/nokogiri/xml/builder.rb', line 408

def []=(k, v)
  @node[k] = v
end