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

#reboot!, #refresh, #set_color, #set_power, #turn_off, #turn_on

Instance Attribute Details

#contextNetworkContext (readonly)

Refers to NetworkContext the instance belongs to

Returns:



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

Returns:

  • (String)


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

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

    The maximum number of seconds a LIFX::Light was last seen to be considered alive

Returns:

  • (Array<Light>)

    Lights considered alive



88
89
90
# File 'lib/lifx/light_collection.rb', line 88

def alive(threshold: DEFAULT_ALIVE_THRESHOLD)
  lights.select { |l| l.seconds_since_seen <= threshold }
end

#lightsArray<Light>

Returns an Array of LIFX::Lights

Returns:



76
77
78
79
80
81
82
# File 'lib/lifx/light_collection.rb', line 76

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

Parameters:

  • threshold: (defaults to: DEFAULT_ALIVE_THRESHOLD)

    The minimum number of seconds since a LIFX::Light was last seen to be considered stale

Returns:

  • (Array<Light>)

    Lights considered stale



95
96
97
# File 'lib/lifx/light_collection.rb', line 95

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

Returns:

  • (String)


101
102
103
# File 'lib/lifx/light_collection.rb', line 101

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

Parameters:

  • id (String)

    Device ID

Returns:



48
49
50
# File 'lib/lifx/light_collection.rb', line 48

def with_id(id)
  lights.find { |l| l.id == id}
end

#with_label(label) ⇒ Light

Returns a LIFX::Light with its label matching label

Parameters:

  • label (String, Regexp)

    Label

Returns:



55
56
57
58
59
60
61
# File 'lib/lifx/light_collection.rb', line 55

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

Parameters:

  • tag (String)

    Tag

Returns:



66
67
68
69
70
71
72
# File 'lib/lifx/light_collection.rb', line 66

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