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



66
67
68
69
70
71
72
73
74
# File 'lib/wedge/html.rb', line 66

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.



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

def scope
  @scope
end

Class Method Details

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



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

def self.method_missing(tag, *args, &block)
  DSL.scope!(scope).new(tag.to_s, *args, &block)
end

.scope!(scope) ⇒ Object



87
88
89
90
91
# File 'lib/wedge/html.rb', line 87

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

Instance Method Details

#childrenObject



55
56
57
# File 'lib/wedge/html.rb', line 55

def children
  @children ||= []
end

#scopeObject



76
77
78
# File 'lib/wedge/html.rb', line 76

def scope
  self.class.scope
end

#to_htmlObject



50
51
52
53
# File 'lib/wedge/html.rb', line 50

def to_html
  @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}>"
end