Class: TracerySymbol

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar, key, rawRules) ⇒ TracerySymbol

Returns a new instance of TracerySymbol.


467
468
469
470
471
472
473
474
475
# File 'lib/tracery.rb', line 467

def initialize(grammar, key, rawRules)
    # Symbols can be made with a single value, and array, or array of objects of (conditions/values)
    @key = key
    @grammar = grammar
    @rawRules = rawRules
    
    @baseRules = RuleSet.new(@grammar, @rawRules)
    clearState
end

Instance Attribute Details

#isDynamicObject

Returns the value of attribute isDynamic


465
466
467
# File 'lib/tracery.rb', line 465

def isDynamic
  @isDynamic
end

Instance Method Details

#clearStateObject


477
478
479
480
481
482
# File 'lib/tracery.rb', line 477

def clearState
    # Clear the stack and clear all ruleset usages
    @stack = [@baseRules]
    @uses = []
    @baseRules.clearState
end

#getActiveRulesObject


502
503
504
505
# File 'lib/tracery.rb', line 502

def getActiveRules
    return nil if @stack.empty?
    return @stack.last.selectRule 
end

#popRulesObject


489
490
491
# File 'lib/tracery.rb', line 489

def popRules
    @stack.pop
end

#pushRules(rawRules) ⇒ Object


484
485
486
487
# File 'lib/tracery.rb', line 484

def pushRules(rawRules)
    rules = RuleSet.new(@grammar, rawRules)
    @stack.push rules
end

#rulesToJSONObject


507
508
509
# File 'lib/tracery.rb', line 507

def rulesToJSON
    return @rawRules.to_json
end

#selectRule(node, errors) ⇒ Object


493
494
495
496
497
498
499
500
# File 'lib/tracery.rb', line 493

def selectRule(node, errors)
    @uses.push({ node: node })
    if(@stack.empty?) then
        errors << "The rule stack for '#{@key}' is empty, too many pops?"
        return "((#{@key}))"
    end
    return @stack.last.selectRule
end