Class: Interfacets::Test::Js::Receivers::React::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/test/js/receivers/react/node.rb,
lib/interfacets/test/js/receivers/react/node/xml_parser.rb

Defined Under Namespace

Classes: XmlParser

Constant Summary collapse

Error =
Class.new(StandardError)
NoMatchesError =
Class.new(Error)
MultipleMatchesError =
Class.new(Error)
StaleNodeError =
Class.new(Error) do
  def initialize
    super(
      <<~TXT
        This node is stale and cannot be used. This occurs when the facet \
        has been updated and you are using a reference to an out-of-date \
        node. Re-fetch the node using `page.dom` rather than storing a \
        reference to the node.
      TXT
    )
  end
end
EMPTY_ARG =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml:, dispatch:, parent:, validation_engine:) ⇒ Node

Returns a new instance of Node.



37
38
39
40
41
42
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 37

def initialize(xml:, dispatch:, parent:, validation_engine:)
  @xml = xml
  @dispatch = dispatch
  @parent = parent
  @validation_engine = validation_engine
end

Instance Attribute Details

#dispatch ⇒ Object (readonly)

Returns the value of attribute dispatch.



36
37
38
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 36

def dispatch
  @dispatch
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



36
37
38
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 36

def parent
  @parent
end

#validation_engine ⇒ Object (readonly)

Returns the value of attribute validation_engine.



36
37
38
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 36

def validation_engine
  @validation_engine
end

#xml ⇒ Object (readonly)

Returns the value of attribute xml.



36
37
38
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 36

def xml
  @xml
end

Class Method Details

.parse(json:, dispatch:, validation_engine:) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 11

def self.parse(json:, dispatch:, validation_engine:)
  new(
    xml: XmlParser.new(json, validation_engine:).call,
    dispatch:,
    validation_engine:,
    parent: nil,
  )
end

Instance Method Details

#all(*a, content: EMPTY_ARG, **p) ⇒ Object

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 88

def all(*a, content: EMPTY_ARG, **p)
  raise StaleNodeError if stale?

  xml
    .css(*a, **p)
    .map { Node.new(xml: _1, dispatch:, parent: self, validation_engine:) }
    .select {
      (
        content == EMPTY_ARG || (
          if content.is_a?(Regexp)
            _1.content.match?(content)
          else
            _1.content == content
          end
        )
      )
    }

end

#attribute(name) ⇒ Object

Raises:



108
109
110
111
112
113
114
115
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 108

def attribute(name)
  raise StaleNodeError if stale?

  xml
    .attribute(name)
    &.value
    &.then { JSON.parse(_1) rescue _1 }
end

#component_name ⇒ Object

Raises:



52
53
54
55
56
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 52

def component_name
  raise StaleNodeError if stale?

  xml.name
end

#content ⇒ Object

Raises:



66
67
68
69
70
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 66

def content
  raise StaleNodeError if stale?

  xml.content
end

#one ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 72

def one(...)
  raise StaleNodeError if stale?

  all(...)
    .tap {
      if _1.count == 0
        raise NoMatchesError
      elsif _1.count > 1
        raise MultipleMatchesError.new(_1.map(&:content).join(", "))
      end
    }
    .first
end

#props ⇒ Object

Raises:



58
59
60
61
62
63
64
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 58

def props
  raise StaleNodeError if stale?

  xml.attributes.transform_values do |attr|
    attr.value.then { JSON.parse(_1) rescue _1 }
  end
end

#stale! ⇒ Object



44
45
46
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 44

def stale!
  @stale = true
end

#stale? ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 48

def stale?
  @stale || parent&.stale?
end

#trigger(name, data = {}) ⇒ Object

Raises:



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/interfacets/test/js/receivers/react/node.rb', line 117

def trigger(name, data = {})
  raise StaleNodeError if stale?

  validation_engine&.validate_event(xml.name, name, data)

  value = attribute(name.to_s)
  raise "No event handler registered for: #{name}" unless value

  value["payload"]["event"] = data
  dispatch.(value)
end