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.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/tracery.rb', line 332

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.



331
332
333
# File 'lib/tracery.rb', line 331

def node
  @node
end

#ruleNodeObject

Returns the value of attribute ruleNode.



331
332
333
# File 'lib/tracery.rb', line 331

def ruleNode
  @ruleNode
end

#targetObject

Returns the value of attribute target.



331
332
333
# File 'lib/tracery.rb', line 331

def target
  @target
end

#typeObject

Returns the value of attribute type.



331
332
333
# File 'lib/tracery.rb', line 331

def type
  @type
end

Instance Method Details

#activateObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/tracery.rb', line 354

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



379
380
381
382
383
384
385
# File 'lib/tracery.rb', line 379

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



387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/tracery.rb', line 387

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