Class: NodeAction

Inherits:
Object
  • Object
show all
Defined in:
lib/tracery.rb

Overview

Types of actions: 0 Push: [key:rule] 1 Pop: [key:POP] 2 function: [functionName(param0,param1)] (TODO!)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, raw) ⇒ NodeAction

Returns a new instance of NodeAction.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/tracery.rb', line 347

def initialize(node, raw)
    # puts("No node for NodeAction") if(node.nil?)
    # puts("No raw commands for NodeAction") if(raw.empty?)
    
    @node = node
    
    sections = raw.split(":")
    @target = sections.first
    if(sections.size == 1) then
        # No colon? A function!
        @type = 2
    else
        # Colon? It's either a push or a pop
        @rule = sections[1] || ""
        if(@rule == "POP")
            @type = 1;
        else
            @type = 0;
        end
    end
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



346
347
348
# File 'lib/tracery.rb', line 346

def node
  @node
end

#ruleNodeObject

Returns the value of attribute ruleNode.



346
347
348
# File 'lib/tracery.rb', line 346

def ruleNode
  @ruleNode
end

#targetObject

Returns the value of attribute target.



346
347
348
# File 'lib/tracery.rb', line 346

def target
  @target
end

#typeObject

Returns the value of attribute type.



346
347
348
# File 'lib/tracery.rb', line 346

def type
  @type
end

Instance Method Details

#activateObject



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/tracery.rb', line 369

def activate
    grammar = @node.grammar
    case(@type)
        when 0 then
            # split into sections (the way to denote an array of rules)
            ruleSections = @rule.split(",")
            finishedRules = ruleSections.map{|ruleSection|
                n = TraceryNode.new(grammar, 0, {
                        type: -1,
                        raw: ruleSection
                    })
                n.expand()
                n.finishedText
            }
            
            # TODO: escape commas properly
            grammar.pushRules(@target, finishedRules, self)
            # puts("Push rules: #{@target} #{@ruleText}")
        when 1 then
            grammar.popRules(@target);
        when 2 then
            grammar.flatten(@target, true);
    end
end

#createUndoObject



394
395
396
397
398
399
400
# File 'lib/tracery.rb', line 394

def createUndo
    if(@type == 0) then
        return NodeAction.new(@node, "#{@target}:POP")
    end
    # TODO Not sure how to make Undo actions for functions or POPs
    return nil
end

#toTextObject



402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/tracery.rb', line 402

def toText
    case(@type)
        when 0 then
            return "#{@target}:#{@rule}"
        when 1 then
            return "#{@target}:POP"
        when 2 then
            return "((some function))"
        else
            return "((Unknown Action))"
    end
end