Class: Code::Object::Html

Inherits:
Code::Object show all
Defined in:
lib/code/object/html.rb

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #as_json, as_json, #call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, code_different, #code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, code_to_json, #code_to_json, #falsy?, falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*_args, **_kargs) ⇒ Html

Returns a new instance of Html.



6
7
8
# File 'lib/code/object/html.rb', line 6

def initialize(*_args, **_kargs, &)
  @raw = nil
end

Class Method Details

.call(**args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/code/object/html.rb', line 10

def self.call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)

  case operator.to_s
  when "link_to"
    sig(args) { [Object.maybe, Object.maybe] }
    code_link_to(*arguments.raw)
  when "escape"
    sig(args) { Object.maybe }
    code_escape(*arguments.raw)
  else
    super
  end
end

.code_escape(string = nil) ⇒ Object



35
36
37
38
39
# File 'lib/code/object/html.rb', line 35

def self.code_escape(string = nil)
  string ||= Nothing.new

  String.new(CGI.escapeHTML(string.raw.to_s))
end


26
27
28
29
30
31
32
33
# File 'lib/code/object/html.rb', line 26

def self.code_link_to(text = nil, href = nil)
  text ||= Nothing.new
  href ||= Nothing.new

  String.new(<<~LINK.strip)
    <a href="#{CGI.escapeHTML(href.raw.to_s)}">#{CGI.escapeHTML(text.raw.to_s)}</a>
  LINK
end