Class: Huey::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/huey/group.rb

Overview

A group is a collection of bulbs.

Constant Summary collapse

ATTRIBUTES =
Huey::Bulb::ATTRIBUTES - [:name, :reachable] + [:rgb]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*string_or_array) ⇒ Group

Returns a new instance of Group.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/huey/group.rb', line 28

def initialize(*string_or_array)
  string_or_array = string_or_array.first if string_or_array.first.is_a?(Array)

  self.bulbs = if string_or_array.first.is_a?(Bulb)
    string_or_array
  else
    string_or_array.collect {|s| Huey::Bulb.find_all(s)}.flatten.uniq
  end

  @attributes_to_write = {}
  Huey::Group.all << self
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/huey/group.rb', line 59

def method_missing(meth, *args, &block)
  if !self.bulbs.empty? && self.bulbs.first.respond_to?(meth)
    h = {}
    bulbs.each {|b| h[b.id] = b.send(meth, *args, &block)}
    h
  else
    super
  end
end

Instance Attribute Details

#bulbsObject

Returns the value of attribute bulbs.



8
9
10
# File 'lib/huey/group.rb', line 8

def bulbs
  @bulbs
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/huey/group.rb', line 8

def name
  @name
end

Class Method Details

.allObject



20
21
22
# File 'lib/huey/group.rb', line 20

def self.all
  @all ||= []
end

.find(name) ⇒ Object



24
25
26
# File 'lib/huey/group.rb', line 24

def self.find(name)
  Huey::Group.all.find {|g| g.name == name}
end

.import(file) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/huey/group.rb', line 10

def self.import(file)
  hash = YAML.load_file(file)

  hash.each do |key, value|
    g = Huey::Group.new(value)
    g.name = key
  end
  Huey::Group.all
end

Instance Method Details

#saveObject Also known as: commit



48
49
50
51
52
# File 'lib/huey/group.rb', line 48

def save
  response = bulbs.collect {|b| b.update(@attributes_to_write)}
  @attributes_to_write = {}
  response
end

#update(attrs) ⇒ Object



55
56
57
# File 'lib/huey/group.rb', line 55

def update(attrs)
  bulbs.collect {|b| b.update(attrs)}
end