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.
173 174 175 |
# File 'lib/openhab/core/events/source.rb', line 173 def actor components.first&.actor end |
#bundle ⇒ String? (readonly)
The bundle (module, app, etc.) of the initial component that sent the event.
163 164 165 |
# File 'lib/openhab/core/events/source.rb', line 163 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.
153 154 155 |
# File 'lib/openhab/core/events/source.rb', line 153 def source @source ||= components.join("=>").freeze end |
Instance Method Details
#!=(other) ⇒ true, false
191 192 193 |
# File 'lib/openhab/core/events/source.rb', line 191 def !=(other) !(self == other) # rubocop:disable Style/InverseMethods end |
#<=>(other) ⇒ Integer?
196 197 198 199 200 201 |
# File 'lib/openhab/core/events/source.rb', line 196 def <=>(other) return 0 if self == other return nil unless other.respond_to?(:to_str) to_s <=> other.to_str end |
#==(other) ⇒ true, false
183 184 185 186 187 188 |
# File 'lib/openhab/core/events/source.rb', line 183 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.
119 120 121 |
# File 'lib/openhab/core/events/source.rb', line 119 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
203 204 205 |
# File 'lib/openhab/core/events/source.rb', line 203 def eql?(other) other.is_a?(Source) && components == other.components end |
#hash ⇒ Integer
208 209 210 |
# File 'lib/openhab/core/events/source.rb', line 208 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.
137 138 139 140 141 142 143 144 145 |
# File 'lib/openhab/core/events/source.rb', line 137 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 113 |
# 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 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 # rubocop:enable Style/CaseEquality end |