Class: Dominate::Dom

Inherits:
Object show all
Defined in:
lib/dominate/dom.rb

Constant Summary collapse

YIELD_REGEX =
/<!--(?:\s*|)@yield(?:\s*|)-->/
PARTIAL_REGEX =
/<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/
PARTIAL_REGEX_WITHIN =
/<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->(.*?)<!--(?:\s*|)\/partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_html, instance = false, config = {}) ⇒ Dom



9
10
11
12
13
14
15
16
# File 'lib/dominate/dom.rb', line 9

def initialize raw_html, instance = false, config = {}
  @raw_html = raw_html
  @instance = instance
  @config   = (Dominate.config.to_h.merge config).to_deep_ostruct

  set_doc raw_html
  load_html if Dominate.config.view_path
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/dominate/dom.rb', line 3

def config
  @config
end

#docObject

Returns the value of attribute doc.



3
4
5
# File 'lib/dominate/dom.rb', line 3

def doc
  @doc
end

#instanceObject

Returns the value of attribute instance.



3
4
5
# File 'lib/dominate/dom.rb', line 3

def instance
  @instance
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/dominate/dom.rb', line 3

def options
  @options
end

#raw_htmlObject

Returns the value of attribute raw_html.



3
4
5
# File 'lib/dominate/dom.rb', line 3

def raw_html
  @raw_html
end

Instance Method Details

#apply(data, &block) ⇒ Object



64
65
66
67
68
# File 'lib/dominate/dom.rb', line 64

def apply data, &block
  @scope.apply data, &block

  self
end

#apply_instanceObject



70
71
72
73
# File 'lib/dominate/dom.rb', line 70

def apply_instance
  reset_html
  Scope.new(instance, config, doc).apply_instance
end

#htmlObject



57
58
59
60
61
62
# File 'lib/dominate/dom.rb', line 57

def html
  @html ||= begin
    apply_instance if instance
    "#{doc}"
  end
end

#load_htmlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dominate/dom.rb', line 18

def load_html
  load_layout if config.layout

  inner_html = doc.inner_html

  updated_html = inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m|
    match   = m.strip.match(PARTIAL_REGEX)
    partial = match[1]
    HTML.load_file "#{view_path}/#{partial}", config, instance
  end

  set_doc updated_html if updated_html

  doc.traverse do |e|
    if match = e.to_html.strip.match(PARTIAL_REGEX)
      partial = match[1]
      e.swap Nokogiri::HTML.fragment(
        HTML.load_file "#{view_path}/#{partial}", config, instance
      )
    end
  end
end

#load_layoutObject



41
42
43
44
45
46
# File 'lib/dominate/dom.rb', line 41

def load_layout
  path       = "#{config.view_path}/#{config.layout}"
  html       = HTML.load_file path, config, instance
  inner_html = doc.inner_html
  set_doc html.gsub YIELD_REGEX, inner_html
end

#reset_htmlObject



75
76
77
# File 'lib/dominate/dom.rb', line 75

def reset_html
  @html = false
end

#scope(name, &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/dominate/dom.rb', line 48

def scope name, &block
  root_doc = doc.search("[data-scope='#{name}']")
  @scope   = Scope.new instance, config, root_doc

  Instance.new(instance, config).instance_exec(root_doc, &block) if block

  self
end