Class: Browser::DOM::Builder

Inherits:
Object
  • Object
show all
Defined in:
opal/browser/dom/builder.rb

Constant Summary collapse

NEW_PAGGIO =
(Paggio::HTML.instance_method(:build!) rescue false)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, builder = nil, &block) ⇒ Builder

Returns a new instance of Builder.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'opal/browser/dom/builder.rb', line 44

def initialize(document, builder=nil, &block)
  @document = document

  # Compatibility issue due to an unreleased Paggio gem.
  # Let's try to support both versions. When Paggio is released,
  # we may remove it.

  if NEW_PAGGIO
    @builder = Paggio::HTML.new(defer: true, &block)

    build = proc do
      @builder.build!(force_call: !!builder)
      @roots = @builder.each.map { |e| Builder.build(self, e) }
    end

    if builder
      builder.extend!(@builder, &build)
    else
      build.()
    end
  else
    @builder = Paggio::HTML.new(&block)
    @roots = @builder.each.map { |e| Builder.build(self, e) }
  end
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



40
41
42
# File 'opal/browser/dom/builder.rb', line 40

def document
  @document
end

#elementObject (readonly)

Returns the value of attribute element.



40
41
42
# File 'opal/browser/dom/builder.rb', line 40

def element
  @element
end

Class Method Details

.build(builder, item) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'opal/browser/dom/builder.rb', line 30

def self.build(builder, item)
  to_h.each {|klass, block|
    if klass === item
      return block.call(builder, item)
    end
  }

  raise ArgumentError, "cannot build unknown item #{item}"
end

.for(klass, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'opal/browser/dom/builder.rb', line 22

def self.for(klass, &block)
  if block
    to_h[klass] = block
  else
    to_h[klass]
  end
end

.to_hObject



18
19
20
# File 'opal/browser/dom/builder.rb', line 18

def self.to_h
  @builders ||= {}
end

Instance Method Details

#to_aObject



70
71
72
# File 'opal/browser/dom/builder.rb', line 70

def to_a
  @roots
end