Class: Huey::Group

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



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

def initialize(*string_or_array)
  @bulbs = []
  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 unless self.bulbs.nil? || self.bulbs.empty?
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/huey/group.rb', line 65

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

Instance Attribute Details

#bulbsObject

Returns the value of attribute bulbs.



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

def bulbs
  @bulbs
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.allObject



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

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

.find(name) ⇒ Object



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

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

.import(file) ⇒ Object



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

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

#each(&block) ⇒ Object



61
62
63
# File 'lib/huey/group.rb', line 61

def each(&block)
  bulbs.each {|b| block.call(b)}
end

#saveObject Also known as: commit



50
51
52
53
54
# File 'lib/huey/group.rb', line 50

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

#update(attrs) ⇒ Object



57
58
59
# File 'lib/huey/group.rb', line 57

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