Class: LIFX::LAN::LightCollection
- Inherits:
-
Object
- Object
- LIFX::LAN::LightCollection
- Extended by:
- Forwardable
- Includes:
- Enumerable, LightTarget, RequiredKeywordArguments
- Defined in:
- lib/lifx/lan/light_collection.rb
Overview
LightCollection represents a collection of Lights, which can either refer to all lights on a NetworkContext, or lights
Defined Under Namespace
Classes: TagNotFound
Constant Summary collapse
- DEFAULT_ALIVE_THRESHOLD =
seconds
30
Constants included from LightTarget
LIFX::LAN::LightTarget::MSEC_PER_SEC, LIFX::LAN::LightTarget::NSEC_IN_SEC
Instance Attribute Summary collapse
-
#context ⇒ NetworkContext
readonly
Refers to NetworkContext the instance belongs to.
-
#tag ⇒ String
readonly
Tag of the collection.
Instance Method Summary collapse
-
#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of Lights considered alive.
-
#lights ⇒ Array<Light>
Returns an Array of Lights.
-
#stale(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of Lights considered stale.
-
#to_s ⇒ String
(also: #inspect)
Returns a nice string representation of itself.
-
#with_id(id) ⇒ Light
Returns a Light with device id matching
id
. -
#with_label(label) ⇒ Light
Returns a Light with its label matching
label
. -
#with_tag(tag) ⇒ LightCollection
Returns a LightCollection of Lights tagged with
tag
.
Methods included from RequiredKeywordArguments
Methods included from LightTarget
#half_sine, #pulse, #refresh, #saw, #set_color, #set_multizone_color, #set_power, #set_tile_colors, #sine, #tile_info, #triangle, #turn_off, #turn_on, #zone_count
Instance Attribute Details
#context ⇒ NetworkContext (readonly)
Refers to NetworkContext the instance belongs to
17 18 19 |
# File 'lib/lifx/lan/light_collection.rb', line 17 def context @context end |
#tag ⇒ String (readonly)
Tag of the collection. nil
represents all lights
21 22 23 |
# File 'lib/lifx/lan/light_collection.rb', line 21 def tag @tag end |
Instance Method Details
#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of LIFX::LAN::Lights considered alive
90 91 92 |
# File 'lib/lifx/lan/light_collection.rb', line 90 def alive(threshold: DEFAULT_ALIVE_THRESHOLD) lights.select { |l| l.seconds_since_seen <= threshold } end |
#lights ⇒ Array<Light>
Returns an Array of LIFX::LAN::Lights
78 79 80 81 82 83 84 |
# File 'lib/lifx/lan/light_collection.rb', line 78 def lights if tag context.all_lights.select { |l| l..include?(tag) } else context.all_lights end end |
#stale(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of LIFX::LAN::Lights considered stale
97 98 99 |
# File 'lib/lifx/lan/light_collection.rb', line 97 def stale(threshold: DEFAULT_ALIVE_THRESHOLD) lights.select { |l| l.seconds_since_seen > threshold } end |
#to_s ⇒ String Also known as: inspect
Returns a nice string representation of itself
103 104 105 |
# File 'lib/lifx/lan/light_collection.rb', line 103 def to_s %Q{#<#{self.class.name} lights=#{lights}#{tag ? " tag=#{tag}" : ''}>} end |
#with_id(id) ⇒ Light
Returns a LIFX::LAN::Light with device id matching id
50 51 52 |
# File 'lib/lifx/lan/light_collection.rb', line 50 def with_id(id) lights.find { |l| l.id == id} end |
#with_label(label) ⇒ Light
Returns a LIFX::LAN::Light with its label matching label
57 58 59 60 61 62 63 |
# File 'lib/lifx/lan/light_collection.rb', line 57 def with_label(label) if label.is_a?(Regexp) lights.find { |l| l.label(fetch: false) =~ label } else lights.find { |l| l.label(fetch: false) == label } end end |
#with_tag(tag) ⇒ LightCollection
Returns a LIFX::LAN::LightCollection of LIFX::LAN::Lights tagged with tag
68 69 70 71 72 73 74 |
# File 'lib/lifx/lan/light_collection.rb', line 68 def with_tag(tag) if context..include?(tag) self.class.new(context: context, tag: tag) else raise TagNotFound.new("No such tag '#{tag}'") end end |