Class: IfConditionAction

Inherits:
Action show all
Defined in:
lib/ruby-macrodroid/actions.rb

Overview

Conditions/Loops

Instance Attribute Summary

Attributes inherited from Action

#constraints

Attributes inherited from MacroObject

#options, #siguid, #type

Instance Method Summary collapse

Methods inherited from Action

#invoke

Methods included from ObjectX

#action_to_object, #object_create, #varify

Methods inherited from MacroObject

#to_h

Constructor Details

#initialize(obj = nil) ⇒ IfConditionAction

Returns a new instance of IfConditionAction.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
# File 'lib/ruby-macrodroid/actions.rb', line 406

def initialize(obj=nil)

  options = {
    a: true,
    constraint_list: []
  }  
  puts 'obj: ' + obj.inspect if $debug
  
  if obj.is_a? Hash then
    
    h = obj
    macro = h[:macro]
    h2 = options.merge(filter(options,h).merge(macro: macro))
    super(h2)      
    
  elsif obj.is_a? Array
    
    e, macro = obj
    super()
    puts 'e.xml: ' + e.xml if $debug
    puts 'e.text: ' + e.text.to_s.strip if $debug
    raw_txt = e.text.to_s.strip[/^if [^$]+/i] || e.text('item/description')
    puts 'raw_txt: ' + raw_txt.inspect if $debug
    
    clause = raw_txt[/^If (.*)/i,1]
    puts 'clause: ' + clause.inspect if $debug
    conditions = clause.split(/\s+\b(?:AND|OR)\b\s+/i)
    puts 'conditions: ' + conditions.inspect if $debug
    
    cp = ConstraintsNlp.new      
    
    @constraints = conditions.map do |c|
      puts 'c: ' + c.inspect  if $debug
      r = cp.find_constraint c
      puts 'found constraint ' + r.inspect if $debug
      
      r[0].new(r[1]) if r
      
    end         
    puts '@constraints: ' + @constraints.inspect if $debug
    
    # find any nested actions
    item = e.element('item')
    
    if item then
      
      ap = ActionsNlp.new
      obj2 = action_to_object(ap, item, item, macro)      
      puts 'obj2: ' + obj2.inspect if $debug
      #macro.add obj2
      
    end
    
    h = {
      constraint_list: @constraints.map(&:to_h)
    }
    super(h)        {}
    
  else
    # get the constraints

  end
  

  

  
  @label = 'If '

end

Instance Method Details

#to_s(colour: false, indent: 0) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/ruby-macrodroid/actions.rb', line 477

def to_s(colour: false, indent: 0)
  
  h = @h.clone    
  #h.delete :macro
  @s = 'If '
  operator = @h[:is_or_condition] ? 'OR' : 'AND'
  constraints = @constraints.map \
      {|x| '  ' * indent + x.to_summary(colour: colour)}.join(" %s " % operator)    
  
  out = []
  out << "; %s" % @h[:comment] if @h[:comment]
  s = @s.lines.map {|x| ('  ' * indent) + x}.join
  out << s + constraints
  out.join("\n")
  
end