Class: Saxxy::NodeAction

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

Overview

NodeAction describes something that should be run on a node. In order to check whether to run this action it accepts as the first argument an activation_rule.

Author:

  • rubymaniac

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(activation_rule, context = self, &block) ⇒ NodeAction

Initializes a NodeAction with an `activation_rule` a context to run its action (block) and the block.


31
32
33
34
35
# File 'lib/saxxy/node_action.rb', line 31

def initialize(activation_rule, context = self, &block)
  @activation_rule = activation_rule
  @ctx = context
  @action = block_given? ? block : ->(e) { e }
end

Instance Attribute Details

#actionProc (readonly)


17
18
19
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
# File 'lib/saxxy/node_action.rb', line 17

class NodeAction
  attr_reader :activation_rule, :action

  # Initializes a NodeAction with an `activation_rule` a context to run
  # its action (block) and the block.
  #
  # @param activation_rule [NodeRule] an instance of NodeRule
  #   used to check whether to run this action on a node
  #
  # @param context [Object] a context (object) on which the block
  #   will be evaluated
  #
  # @param block [Proc] a block that will get evaluated on context
  #
  def initialize(activation_rule, context = self, &block)
    @activation_rule = activation_rule
    @ctx = context
    @action = block_given? ? block : ->(e) { e }
  end

  # Delegates the call to its `activation_rule`
  #
  # @param element_name [String] the name of a node
  #
  # @param attributes [Hash<String, String>] the attributes of a node
  #
  # @return [Boolean] whether it matches the node
  #
  def matches(element_name, attributes)
    activation_rule.matches(element_name, attributes)
  end

  # Evaluates the block that was given to the constructor on the context
  # and passes the arguments to the block
  #
  # @param args [Array] variable arguments that pass to the block
  #
  def call(*args)
    @ctx.instance_exec(args, &action)
  end
end

#activation_ruleContext (readonly)


17
18
19
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
# File 'lib/saxxy/node_action.rb', line 17

class NodeAction
  attr_reader :activation_rule, :action

  # Initializes a NodeAction with an `activation_rule` a context to run
  # its action (block) and the block.
  #
  # @param activation_rule [NodeRule] an instance of NodeRule
  #   used to check whether to run this action on a node
  #
  # @param context [Object] a context (object) on which the block
  #   will be evaluated
  #
  # @param block [Proc] a block that will get evaluated on context
  #
  def initialize(activation_rule, context = self, &block)
    @activation_rule = activation_rule
    @ctx = context
    @action = block_given? ? block : ->(e) { e }
  end

  # Delegates the call to its `activation_rule`
  #
  # @param element_name [String] the name of a node
  #
  # @param attributes [Hash<String, String>] the attributes of a node
  #
  # @return [Boolean] whether it matches the node
  #
  def matches(element_name, attributes)
    activation_rule.matches(element_name, attributes)
  end

  # Evaluates the block that was given to the constructor on the context
  # and passes the arguments to the block
  #
  # @param args [Array] variable arguments that pass to the block
  #
  def call(*args)
    @ctx.instance_exec(args, &action)
  end
end

Instance Method Details

#call(*args) ⇒ Object

Evaluates the block that was given to the constructor on the context and passes the arguments to the block


54
55
56
# File 'lib/saxxy/node_action.rb', line 54

def call(*args)
  @ctx.instance_exec(args, &action)
end

#matches(element_name, attributes) ⇒ Boolean

Delegates the call to its `activation_rule`


45
46
47
# File 'lib/saxxy/node_action.rb', line 45

def matches(element_name, attributes)
  activation_rule.matches(element_name, attributes)
end