Class: Saxxy::Context

Inherits:
Object
  • Object
show all
Includes:
Activatable
Defined in:
lib/saxxy/context.rb

Overview

Context describes, semantically, an XML tag-context. For example an XML tag-context: <div>This is a context</div>

Whether a tag-context is described by a Context object depends on whether the Context's activation rule matches this tag-context. Because a Context can be activated on a tag-context that matches it's activation rule it includes the “Activatable” module.

A context can belong to a parent context and thus it may have a `parent_context` attribute that points to it's parent. A context may also have `child_contexts`.

Author:

  • rubymaniac

Constant Summary

Constants included from Activatable

Activatable::DLEVEL_MIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Activatable

#activate_on, #deactivate_on, included, #initialize_activatable, #on_activation, #on_deactivation

Constructor Details

#initialize(activation_rule = nil) ⇒ Context

Initializes a context with an `activation_rule` (defaults to `nil`).


43
44
45
46
47
# File 'lib/saxxy/context.rb', line 43

def initialize(activation_rule = nil)
  @child_contexts = []
  @actions = []
  initialize_activatable(activation_rule)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions


36
37
38
# File 'lib/saxxy/context.rb', line 36

def actions
  @actions
end

#activation_ruleNodeRule (readonly)


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
84
85
86
# File 'lib/saxxy/context.rb', line 32

class Context
  include Activatable

  attr_accessor :parent_context
  attr_reader :child_contexts, :actions

  # Initializes a context with an `activation_rule` (defaults to `nil`).
  #
  # @param activation_rule [NodeRule] an instance of NodeRule or nil to
  #   declare that this context is automatically active.
  #
  def initialize(activation_rule = nil)
    @child_contexts = []
    @actions = []
    initialize_activatable(activation_rule)
  end

  # Registers either a Context as a `child_context` by setting
  # it's `parent_context` attribute to itself and appending it to
  # the `child_contexts` array, or a NodeAction by appending it to
  # the `actions` array.
  #
  # @param obj [Context|NodeAction] An instance of Context or
  #   an instance of NodeAction.
  #
  # @return [Context] self, i.e. the context
  #
  def register(obj)
    case obj
    when Context
      obj.parent_context = self
      @child_contexts << obj
    when NodeAction
      @actions << obj
    end
    self
  end

  # Checks whether this context has a parent.
  #
  # @return [Boolean] true if it has a `parent_context`, false otherwise
  #
  def has_parent?
    !parent_context.nil?
  end

  # Checks whether this context is a root context,
  # i.e. it has no `parent_context`
  #
  # @return [Boolean] false if it has a `parent_context`, true otherwise
  #
  def root?
    !has_parent?
  end
end

#child_contextsArray<Context> (readonly)


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
84
85
86
# File 'lib/saxxy/context.rb', line 32

class Context
  include Activatable

  attr_accessor :parent_context
  attr_reader :child_contexts, :actions

  # Initializes a context with an `activation_rule` (defaults to `nil`).
  #
  # @param activation_rule [NodeRule] an instance of NodeRule or nil to
  #   declare that this context is automatically active.
  #
  def initialize(activation_rule = nil)
    @child_contexts = []
    @actions = []
    initialize_activatable(activation_rule)
  end

  # Registers either a Context as a `child_context` by setting
  # it's `parent_context` attribute to itself and appending it to
  # the `child_contexts` array, or a NodeAction by appending it to
  # the `actions` array.
  #
  # @param obj [Context|NodeAction] An instance of Context or
  #   an instance of NodeAction.
  #
  # @return [Context] self, i.e. the context
  #
  def register(obj)
    case obj
    when Context
      obj.parent_context = self
      @child_contexts << obj
    when NodeAction
      @actions << obj
    end
    self
  end

  # Checks whether this context has a parent.
  #
  # @return [Boolean] true if it has a `parent_context`, false otherwise
  #
  def has_parent?
    !parent_context.nil?
  end

  # Checks whether this context is a root context,
  # i.e. it has no `parent_context`
  #
  # @return [Boolean] false if it has a `parent_context`, true otherwise
  #
  def root?
    !has_parent?
  end
end

#parent_contextContext


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
84
85
86
# File 'lib/saxxy/context.rb', line 32

class Context
  include Activatable

  attr_accessor :parent_context
  attr_reader :child_contexts, :actions

  # Initializes a context with an `activation_rule` (defaults to `nil`).
  #
  # @param activation_rule [NodeRule] an instance of NodeRule or nil to
  #   declare that this context is automatically active.
  #
  def initialize(activation_rule = nil)
    @child_contexts = []
    @actions = []
    initialize_activatable(activation_rule)
  end

  # Registers either a Context as a `child_context` by setting
  # it's `parent_context` attribute to itself and appending it to
  # the `child_contexts` array, or a NodeAction by appending it to
  # the `actions` array.
  #
  # @param obj [Context|NodeAction] An instance of Context or
  #   an instance of NodeAction.
  #
  # @return [Context] self, i.e. the context
  #
  def register(obj)
    case obj
    when Context
      obj.parent_context = self
      @child_contexts << obj
    when NodeAction
      @actions << obj
    end
    self
  end

  # Checks whether this context has a parent.
  #
  # @return [Boolean] true if it has a `parent_context`, false otherwise
  #
  def has_parent?
    !parent_context.nil?
  end

  # Checks whether this context is a root context,
  # i.e. it has no `parent_context`
  #
  # @return [Boolean] false if it has a `parent_context`, true otherwise
  #
  def root?
    !has_parent?
  end
end

Instance Method Details

#has_parent?Boolean

Checks whether this context has a parent.


74
75
76
# File 'lib/saxxy/context.rb', line 74

def has_parent?
  !parent_context.nil?
end

#register(obj) ⇒ Context

Registers either a Context as a `child_context` by setting it's `parent_context` attribute to itself and appending it to the `child_contexts` array, or a NodeAction by appending it to the `actions` array.


59
60
61
62
63
64
65
66
67
68
# File 'lib/saxxy/context.rb', line 59

def register(obj)
  case obj
  when Context
    obj.parent_context = self
    @child_contexts << obj
  when NodeAction
    @actions << obj
  end
  self
end

#root?Boolean

Checks whether this context is a root context, i.e. it has no `parent_context`


83
84
85
# File 'lib/saxxy/context.rb', line 83

def root?
  !has_parent?
end