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(level: 0) ⇒ 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 puts 's: ' + s.inspect if @debug if s[0] == '<' or s.lines[1][0..1] == ' ' then puts 'before easydom' if @debug s2 = if s.lines[1][0..1] == ' ' then lines = s.lines.map do |line| line.sub(/(\w+) +is +(\w+)$/) {|x| "#{$1} {switch: #{$2}}" } end lines.join else s end @ed = EasyDom.new(s2) else build(s, root: root) end end end |
Instance Method Details
#build(raw_requests, root: @root) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/projectsimulator.rb', line 52 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
59 60 61 62 63 64 65 66 |
# File 'lib/projectsimulator.rb', line 59 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/projectsimulator.rb', line 68 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’
87 88 89 |
# File 'lib/projectsimulator.rb', line 87 def op() @ed end |
#query(s) ⇒ Object
91 92 93 |
# File 'lib/projectsimulator.rb', line 91 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’
98 99 100 101 102 103 104 105 106 |
# File 'lib/projectsimulator.rb', line 98 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end |
#set_device(h) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/projectsimulator.rb', line 108 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
116 117 118 119 120 121 122 123 |
# File 'lib/projectsimulator.rb', line 116 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(level: 0) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/projectsimulator.rb', line 125 def to_sliml(level: 0) s = @ed.to_sliml return s if level.to_i > 0 lines = s.lines.map do |line| line.sub(/\{[^\}]+\}/) do |x| a = x.scan(/\w+: +[^ ]+/) if a.length == 1 and x[/switch:/] then val = x[/(?<=switch: ) *["']([^"']+)/,1] 'is ' + val else x end end end lines.join end |
#to_xml(options = nil) ⇒ Object Also known as: xml
151 152 153 |
# File 'lib/projectsimulator.rb', line 151 def to_xml(=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end |