Class: Code::Object::Html

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes included from Concerns::Shared

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#code_new, code_new, maybe, name, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_inclusive_range, #code_inspect, #code_name, #code_or, #code_self, code_set, #code_set, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #succ, #to_code, #to_json, #truthy?

Constructor Details

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

Returns a new instance of Html.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/code/object/html.rb', line 6

def initialize(*args, **_kargs, &)
  @raw =
    if args.first.is_an?(Html)
      args.first.raw
    elsif args.first.is_a?(::Nokogiri::XML::NodeSet)
      args.first
    elsif args.first.is_a?(Nokogiri::XML::Node)
      args.first
    else
      Nokogiri.HTML(args.first.to_s)
    end
end

Class Method Details

.call(**args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code/object/html.rb', line 19

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code

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

.code_escape(string = nil) ⇒ Object



46
47
48
49
50
# File 'lib/code/object/html.rb', line 46

def self.code_escape(string = nil)
  code_string = string.to_code

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


35
36
37
38
39
40
41
42
43
44
# File 'lib/code/object/html.rb', line 35

def self.code_link_to(text = nil, href = nil)
  code_text = text.to_code
  code_href = href.to_code

  String.new("    <a\n      href=\"\#{CGI.escapeHTML(code_href.to_s)}\"\n    >\#{CGI.escapeHTML(code_text.to_s)}</a>\n  LINK\nend\n".strip)

Instance Method Details

#call(**args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/code/object/html.rb', line 52

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first
  globals = multi_fetch(args, *GLOBALS)

  case code_operator.to_s
  when "css"
    sig(args) { String }
    code_css(code_value)
  when "at_css"
    sig(args) { String }
    code_at_css(code_value)
  when "map"
    sig(args) { Function }
    code_map(code_value, **globals)
  when "to_string"
    sig(args)
    code_to_string
  else
    super
  end
end

#code_at_css(query) ⇒ Object



82
83
84
85
86
# File 'lib/code/object/html.rb', line 82

def code_at_css(query)
  code_query = query.to_code

  Html.new(raw.at_css(code_query.raw))
end

#code_css(query) ⇒ Object



76
77
78
79
80
# File 'lib/code/object/html.rb', line 76

def code_css(query)
  code_query = query.to_code

  Html.new(raw.css(code_query.raw))
end

#code_map(argument, **globals) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/code/object/html.rb', line 88

def code_map(argument, **globals)
  code_argument = argument.to_code

  List.new(
    raw.map.with_index do |element, index|
      code_argument.call(
        arguments: List.new([element.to_code, Integer.new(index), self]),
        **globals
      )
    rescue Error::Next => e
      e.code_value
    end
  )
end

#code_to_stringObject



107
108
109
# File 'lib/code/object/html.rb', line 107

def code_to_string
  String.new(raw.text)
end

#to_sObject



103
104
105
# File 'lib/code/object/html.rb', line 103

def to_s
  raw.text
end