Class: Scarpe::Components::HTML

Inherits:
Object
  • Object
show all
Defined in:
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,
  :svg,
  :h1,
  :h2,
  :h3,
  :h4,
  :h5,
].freeze
VOID_TAGS =
[:input, :img, :polygon, :source, :link, :path, :rect].freeze
TAGS =
( + VOID_TAGS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HTML



40
41
42
43
# File 'lib/scarpe/components/html.rb', line 40

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



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

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



35
36
37
# File 'lib/scarpe/components/html.rb', line 35

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

Instance Method Details

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



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

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

#p(*args, &block) ⇒ Object



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

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

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



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

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

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



83
84
85
# File 'lib/scarpe/components/html.rb', line 83

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

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



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

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



45
46
47
# File 'lib/scarpe/components/html.rb', line 45

def value
  @buffer
end