Class: Makiri::XML::Builder::NodeBuilder

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

Overview

The value returned by each element call, wrapping the just-inserted node so attributes can be added with the Nokogiri terse-chain syntax:

xml.object.classy.thing!   # => <object class="classy" id="thing"/>

A plain method name appends to the class attribute, name! sets id (and content if given), name= sets that attribute, a trailing Hash adds each key as an attribute, and a block descends into the node. [] / []= read and write attributes directly. Semantics mirror Nokogiri::XML::Builder::NodeBuilder.

Instance Method Summary collapse

Constructor Details

#initialize(node, doc_builder) ⇒ NodeBuilder

Returns a new instance of NodeBuilder.



219
220
221
222
# File 'lib/makiri/xml/builder.rb', line 219

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/makiri/xml/builder.rb', line 234

def method_missing(method, *args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  case method.to_s
  when /\A(.*)!\z/
    @node["id"] = Regexp.last_match(1)
    @node.content = args.first if args.first
  when /\A(.*)=\z/
    @node[Regexp.last_match(1)] = args.first
  else
    append_attr("class", method.to_s)
    @node.content = args.first if args.first
  end

  opts.each { |key, value| append_attr(key.to_s, value) }

  # Descend into this node for a nested block via the builder's own parent
  # stack (with its ensure-based restore), rather than re-rooting it by hand.
  return @doc_builder.descend(@node, &block) if block

  self
end

Instance Method Details

#[](key) ⇒ Object

Read an attribute of the wrapped node.



225
226
227
# File 'lib/makiri/xml/builder.rb', line 225

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

#[]=(key, value) ⇒ Object

Write an attribute of the wrapped node.



230
231
232
# File 'lib/makiri/xml/builder.rb', line 230

def []=(key, value)
  @node[key] = value
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/makiri/xml/builder.rb', line 256

def respond_to_missing?(_name, _include_private = false)
  true
end