Class: OpenHAB::Core::Events::Source
- Inherits:
-
Delegator
- Object
- Delegator
- OpenHAB::Core::Events::Source
- Defined in:
- lib/openhab/core/events/source.rb
Overview
Represents the source of an event as a chain of delegated Components.
This class behaves like a String representing the event source, but also provides methods to access the individual components in the delegation chain.
Starting from openHAB 5.1, the source can contain multiple components that contain information about the delegation chain of the event.
Each component contains:
Defined Under Namespace
Classes: Component
Instance Attribute Summary collapse
-
#actor ⇒ String?
readonly
The actor (user, device, etc.) of the initial component that sent the event.
-
#bundle ⇒ String?
readonly
The bundle (module, app, etc.) of the initial component that sent the event.
-
#components ⇒ Array<Component>
readonly
The components in the event source delegation chain.
-
#source ⇒ String
(also: #to_s, #to_str, #inspect, #__getobj__)
readonly
The event source as a string.
Instance Method Summary collapse
- #!=(other) ⇒ true, false
- #<=>(other) ⇒ Integer?
- #==(other) ⇒ true, false
-
#actor_for(bundle) ⇒ String?
The actor (user, device, etc.) for the specified bundle, if any.
-
#delegate(bundle, actor = nil) ⇒ Source
Construct a new Source by adding an additional component to the delegation chain.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Integer
-
#initialize(source_or_components) ⇒ Source
constructor
Construct a new Source object.
-
#reject(bundle = nil) ⇒ Object
Construct a new Source without any components from the specified bundle.
-
#sender?(bundle_or_actor = nil, bundle: nil, actor: nil) ⇒ Object
Check if the event was sent by the specified bundle or actor.
Constructor Details
#initialize(source) ⇒ Source #initialize(components) ⇒ Source
Construct a new OpenHAB::Core::Events::Source object.
63 64 65 66 67 68 69 70 71 |
# File 'lib/openhab/core/events/source.rb', line 63 def initialize(source_or_components) # rubocop:disable Lint/MissingSuper @components = if source_or_components.is_a?(Array) @source = nil source_or_components.dup else @source = -source_or_components @source.split("=>").map { |s| Component.parse(s) } end.freeze end |
Instance Attribute Details
#actor ⇒ String? (readonly)
The actor (user, device, etc.) of the initial component that sent the event.
172 173 174 |
# File 'lib/openhab/core/events/source.rb', line 172 def actor components.first&.actor end |
#bundle ⇒ String? (readonly)
The bundle (module, app, etc.) of the initial component that sent the event.
162 163 164 |
# File 'lib/openhab/core/events/source.rb', line 162 def bundle components.first&.bundle end |
#components ⇒ Array<Component> (readonly)
The components in the event source delegation chain.
52 53 54 |
# File 'lib/openhab/core/events/source.rb', line 52 def components @components end |
#source ⇒ String (readonly) Also known as: to_s, to_str, inspect, __getobj__
The event source as a string.
152 153 154 |
# File 'lib/openhab/core/events/source.rb', line 152 def source @source ||= components.join("=>").freeze end |
Instance Method Details
#!=(other) ⇒ true, false
190 191 192 |
# File 'lib/openhab/core/events/source.rb', line 190 def !=(other) !(self == other) # rubocop:disable Style/InverseMethods end |
#<=>(other) ⇒ Integer?
195 196 197 198 199 200 |
# File 'lib/openhab/core/events/source.rb', line 195 def <=>(other) return 0 if self == other return nil unless other.respond_to?(:to_str) to_s <=> other.to_str end |
#==(other) ⇒ true, false
182 183 184 185 186 187 |
# File 'lib/openhab/core/events/source.rb', line 182 def ==(other) return components == other.components if other.is_a?(Source) return to_s == other.to_str if other.respond_to?(:to_str) false end |
#actor_for(bundle) ⇒ String?
Returns the actor (user, device, etc.) for the specified bundle, if any.
118 119 120 |
# File 'lib/openhab/core/events/source.rb', line 118 def actor_for(bundle) components.find { |c| bundle === c.bundle }&.actor # rubocop:disable Style/CaseEquality end |
#delegate(bundle, actor = nil) ⇒ Source
Construct a new OpenHAB::Core::Events::Source by adding an additional component to the delegation chain.
80 81 82 |
# File 'lib/openhab/core/events/source.rb', line 80 def delegate(bundle, actor = nil) Source.new(components + [Component.build(bundle, actor)]) end |
#eql?(other) ⇒ Boolean
202 203 204 |
# File 'lib/openhab/core/events/source.rb', line 202 def eql?(other) other.is_a?(Source) && components == other.components end |
#hash ⇒ Integer
207 208 209 |
# File 'lib/openhab/core/events/source.rb', line 207 def hash components.hash end |
#reject(bundle) ⇒ Source #reject({ |component| ... }) {|component| ... } ⇒ Source
Construct a new OpenHAB::Core::Events::Source without any components from the specified bundle.
This is useful if you consider events from a specific bundle as sensitive, and want to filter that component out from an untrusted sender.
136 137 138 139 140 141 142 143 144 |
# File 'lib/openhab/core/events/source.rb', line 136 def reject(bundle = nil, &) raise ArgumentError, "Either a bundle or a block must be given" unless bundle || block_given? Source.new(if block_given? components.reject(&) else components.reject { |c| bundle === c.bundle } # rubocop:disable Style/CaseEquality end) end |
#sender?(bundle_or_actor) ⇒ true, false #sender?(bundle: nil, actor: nil) ⇒ true, false
Check if the event was sent by the specified bundle or actor.
Checks if any component matches the specified bundle or actor.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openhab/core/events/source.rb', line 100 def sender?(bundle_or_actor = nil, bundle: nil, actor: nil) unless bundle_or_actor || bundle || actor raise ArgumentError, "Specify one of bundle_or_actor, bundle or actor" end # rubocop:disable-next Style/CaseEquality components.any? do |sender| (bundle && bundle === sender.bundle) || (actor && actor === sender.actor) || (bundle_or_actor && bundle_or_actor === sender.bundle) || (bundle_or_actor && bundle_or_actor === sender.actor) end end |