Class: Opener::KafParser::AST::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/kaf_parser/ast/base.rb

Overview

Base node class that provides some common boilerplate for the various other node classes.

Direct Known Subclasses

Document, Opinion, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • attributes (Hash) (defaults to: {})


23
24
25
26
27
28
29
30
31
32
# File 'lib/opener/kaf_parser/ast/base.rb', line 23

def initialize(attributes = {})
  attributes.each do |key, value|
    instance_variable_set("@#{key}", value) if respond_to?(key)
  end

  @children ||= []
  @type     ||= :generic

  after_initialize if respond_to?(:after_initialize)
end

Instance Attribute Details

#childrenArray<Opener::KafParser::AST::Base>

Returns:



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opener/kaf_parser/ast/base.rb', line 17

class Base
  attr_accessor :type, :value, :children

  ##
  # @param [Hash] attributes
  #
  def initialize(attributes = {})
    attributes.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    @children ||= []
    @type     ||= :generic

    after_initialize if respond_to?(:after_initialize)
  end

  ##
  # @return [String]
  #
  def inspect(indent = 0)
    spaces       = ' ' * indent
    child_values = children.map { |c| c.inspect(indent + 2) }
    segments     = ["#{spaces}(#{type}"]

    if value
      segments << "#{value.inspect}"
    end

    unless child_values.empty?
      segments << "\n#{child_values.join("\n")}"
    end

    return segments.join(' ') + ')'
  end

  ##
  # @return [Hash]
  #
  def attributes
    return {}
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def text?
    return type == :text
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def opinion?
    return type == :opinion
  end
end

#typeSymbol

Returns:

  • (Symbol)


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opener/kaf_parser/ast/base.rb', line 17

class Base
  attr_accessor :type, :value, :children

  ##
  # @param [Hash] attributes
  #
  def initialize(attributes = {})
    attributes.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    @children ||= []
    @type     ||= :generic

    after_initialize if respond_to?(:after_initialize)
  end

  ##
  # @return [String]
  #
  def inspect(indent = 0)
    spaces       = ' ' * indent
    child_values = children.map { |c| c.inspect(indent + 2) }
    segments     = ["#{spaces}(#{type}"]

    if value
      segments << "#{value.inspect}"
    end

    unless child_values.empty?
      segments << "\n#{child_values.join("\n")}"
    end

    return segments.join(' ') + ')'
  end

  ##
  # @return [Hash]
  #
  def attributes
    return {}
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def text?
    return type == :text
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def opinion?
    return type == :opinion
  end
end

#valueString

Returns:

  • (String)


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opener/kaf_parser/ast/base.rb', line 17

class Base
  attr_accessor :type, :value, :children

  ##
  # @param [Hash] attributes
  #
  def initialize(attributes = {})
    attributes.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    @children ||= []
    @type     ||= :generic

    after_initialize if respond_to?(:after_initialize)
  end

  ##
  # @return [String]
  #
  def inspect(indent = 0)
    spaces       = ' ' * indent
    child_values = children.map { |c| c.inspect(indent + 2) }
    segments     = ["#{spaces}(#{type}"]

    if value
      segments << "#{value.inspect}"
    end

    unless child_values.empty?
      segments << "\n#{child_values.join("\n")}"
    end

    return segments.join(' ') + ')'
  end

  ##
  # @return [Hash]
  #
  def attributes
    return {}
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def text?
    return type == :text
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def opinion?
    return type == :opinion
  end
end

Instance Method Details

#attributesHash

Returns:

  • (Hash)


56
57
58
# File 'lib/opener/kaf_parser/ast/base.rb', line 56

def attributes
  return {}
end

#inspect(indent = 0) ⇒ String

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opener/kaf_parser/ast/base.rb', line 37

def inspect(indent = 0)
  spaces       = ' ' * indent
  child_values = children.map { |c| c.inspect(indent + 2) }
  segments     = ["#{spaces}(#{type}"]

  if value
    segments << "#{value.inspect}"
  end

  unless child_values.empty?
    segments << "\n#{child_values.join("\n")}"
  end

  return segments.join(' ') + ')'
end

#opinion?TrueClass|FalseClass

Returns:

  • (TrueClass|FalseClass)


70
71
72
# File 'lib/opener/kaf_parser/ast/base.rb', line 70

def opinion?
  return type == :opinion
end

#text?TrueClass|FalseClass

Returns:

  • (TrueClass|FalseClass)


63
64
65
# File 'lib/opener/kaf_parser/ast/base.rb', line 63

def text?
  return type == :text
end