Class: Wedge::HTML::DSL

Inherits:
Object show all
Defined in:
lib/wedge/html.rb

Overview

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, *args, &block) ⇒ DSL

Returns a new instance of DSL.



42
43
44
45
46
47
48
# File 'lib/wedge/html.rb', line 42

def initialize(tag, *args, &block)
  @tag         = tag
  @content     = args.find { |a| a.instance_of? String }
  @attributes  = args.find { |a| a.instance_of? Hash }
  @attr_string = []
  self.instance_eval &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



70
71
72
73
74
75
76
77
78
# File 'lib/wedge/html.rb', line 70

def method_missing(tag, *args, &block)
  if !TAGS.include?(tag.to_s) && scope.respond_to?(tag, true)
    scope.send(tag, *args, &block)
  else
    child = DSL.scope!(scope).new(tag.to_s, *args, &block)
    children << child
    child
  end
end

Class Attribute Details

.scopeObject

Returns the value of attribute scope.



90
91
92
# File 'lib/wedge/html.rb', line 90

def scope
  @scope
end

Class Method Details

.html(&block) ⇒ Object

fix: opal bug. for some reason this can’t be in the class << self block.



85
86
87
# File 'lib/wedge/html.rb', line 85

def self.html &block
  DSL.scope!(scope).new(nil, nil, &block)
end

.scope!(scope) ⇒ Object



92
93
94
95
96
# File 'lib/wedge/html.rb', line 92

def scope! scope
  klass = Class.new(self)
  klass.instance_variable_set(:@scope, scope)
  klass
end

Instance Method Details

#childrenObject



59
60
61
# File 'lib/wedge/html.rb', line 59

def children
  @children ||= []
end

#scopeObject



80
81
82
# File 'lib/wedge/html.rb', line 80

def scope
  self.class.scope
end

#to_htmlObject



50
51
52
53
54
55
56
57
# File 'lib/wedge/html.rb', line 50

def to_html
  if @tag
    @attr_string << " #{@attributes.map {|k,v| "#{k}=#{v.to_s.inspect}" }.join(" ")}" if @attributes
    "<#{@tag}#{@attr_string.join}>#{@content}#{children.map(&:to_html).join}</#{@tag}>"
  else
    "#{@content}#{children.map(&:to_html).join}"
  end
end