Class: Macro
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#description ⇒ Object
Returns the value of attribute description.
-
#deviceid ⇒ Object
readonly
Returns the value of attribute deviceid.
-
#guid ⇒ Object
readonly
Returns the value of attribute guid.
-
#local_variables ⇒ Object
readonly
Returns the value of attribute local_variables.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#picture_path ⇒ Object
Returns the value of attribute picture_path.
-
#remote_url ⇒ Object
Returns the value of attribute remote_url.
-
#title ⇒ Object
Returns the value of attribute title.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
- #add(obj) ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #import_h(h) ⇒ Object
- #import_xml(node) ⇒ Object
-
#initialize(name = nil, geofences: nil, deviceid: nil, remote_url: nil, picture_path: nil, parent: nil, debug: false) ⇒ Macro
constructor
A new instance of Macro.
- #match?(triggerx, detail = {}, model = nil) ⇒ Boolean
-
#run ⇒ Object
invokes the actions.
-
#set_env ⇒ Object
prepares the environment in order for triggers to test fire successfully Used for testing.
- #set_var(label, v = '') ⇒ Object
- #to_h ⇒ Object
- #to_pc ⇒ Object
- #to_s(colour: false) ⇒ Object
- #to_summary(colour: false) ⇒ Object
Methods included from ObjectX
#action_to_object, #object_create, #varify
Constructor Details
#initialize(name = nil, geofences: nil, deviceid: nil, remote_url: nil, picture_path: nil, parent: nil, debug: false) ⇒ Macro
Returns a new instance of Macro.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby-macrodroid/macro.rb', line 32 def initialize(name=nil, geofences: nil, deviceid: nil, remote_url: nil, \ picture_path: nil, parent: nil, debug: false) @title, @geofences, @deviceid, @debug = name, geofences, deviceid, debug @remote_url, @picture_path, @parent = remote_url, picture_path, parent puts 'inside Macro#initialize' if @debug @local_variables, @triggers, @actions, @constraints = {}, [], [], [] @h = {} @guid = generate_guid() @enabled = true end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def actions @actions end |
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def constraints @constraints end |
#description ⇒ Object
Returns the value of attribute description.
30 31 32 |
# File 'lib/ruby-macrodroid/macro.rb', line 30 def description @description end |
#deviceid ⇒ Object (readonly)
Returns the value of attribute deviceid.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def deviceid @deviceid end |
#guid ⇒ Object (readonly)
Returns the value of attribute guid.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def guid @guid end |
#local_variables ⇒ Object (readonly)
Returns the value of attribute local_variables.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def local_variables @local_variables end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def parent @parent end |
#picture_path ⇒ Object
Returns the value of attribute picture_path.
30 31 32 |
# File 'lib/ruby-macrodroid/macro.rb', line 30 def picture_path @picture_path end |
#remote_url ⇒ Object
Returns the value of attribute remote_url.
30 31 32 |
# File 'lib/ruby-macrodroid/macro.rb', line 30 def remote_url @remote_url end |
#title ⇒ Object
Returns the value of attribute title.
30 31 32 |
# File 'lib/ruby-macrodroid/macro.rb', line 30 def title @title end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
28 29 30 |
# File 'lib/ruby-macrodroid/macro.rb', line 28 def triggers @triggers end |
Instance Method Details
#add(obj) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby-macrodroid/macro.rb', line 47 def add(obj) puts 'inside add; ' + obj.inspect if @debug puts '@actions: ' + @actions.inspect if @debug if obj.kind_of? Trigger then puts 'trigger found' if @debug @triggers << obj elsif obj.kind_of? Action puts 'action found' if @debug @actions << obj elsif obj.kind_of? Constraint puts 'constraint found' if @debug @constraints << obj end end |
#disable ⇒ Object
70 71 72 |
# File 'lib/ruby-macrodroid/macro.rb', line 70 def disable() @enabled = false end |
#enable ⇒ Object
74 75 76 |
# File 'lib/ruby-macrodroid/macro.rb', line 74 def enable() @enabled = true end |
#enabled? ⇒ Boolean
78 79 80 |
# File 'lib/ruby-macrodroid/macro.rb', line 78 def enabled?() @enabled end |
#import_h(h) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ruby-macrodroid/macro.rb', line 109 def import_h(h) if @debug then puts 'inside import_h' puts 'h:' + h.inspect end @guid = h[:guid] @category = h[:category] @title = h[:name] @description = h[:description] # fetch the local variables if h[:local_variables].any? and h[:local_variables].first.any? then @local_variables = h[:local_variables].map do |var| val = case var[:type] when 0 # boolean var[:boolean_value] when 1 # integer var[:int_value] when 2 # string var[:string_value] when 3 # decimal var[:decimal_Value] end [var[:name], val] end.to_h end # fetch the triggers @triggers = h[:trigger_list].map do |trigger| puts 'trigger: ' + trigger.inspect if @debug #exit object(trigger.to_snake_case) end @actions = h[:action_list].map do |action| object(action.to_snake_case) end puts 'before fetch constraints' if @debug # fetch the constraints @constraints = h[:constraint_list].map do |constraint| object(constraint.to_snake_case) end puts 'after fetch constraints' if @debug @h = h %i(local_variables m_trigger_list m_action_list m_constraint_list)\ .each {|x| @h[x] = [] } puts 'after @h set' if @debug @h end |
#import_xml(node) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/ruby-macrodroid/macro.rb', line 168 def import_xml(node) if @debug then puts 'inside Macro#import_xml' puts 'node: ' + node.xml.inspect end if node.element('triggers') then # level 2 @title = node.attributes[:name] @category = node.attributes[:category] @description = node.attributes[:description] # get all the triggers @triggers = node.xpath('triggers/*').map do |e| puts 'e.name: ' + e.name.inspect if @debug {timer: TimerTrigger}[e.name.to_sym].new(e.attributes.to_h) end # get all the actions @actions = node.xpath('actions/*').map do |e| if e.name == 'notification' then case e.attributes[:type].to_sym when :popup e.attributes.delete :type ToastAction.new e.attributes.to_h end end end # get all the constraints @constraints = node.xpath('constraints/*').map do |e| puts 'e.name: ' + e.name.inspect if @debug {airplanemode: AirplaneModeConstraint}[e.name.to_sym].new(e.attributes.to_h) end else # Level 1 puts 'import_xml: inside level 1' if @debug @title = node.text('macro') || node.attributes[:name] d = node.element('description') if d then desc = [] desc << d.text.strip if d.element('item/description') then desc << d.text('item/description').strip end @description = desc.join("\n") end node.xpath('variable').each {|e| set_var(*e.text.to_s.split(/: */,2)) } #@description = node.attributes[:description] tp = TriggersNlp.new(self) @triggers = node.xpath('trigger').flat_map do |e| r = tp.find_trigger e.text puts 'found trigger ' + r.inspect if @debug item = e.element('item') if item then if item.element('description') then item.xpath('description').map do |description| inner_lines = description.text.to_s.strip.lines puts 'inner_lines: ' + inner_lines.inspect if @debug trigger = if e.text.to_s.strip.empty? then inner_lines.shift.strip else e.text.strip end puts 'trigger: ' + trigger.inspect if @debug r = tp.find_trigger trigger puts 'r: ' + r.inspect if @debug #o = r[0].new([description, self]) if r o = object_create(r[0], [description, self, r[1]]) if r puts 'after o' if @debug o end else trigger = e.text.strip r = tp.find_trigger trigger a = e.xpath('item/*') h = if a.any? then a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h else {} end r = tp.find_trigger trigger #r[0].new(h) if r if r then object_create(r[0], h) else raise MacroError, 'App-routes: Trigger "' + trigger + '" not found' end end else trigger = e.text.strip r = tp.find_trigger trigger #r[0].new(r[1]) if r if r then object_create(r[0],r[1]) else raise MacroError, 'App-routes: Trigger "' + trigger + '" not found' end end end ap = ActionsNlp.new self node.xpath('action').each do |e| puts 'action e: ' + e.xml.inspect if @debug puts 'e.text ' + e.text if @debug item = e.element('item') if item then action_to_object(ap, e, item, self) else action = e.text.strip r = ap.find_action action #r[0].new(r[1]) if r self.add object_create(r[0],*r[1..-1]) if r end end cp = ConstraintsNlp.new @constraints = node.xpath('constraint').map do |e| r = cp.find_constraint e.text puts 'found constraint ' + r.inspect if @debug object_create(r[0], r[1]) if r end end self end |
#match?(triggerx, detail = {}, model = nil) ⇒ Boolean
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/ruby-macrodroid/macro.rb', line 359 def match?(triggerx, detail={}, model=nil ) if @triggers.any? {|x| x.type == triggerx and x.match?(detail, model) } then if @debug then puts 'checking constraints ...' puts '@constraints: ' + @constraints.inspect end if @constraints.all? {|x| x.match?($env.merge(detail), model) } then true else return false end end end |
#run ⇒ Object
invokes the actions
384 385 386 |
# File 'lib/ruby-macrodroid/macro.rb', line 384 def run() @actions.map(&:invoke) end |
#set_env ⇒ Object
prepares the environment in order for triggers to test fire successfully Used for testing
391 392 393 |
# File 'lib/ruby-macrodroid/macro.rb', line 391 def set_env() @triggers.each(&:set_env) end |
#set_var(label, v = '') ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/ruby-macrodroid/macro.rb', line 395 def set_var(label, v='') value = if v.to_f.to_s == v v.to_f elsif v.downcase == 'true' true elsif v.downcase == 'false' false elsif v.to_i.to_s == v v.to_i else v end if not @local_variables.has_key? label.to_sym then @local_variables.merge!({label.to_sym => value}) end if @debug then puts ("before varify; label: %s value: %s" % [label, value]).debug end varify(label, value) end |
#to_h ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ruby-macrodroid/macro.rb', line 82 def to_h() a = @local_variables.map do |k,v| varify(k,v).to_camelcase.map{|key,value| ['m_' + key, value]}.to_h end h = { local_variables: a, m_trigger_list: @triggers.map(&:to_h), m_action_list: @actions.map(&:to_h), m_category: @category, m_constraint_list: @constraints.map(&:to_h), m_description: '', m_name: title(), m_excludeLog: false, m_GUID: @guid, m_isOrCondition: false, m_enabled: @enabled, m_descriptionOpen: false, m_headingColor: 0 } puts 'h: ' + h.inspect if @debug @h.merge(h) end |
#to_pc ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/ruby-macrodroid/macro.rb', line 419 def to_pc() heading = '# ' + @title heading += '\n# ' + @description if @description condition = @triggers.first.to_pc actions = @actions.map(&:to_pc).join("\n") <<EOF #{heading} if #{condition} then #{actions} end EOF end |
#to_s(colour: false) ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/ruby-macrodroid/macro.rb', line 435 def to_s(colour: false) indent = 0 #@actions.map(&:to_s).join.lines.length > 0 ? 1 : 0 a = [] a << '# ' + @category + "\n" if @category a << (colour ? "m".bg_cyan.gray.bold : 'm') + ': ' + @title if @description and @description.length >= 1 then a << (colour ? "d".bg_gray.gray.bold : 'd') + ': ' \ + @description.gsub(/\n/,"\n ") end if @local_variables.length >= 1 then vars = @local_variables.map do |k,v| label = colour ? 'v'.bg_magenta : 'v' label += ': ' label + "%s: %s" % [k,v] end a << vars.join("\n") end puts 'before triggers' if @debug a << @triggers.map do |x| puts 'x: ' + x.inspect if @debug raise 'Macro#to_s trigger cannot be nil' if x.nil? s =-x.to_s(colour: colour) puts 's: ' + s.inspect if @debug s2 = if s.lines.length > 1 then "\n" + s.lines.map {|x| x.prepend (' ' * (indent+1)) }.join else ' ' + s end puts 's2: ' + s2.inspect if @debug #s.lines > 1 ? "\n" + x : x (colour ? "t".bg_red.gray.bold : 't') + ":" + s2 end.join("\n") puts 'before actions' if @debug actions = @actions.map do |x| puts 'x: ' + x.inspect if @debug raise 'Macro#to_s action cannot be nil' if x.nil? s = x.to_s(colour: colour) #puts 's: ' + s.inspect r = if indent <= 0 then lines = s.lines if lines.length > 1 then s = lines.map {|x| x.prepend (' ' * (indent+1)) }.join end s2 = s.lines.length > 1 ? "\n" + s : ' ' + s if colour then "a".bg_blue.gray.bold + ":" + s2 else "a:" + s2 end elsif indent > 0 if s =~ /^Else/ then (' ' * (indent-1)) + "%s" % s elsif s =~ /^End/ indent -= 1 (' ' * indent) + "%s" % s else s2 = s.lines[0] + s.lines[1..-1].map {|x| (' ' * indent) + x }.join (' ' * indent) + "%s" % s2 end end if s =~ /^(?:If|DO \/ WHILE)/i then if indent < 1 then r = if colour then "a".bg_blue.gray.bold + ":\n %s" % s else "a:\n %s" % s end indent += 1 else r = (' ' * indent) + "%s" % s end indent += 1 end r end.join("\n") a << actions puts 'before constraints' if @debug if @constraints.any? then a << @constraints.map do |x| (colour ? "c".bg_green.gray.bold : 'c') + ": %s" % x end.join("\n") end a.join("\n") + "\n" end |
#to_summary(colour: false) ⇒ Object
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/ruby-macrodroid/macro.rb', line 556 def to_summary(colour: false) if colour then a = [ 'm'.bg_cyan.gray.bold + ': ' + @title, 't'.bg_red.gray.bold + ': ' + @triggers.map \ {|x| x.to_summary(colour: false)}.join(", "), 'a'.bg_blue.gray.bold + ': ' + @actions.map \ {|x| x.to_summary(colour: false)}.join(", ") ] if @constraints.any? then a << 'c'.bg_green.gray.bold + ': ' + @constraints.map \ {|x| x.to_summary(colour: false)}.join(", ") end else a = [ 'm: ' + @title, 't: ' + @triggers.map {|x| x.to_summary(colour: false)}.join(", "), 'a: ' + @actions.map {|x| x.to_summary(colour: false)}.join(", ") ] if @constraints.any? then a << 'c: ' + @constraints.map \ {|x| x.to_summary(colour: false)}.join(", ") end end a.join("\n") + "\n" end |