Class: RideTheStreetcar::QuerySelector

Inherits:
Object
  • Object
show all
Includes:
Streamlined::Renderable
Defined in:
lib/ride_the_streetcar/query_selector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selectors, all: false) ⇒ QuerySelector

Returns a new instance of QuerySelector.



11
12
13
14
15
16
17
18
19
20
# File 'lib/ride_the_streetcar/query_selector.rb', line 11

def initialize(selectors, all: false)
  @selectors = if selectors.respond_to?(:to_dom_selector)
                 selectors.to_dom_selector
               elsif selectors.is_a?(Symbol)
                 selectors.to_s.tr("_", "-")
               else
                 selectors.to_s
               end
  @all = all
end

Instance Attribute Details

#allBoolean (readonly)

Returns:

  • (Boolean)


9
10
11
# File 'lib/ride_the_streetcar/query_selector.rb', line 9

def all
  @all
end

#selectorsString (readonly)

Returns:

  • (String)


6
7
8
# File 'lib/ride_the_streetcar/query_selector.rb', line 6

def selectors
  @selectors
end

Instance Method Details

#__insert(code) ⇒ Object Also known as: before, prepend, append, after



64
65
66
67
# File 'lib/ride_the_streetcar/query_selector.rb', line 64

def __insert(code)
  statements << { __callee__ => { code: } }
  self
end

#applyObject



78
79
80
81
# File 'lib/ride_the_streetcar/query_selector.rb', line 78

def apply(...)
  Apply.new(...).tap { statements << { apply: { obj: _1 } } }
  self
end

#class_listObject



42
43
44
# File 'lib/ride_the_streetcar/query_selector.rb', line 42

def class_list
  ClassList.new.tap { statements << { class_list: { obj: _1 } } }
end

#customObject



107
108
109
# File 'lib/ride_the_streetcar/query_selector.rb', line 107

def custom(...)
  Custom.new(...).tap { statements << { custom: { obj: _1 } } }
end

#dispatch_event(name, bubbles: false, cancelable: false, composed: false, detail: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



94
95
96
97
98
99
# File 'lib/ride_the_streetcar/query_selector.rb', line 94

def dispatch_event(name, bubbles: false, cancelable: false, composed: false, detail: nil) # rubocop:disable Metrics/ParameterLists
  extra_details = detail.nil? ? {} : { detail: detail.to_json }

  statements << { dispatch_event: { name:, bubbles:, cancelable:, composed:, **extra_details } }
  self
end

#inner_html=(code) ⇒ Object Also known as: inner_html, outer_html



24
25
26
27
# File 'lib/ride_the_streetcar/query_selector.rb', line 24

def inner_html=(code)
  statements << { inner_html: { code: } }
  self # rubocop:disable Lint/Void
end

#jsObject

TODO: setTimeout



103
104
105
# File 'lib/ride_the_streetcar/query_selector.rb', line 103

def js
  JS.new.tap { statements << { js: { obj: _1 } } }
end

#outer_html=(code) ⇒ Object



30
31
32
33
# File 'lib/ride_the_streetcar/query_selector.rb', line 30

def outer_html=(code)
  statements << { outer_html: { code: } }
  self # rubocop:disable Lint/Void
end

#removeObject



73
74
75
76
# File 'lib/ride_the_streetcar/query_selector.rb', line 73

def remove
  statements << { remove: {} }
  self
end

#remove_attribute(name) ⇒ Object



52
53
54
55
56
# File 'lib/ride_the_streetcar/query_selector.rb', line 52

def remove_attribute(name)
  name = normalize_name(name)
  statements << { remove_attribute: { name: } }
  self
end

#set_attribute(name, value) ⇒ Object



46
47
48
49
50
# File 'lib/ride_the_streetcar/query_selector.rb', line 46

def set_attribute(name, value)
  name = normalize_name(name)
  statements << { set_attribute: { name:, value: } }
  self
end

#set_propertyObject



83
84
85
86
# File 'lib/ride_the_streetcar/query_selector.rb', line 83

def set_property(...)
  SetProperty.new(...).tap { statements << { set_property: { obj: _1 } } }
  self
end

#set_style(name, value) ⇒ Object



88
89
90
91
92
# File 'lib/ride_the_streetcar/query_selector.rb', line 88

def set_style(name, value)
  name = name.to_s.gsub(%r!_[a-z]!) { _1[1].upcase }
  statements << { set_style: { name:, value: } }
  self
end

#statementsObject



22
# File 'lib/ride_the_streetcar/query_selector.rb', line 22

def statements = @statements ||= []

#templateObject



111
112
113
114
115
116
117
118
# File 'lib/ride_the_streetcar/query_selector.rb', line 111

def template
  html->{ <<~HTML
    <sc-query-selector#{text->{ " all" } if all} selectors="#{text->{ selectors }}">#{
      html_map(statements) { |statement| output_statement(statement) }
    }</sc-query-selector>
  HTML
  }
end

#text_content=(code) ⇒ Object Also known as: text_content



36
37
38
39
# File 'lib/ride_the_streetcar/query_selector.rb', line 36

def text_content=(code)
  statements << { text_content: { code: } }
  self # rubocop:disable Lint/Void
end

#toggle_attribute(name, force: nil) ⇒ Object



58
59
60
61
62
# File 'lib/ride_the_streetcar/query_selector.rb', line 58

def toggle_attribute(name, force: nil)
  name = normalize_name(name)
  statements << { toggle_attribute: { name:, force: } }
  self
end