Class: ProjectSimulator::Model
- Inherits:
-
Object
- Object
- ProjectSimulator::Model
- Includes:
- AppRoutes
- Defined in:
- lib/projectsimulator.rb
Instance Method Summary collapse
- #build(raw_requests, root: @root) ⇒ Object
- #get_device(h) ⇒ Object
- #get_service(h) ⇒ Object
-
#initialize(obj = nil, root: 'building1', debug: false) ⇒ Model
constructor
A new instance of Model.
-
#op ⇒ Object
Object Property (op) Helpful for accessing properites in dot notation e.g.
- #query(s) ⇒ Object
-
#request(s) ⇒ Object
request accepts a string in plain english e.g.
- #set_device(h) ⇒ Object
- #set_service(h) ⇒ Object
- #to_sliml ⇒ Object
- #to_xml(options = nil) ⇒ Object (also: #xml)
Constructor Details
#initialize(obj = nil, root: 'building1', debug: false) ⇒ Model
Returns a new instance of Model.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/projectsimulator.rb', line 15 def initialize(obj=nil, root: 'building1', debug: false) super() @root, @debug = root, debug @location = nil if obj then s = obj.strip if s[0] == '<' or s.lines[1][0..1] == ' ' then @ed = EasyDom.new(s) else build(obj, root: root) end end end |
Instance Method Details
#build(raw_requests, root: @root) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/projectsimulator.rb', line 34 def build(raw_requests, root: @root) @ed = EasyDom.new(debug: false, root: root) raw_requests.lines.each {|line| request(line) } end |
#get_device(h) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/projectsimulator.rb', line 41 def get_device(h) a = h[:location].split(/ /) a << h[:device] status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action]) "The %s %s is %s." % [h[:location], h[:device], status] end |
#get_service(h) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/projectsimulator.rb', line 50 def get_service(h) a = [] a << h[:location].split(/ /) if h.has_key? :location a << h[:service] status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action]) if h.has_key? :location then "The %s %s is %s." % [h[:location], h[:service], status] else "%s is %s." % [h[:service].capitalize, status] end end |
#op ⇒ Object
Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’
69 70 71 |
# File 'lib/projectsimulator.rb', line 69 def op() @ed end |
#query(s) ⇒ Object
73 74 75 |
# File 'lib/projectsimulator.rb', line 73 def query(s) @ed.e.element(s) end |
#request(s) ⇒ Object
request accepts a string in plain english e.g. request ‘switch the livingroom light on’
80 81 82 83 84 85 86 87 88 |
# File 'lib/projectsimulator.rb', line 80 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end |
#set_device(h) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/projectsimulator.rb', line 90 def set_device(h) a = h[:location].split(/ /) a << h[:device] a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value]) end |
#set_service(h) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/projectsimulator.rb', line 98 def set_service(h) a = [] a += h[:location].split(/ /) if h[:location] a << h[:service] a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value]) end |
#to_sliml ⇒ Object
107 108 109 |
# File 'lib/projectsimulator.rb', line 107 def to_sliml() @ed.to_sliml end |
#to_xml(options = nil) ⇒ Object Also known as: xml
111 112 113 |
# File 'lib/projectsimulator.rb', line 111 def to_xml(=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end |