Class: Nokolexbor::Builder::NodeBuilder

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

Overview

Wraps a built node and exposes a fluent API for setting classes and ids:

div.container        # => <div class="container">
div.box.highlight    # => <div class="box highlight">
div.thing!           # => <div id="thing">
div.container.hero!  # => <div class="container" id="hero">

Instance Method Summary collapse

Constructor Details

#initialize(node, doc_builder) ⇒ NodeBuilder

:nodoc:



170
171
172
173
# File 'lib/nokolexbor/builder.rb', line 170

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/nokolexbor/builder.rb', line 183

def method_missing(method, *args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}

  case method.to_s
  when /^(.*)!$/
    @node["id"]  = Regexp.last_match(1)
    @node.content = args.first if args.first
  when /^(.*)=$/
    @node[Regexp.last_match(1)] = args.first
  else
    @node["class"] =
      ((@node["class"] || "").split(/\s/) + [method.to_s]).join(" ")
    @node.content = args.first if args.first
  end

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

  if block
    old_parent           = @doc_builder.parent
    @doc_builder.parent  = @node
    arity = @doc_builder.arity || block.arity
    value = if arity <= 0
      @doc_builder.instance_eval(&block)
    else
      yield(@doc_builder)
    end
    @doc_builder.parent = old_parent
    return value
  end

  self
end

Instance Method Details

#[](k) ⇒ Object



179
180
181
# File 'lib/nokolexbor/builder.rb', line 179

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

#[]=(k, v) ⇒ Object



175
176
177
# File 'lib/nokolexbor/builder.rb', line 175

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

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

:nodoc:

Returns:

  • (Boolean)


218
219
220
# File 'lib/nokolexbor/builder.rb', line 218

def respond_to_missing?(_method, _include_private = false) # :nodoc:
  true
end