Skip to content

Instantly share code, notes, and snippets.

@jgorset
Created December 7, 2012 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgorset/4237588 to your computer and use it in GitHub Desktop.
Save jgorset/4237588 to your computer and use it in GitHub Desktop.
Rooms...
# Load all rooms.
#
# Returns an array of hashes describing each room.
def all
devices = Device.all
rooms = []
# Go through each device, check wich room it belongs
# do, if the room does not exist, add it to rooms, if
# just add this device to the list of devices
devices.each do |device|
if rooms.key? device.room
room = rooms[device.room]
else
room = rooms[device.room] = new device.room
end
room.devices << device
end
rooms
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment