Class: ProjectSimulator::Model
- Inherits:
-
Object
- Object
- ProjectSimulator::Model
- Includes:
- AppRoutes
- Defined in:
- lib/projectsimulator/model.rb
Instance Method Summary collapse
- #build(raw_requests, root: @root) ⇒ Object
- #get_thing(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_thing(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.
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 40 41 42 43 44 45 46 |
# File 'lib/projectsimulator/model.rb', line 11 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
48 49 50 51 52 53 |
# File 'lib/projectsimulator/model.rb', line 48 def build(raw_requests, root: @root) @ed = EasyDom.new(debug: false, root: root) raw_requests.lines.each {|line| request(line) } end |
#get_thing(h) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/projectsimulator/model.rb', line 56 def get_thing(h) h[:thing].gsub!(/ /,'_') if not h.has_key? :location then location = false h[:location] = find_path(h[:thing]) else location = true end puts 'h: ' + h.inspect if @debug a = [] a += h[:location].split(/ /) a << h[:thing] status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action]) if location then "The %s %s is %s." % [h[:location], h[:thing], status] else "%s is %s." % [h[:thing].capitalize, status] end end |
#op ⇒ Object
Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’
86 87 88 |
# File 'lib/projectsimulator/model.rb', line 86 def op() @ed end |
#query(s) ⇒ Object
90 91 92 |
# File 'lib/projectsimulator/model.rb', line 90 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’
97 98 99 100 101 102 103 104 105 |
# File 'lib/projectsimulator/model.rb', line 97 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end |
#set_thing(h) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/projectsimulator/model.rb', line 107 def set_thing(h) h[:thing].gsub!(/ /,'_') h[:location] = find_path(h[:thing]) unless h.has_key? :location a = [] a += h[:location].split(/ /) a << h[:thing] a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value]) end |
#to_sliml(level: 0) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/projectsimulator/model.rb', line 120 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
146 147 148 |
# File 'lib/projectsimulator/model.rb', line 146 def to_xml(=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end |