Class: LIFX::LightCollection
- Inherits:
-
Object
- Object
- LIFX::LightCollection
- Extended by:
- Forwardable
- Includes:
- Enumerable, LightTarget, RequiredKeywordArguments
- Defined in:
- lib/lifx/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::LightTarget::MSEC_PER_SEC, LIFX::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
#refresh, #set_color, #set_power, #turn_off, #turn_on
Instance Attribute Details
#context ⇒ NetworkContext (readonly)
Refers to NetworkContext the instance belongs to
16 17 18 |
# File 'lib/lifx/light_collection.rb', line 16 def context @context end |
#tag ⇒ String (readonly)
Tag of the collection. nil
represents all lights
20 21 22 |
# File 'lib/lifx/light_collection.rb', line 20 def tag @tag end |
Instance Method Details
#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of LIFX::Lights considered alive
89 90 91 |
# File 'lib/lifx/light_collection.rb', line 89 def alive(threshold: DEFAULT_ALIVE_THRESHOLD) lights.select { |l| l.seconds_since_seen <= threshold } end |
#lights ⇒ Array<Light>
Returns an Array of LIFX::Lights
77 78 79 80 81 82 83 |
# File 'lib/lifx/light_collection.rb', line 77 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::Lights considered stale
96 97 98 |
# File 'lib/lifx/light_collection.rb', line 96 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
102 103 104 |
# File 'lib/lifx/light_collection.rb', line 102 def to_s %Q{#<#{self.class.name} lights=#{lights}#{tag ? " tag=#{tag}" : ''}>} end |
#with_id(id) ⇒ Light
Returns a LIFX::Light with device id matching id
49 50 51 |
# File 'lib/lifx/light_collection.rb', line 49 def with_id(id) lights.find { |l| l.id == id} end |
#with_label(label) ⇒ Light
Returns a LIFX::Light with its label matching label
56 57 58 59 60 61 62 |
# File 'lib/lifx/light_collection.rb', line 56 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::LightCollection of LIFX::Lights tagged with tag
67 68 69 70 71 72 73 |
# File 'lib/lifx/light_collection.rb', line 67 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 |