Class: LIFX::LightCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from RequiredKeywordArguments

#required!

Methods included from LightTarget

#half_sine, #pulse, #refresh, #saw, #set_color, #set_power, #sine, #triangle, #turn_off, #turn_on

Instance Attribute Details

#contextNetworkContext (readonly)

Refers to NetworkContext the instance belongs to


16
17
18
# File 'lib/lifx/light_collection.rb', line 16

def context
  @context
end

#tagString (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

#lightsArray<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.tags.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_sString 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.tags.include?(tag)
    self.class.new(context: context, tag: tag)
  else
    raise TagNotFound.new("No such tag '#{tag}'")
  end
end