public
Description: A Sinatra application that turns LEDs on and off on an embedded system
Homepage:
Clone URL: git://github.com/dougbradbury/weblink.git
weblink / lib / mock_led.rb
100644 39 lines (31 sloc) 0.547 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'yaml'
 
class MockLed
  def initialize(id=1)
    @id = id
  end
  
  def on
    @on = true
    save
  end
  
  def off
    @on = false
    save
  end
  
  def on?
    @on
  end
  
  def save
    File.open(CONFIG[:data_dir] + "/led#{@id}.yaml", "w") do |file|
      file.write YAML::dump(self)
    end
  end
  
end
 
class Led
  def self.find(id)
    if File.exists?(CONFIG[:data_dir] + "/led#{id}.yaml")
      return YAML::load(File.open(CONFIG[:data_dir] + "/led#{id}.yaml", "r"))
    else
      return MockLed.new(id)
    end
  end
end