Class: Scarpe::Components::HTML

Inherits:
Object
  • Object
show all
Defined in:
scarpe-components/lib/scarpe/components/html.rb

Constant Summary collapse

CONTENT_TAGS =
[
  :div,
  :p,
  :button,
  :ul,
  :li,
  :textarea,
  :a,
  :video,
  :strong,
  :style,
  :progress,
  :em,
  :code,
  :defs,
  :marker,
  :u,
  :line,
  :span,
  :sub,
  :sup,
  :del,
  :svg,
  :h1,
  :h2,
  :h3,
  :h4,
  :h5,
].freeze
VOID_TAGS =
[:input, :img, :polygon, :source, :link, :path, :rect, :ellipse].freeze
TAGS =
(CONTENT_TAGS + VOID_TAGS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HTML

Returns a new instance of HTML.



43
44
45
46
# File 'scarpe-components/lib/scarpe/components/html.rb', line 43

def initialize(&block)
  @buffer = ""
  block.call(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'scarpe-components/lib/scarpe/components/html.rb', line 90

def method_missing(name, *args, &block)
  raise Scarpe::InvalidHTMLTag, "no method #{name} for #{self.class.name}" unless TAGS.include?(name)

  if VOID_TAGS.include?(name)
    raise Shoes::Errors::InvalidAttributeValueError, "void tag #{name} cannot have content" if block_given?

    @buffer += "<#{name}#{render_attributes(*args)} />"
  else
    @buffer += "<#{name}#{render_attributes(*args)}>"

    if block_given?
      result = block.call(self)
    else
      result = args.first
      @buffer += result if result.is_a?(String)
    end
    @buffer += result if result.is_a?(String)

    @buffer += "</#{name}>"
  end

  nil
end

Class Method Details

.render(&block) ⇒ Object



38
39
40
# File 'scarpe-components/lib/scarpe/components/html.rb', line 38

def render(&block)
  new(&block).value
end

Instance Method Details

#option(**attrs, &block) ⇒ Object



60
61
62
# File 'scarpe-components/lib/scarpe/components/html.rb', line 60

def option(**attrs, &block)
  tag(:option, **attrs, &block)
end

#p(*args, &block) ⇒ Object



56
57
58
# File 'scarpe-components/lib/scarpe/components/html.rb', line 56

def p(*args, &block)
  method_missing(:p, *args, &block)
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'scarpe-components/lib/scarpe/components/html.rb', line 52

def respond_to_missing?(name, include_all = false)
  TAGS.include?(name) || super(name, include_all)
end

#select(**attrs, &block) ⇒ Object



86
87
88
# File 'scarpe-components/lib/scarpe/components/html.rb', line 86

def select(**attrs, &block)
  tag(:select, **attrs, &block)
end

#tag(name, **attrs, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'scarpe-components/lib/scarpe/components/html.rb', line 64

def tag(name, **attrs, &block)
  if VOID_TAGS.include?(name)
    raise Shoes::Errors::InvalidAttributeValueError, "void tag #{name} cannot have content" if block_given?

    @buffer += "<#{name}#{render_attributes(attrs)} />"
  else
    @buffer += "<#{name}#{render_attributes(attrs)}>"

    if block_given?
      result = block.call(self)
    else
      result = attrs[:content]
      @buffer += result if result.is_a?(String)
    end
    @buffer += result if result.is_a?(String)

    @buffer += "</#{name}>"
  end

  nil
end

#valueObject



48
49
50
# File 'scarpe-components/lib/scarpe/components/html.rb', line 48

def value
  @buffer
end