Class: Saxxy::ContextTree
- Inherits:
-
Object
- Object
- Saxxy::ContextTree
- Defined in:
- lib/saxxy/context_tree.rb
Overview
ContextTree describes the tree of contexts that the user eventually constructs by constraining NodeActions to be active under some Context.
Instance Attribute Summary collapse
-
#root ⇒ Context
The root context of the tree.
Instance Method Summary collapse
-
#initialize(ctx = nil, &block) ⇒ ContextTree
constructor
Initializes a ContextTree by passing an optional context to be used by the actions in order to execute their action block and a block that will be evaluated in order to create the intenal tree structure.
-
#on(regexp_or_string, attributes = {}, &block) ⇒ NodeAction
Creates a NodeAction and uses the arguments to create its activation rule and registers it under the current root context.
-
#under(regexp_or_string, attributes = {}, &block) ⇒ Context
Creates a Context and uses the arguments to create its activation rule.
Constructor Details
#initialize(ctx = nil, &block) ⇒ ContextTree
Initializes a ContextTree by passing an optional context to be used by the actions in order to execute their action block and a block that will be evaluated in order to create the intenal tree structure.
33 34 35 36 37 |
# File 'lib/saxxy/context_tree.rb', line 33 def initialize(ctx = nil, &block) self.root = Saxxy::Context.new @ctx = ctx || eval("self", block.binding) eval_subtree!(&block) end |
Instance Attribute Details
#root ⇒ Context
Returns the root context of the tree.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/saxxy/context_tree.rb', line 20 class ContextTree attr_accessor :root # Initializes a ContextTree by passing an optional context to be used # by the actions in order to execute their action block and a block that # will be evaluated in order to create the intenal tree structure. # # @param ctx [Object] an object to be used by the NodeActions # in order to evaluate their block # # @param block [Proc] a block that will get evaluated and create # the context tree structure # def initialize(ctx = nil, &block) self.root = Saxxy::Context.new @ctx = ctx || eval("self", block.binding) eval_subtree!(&block) end # Creates a Context and uses the arguments to create its activation rule. After # creating the context it registers it under the current root context and returns it. # # @param regexp_or_string [String|Regexp] the activation rule's name # @param attributes [Hash] the activation rule's attributes # @param block [Proc] a block that will get evaluated and register # the child contexts and the actions # # @return [Context] the registered Context def under(regexp_or_string, attributes = {}, &block) Saxxy::Context.new(Saxxy::NodeRule.new(regexp_or_string, attributes)).tap do |context| __register_context(context, &block) end end # Creates a NodeAction and uses the arguments to create its activation rule and registers # it under the current root context. # # @param regexp_or_string [String|Regexp] the activation rule's name # @param attributes [Hash] the activation rule's attributes # @param block [Proc] the NodeAction's action block that will get # evaluated on the passed context at construction # # @return [NodeAction] the registered NodeAction def on(regexp_or_string, attributes = {}, &block) Saxxy::NodeAction.new(Saxxy::NodeRule.new(regexp_or_string, attributes), @ctx, &block).tap do |action| __register_action(action) end end private def eval_subtree!(&block) instance_eval(&block) if block_given? self.root = root.parent_context if root.has_parent? end def __register_action(action) root.register(action) end def __register_context(context, &block) root.register(self.root = context) eval_subtree!(&block) end end |
Instance Method Details
#on(regexp_or_string, attributes = {}, &block) ⇒ NodeAction
Creates a NodeAction and uses the arguments to create its activation rule and registers it under the current root context.
63 64 65 66 67 |
# File 'lib/saxxy/context_tree.rb', line 63 def on(regexp_or_string, attributes = {}, &block) Saxxy::NodeAction.new(Saxxy::NodeRule.new(regexp_or_string, attributes), @ctx, &block).tap do |action| __register_action(action) end end |
#under(regexp_or_string, attributes = {}, &block) ⇒ Context
Creates a Context and uses the arguments to create its activation rule. After creating the context it registers it under the current root context and returns it.
48 49 50 51 52 |
# File 'lib/saxxy/context_tree.rb', line 48 def under(regexp_or_string, attributes = {}, &block) Saxxy::Context.new(Saxxy::NodeRule.new(regexp_or_string, attributes)).tap do |context| __register_context(context, &block) end end |