Class: MacroDroid
- Inherits:
-
Object
- Object
- MacroDroid
- Defined in:
- lib/ruby-macrodroid.rb
Instance Attribute Summary collapse
-
#macros ⇒ Object
readonly
Returns the value of attribute macros.
Instance Method Summary collapse
- #add(macro) ⇒ Object
- #build_h ⇒ Object
- #export_json ⇒ Object (also: #to_json)
- #import_json(s) ⇒ Object
- #import_xml(raws) ⇒ Object
-
#initialize(obj = nil, debug: false) ⇒ MacroDroid
constructor
A new instance of MacroDroid.
- #pc_to_xml(s) ⇒ Object
- #text_to_xml(s) ⇒ Object
- #to_h ⇒ Object
-
#to_pc ⇒ Object
returns pseudocode.
- #to_s ⇒ Object
Constructor Details
#initialize(obj = nil, debug: false) ⇒ MacroDroid
Returns a new instance of MacroDroid.
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 |
# File 'lib/ruby-macrodroid.rb', line 464 def initialize(obj=nil, debug: false) @debug = debug if obj then raw_s, _ = RXFHelper.read(obj) s = raw_s.strip if s[0] == '{' then import_json(s) elsif s[0] == '<' import_xml(s) @h = build_h else xml = if s =~ /^m: / then text_to_xml(s) elsif s =~ /^# / pc_to_xml(s) else raise MacroDroidError, 'invalid input' end import_xml(xml) @h = build_h end else @h = build_h() @macros = [] end end |
Instance Attribute Details
#macros ⇒ Object (readonly)
Returns the value of attribute macros.
462 463 464 |
# File 'lib/ruby-macrodroid.rb', line 462 def macros @macros end |
Instance Method Details
#add(macro) ⇒ Object
508 509 510 |
# File 'lib/ruby-macrodroid.rb', line 508 def add(macro) @macros << macro end |
#build_h ⇒ Object
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 |
# File 'lib/ruby-macrodroid.rb', line 512 def build_h() puts 'inside Macro#build_h' if @debug { cell_tower_groups: [], cell_towers_ignore: [], drawer_configuration: { drawer_items: [], background_color: -1, header_color: 12692882, left_side: false, swipe_area_color: -7829368, swipe_area_height: 20, swipe_area_offset: 40, swipe_area_opacity: 80, swipe_area_width: 14, visible_swipe_area_width: 0 }, variables: [], user_icons: [], geofence_data: { geofence_map: {} }, macro_list: [] } end |
#export_json ⇒ Object Also known as: to_json
540 541 542 543 544 |
# File 'lib/ruby-macrodroid.rb', line 540 def export_json() to_h.to_json end |
#import_json(s) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/ruby-macrodroid.rb', line 548 def import_json(s) h = JSON.parse(s, symbolize_names: true) puts 'json_to_yaml: ' + h.to_yaml if @debug @h = h.to_snake_case puts ('@h: ' + @h.inspect).debug if @debug @macros = @h[:macro_list].map do |macro| puts ('macro: ' + macro.inspect).debug if @debug m = Macro.new(debug: @debug) m.import_h(macro) m end @h[:macro_list] = [] end |
#import_xml(raws) ⇒ Object
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/ruby-macrodroid.rb', line 569 def import_xml(raws) puts 'raws: ' + raws.inspect if @debug s = RXFHelper.read(raws).first puts 's: ' + s.inspect if @debug doc = Rexle.new(s) if @debug then puts 'doc: ' + doc.root.xml end debug = @debug @macros = doc.root.xpath('macro').map do |node| macro = Macro.new @title, debug: debug macro.import_xml(node) macro end end |
#pc_to_xml(s) ⇒ Object
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/ruby-macrodroid.rb', line 591 def pc_to_xml(s) macros = s.strip.split(/(?=#)/).map do |raw_macro| a = raw_macro.lines name = a.shift[/(?<=# ).*/] description = a.shift[/(?<=# ).*/] if a[0][/^# /] body = a.join.strip a2 = body.lines # get the trigger trigger = [:trigger, {}, a2[0][/^if (.*) then/,1]] action = [:action, {}, a2[1].strip] [:macro, {name: name, description: description}, trigger, action, []] end doc = Rexle.new([:macros, {}, '', *macros]) doc.root.xml pretty: true end |
#text_to_xml(s) ⇒ Object
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/ruby-macrodroid.rb', line 613 def text_to_xml(s) a = s.split(/.*(?=^m:)/); a.shift a.map!(&:chomp) macros = a.map do |x| lines = x.lines puts 'lines: ' + lines.inspect if @debug name = lines.shift[/^m: +(.*)/,1] h = {t: [], a: [], c: []} lines.each {|line| h[line[0].to_sym] << line[/^\w: +(.*)/,1] } triggers = h[:t].map {|text| [:trigger, {}, text]} actions = h[:a].map {|text| [:action, {}, text]} constraints = h[:c].map {|text| [:constraint, {}, text]} [:macro, {name: name},'', *triggers, *actions, *constraints] end doc = Rexle.new([:macros, {}, '', *macros]) doc.root.xml pretty: true end |
#to_h ⇒ Object
640 641 642 643 644 |
# File 'lib/ruby-macrodroid.rb', line 640 def to_h() @h.merge(macro_list: @macros.map(&:to_h)).to_camel_case end |
#to_pc ⇒ Object
returns pseudocode
648 649 650 |
# File 'lib/ruby-macrodroid.rb', line 648 def to_pc() @macros.map(&:to_pc).join("\n\n") end |
#to_s ⇒ Object
652 653 654 |
# File 'lib/ruby-macrodroid.rb', line 652 def to_s() @macros.map(&:to_s).join("\n") end |