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

#methods, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

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

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #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, #something?, #succ, #to_code, #to_i, #to_json, #truthy?

Constructor Details

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

Returns a new instance of Html.



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

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

Class Method Details

.call(**args) ⇒ Object



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

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 "escape"
    sig(args) { Object.maybe }
    code_escape(*code_arguments.raw)
  else
    super
  end
end

.code_escape(string = nil) ⇒ Object



31
32
33
34
35
# File 'lib/code/object/html.rb', line 31

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

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

Instance Method Details

#call(**args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/code/object/html.rb', line 37

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



67
68
69
70
71
# File 'lib/code/object/html.rb', line 67

def code_at_css(query)
  code_query = query.to_code

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

#code_css(query) ⇒ Object



61
62
63
64
65
# File 'lib/code/object/html.rb', line 61

def code_css(query)
  code_query = query.to_code

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

#code_map(argument, **globals) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/code/object/html.rb', line 73

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



92
93
94
# File 'lib/code/object/html.rb', line 92

def code_to_string
  String.new(raw.text)
end

#to_sObject



88
89
90
# File 'lib/code/object/html.rb', line 88

def to_s
  raw.text
end