Class: Huey::Group
- Inherits:
-
Object
- Object
- Huey::Group
- 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
-
#bulbs ⇒ Object
Returns the value of attribute bulbs.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*string_or_array) ⇒ Group
constructor
A new instance of Group.
- #method_missing(meth, *args, &block) ⇒ Object
- #save ⇒ Object (also: #commit)
- #update(attrs) ⇒ Object
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
#bulbs ⇒ Object
Returns the value of attribute bulbs.
8 9 10 |
# File 'lib/huey/group.rb', line 8 def bulbs @bulbs end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/huey/group.rb', line 8 def name @name end |
Class Method Details
.all ⇒ Object
20 21 22 |
# File 'lib/huey/group.rb', line 20 def self.all @all ||= [] end |
Instance Method Details
#save ⇒ Object 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 |