Class: Ruby::Rego::AST::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/ast/base.rb

Overview

Base class for all AST nodes.

Provides location tracking, a visitor entry point, and common debugging helpers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • location (Location, nil) (defaults to: nil)


12
13
14
# File 'lib/ruby/rego/ast/base.rb', line 12

def initialize(location: nil)
  @location = location
end

Instance Attribute Details

#locationLocation? (readonly)

Returns:



17
18
19
# File 'lib/ruby/rego/ast/base.rb', line 17

def location
  @location
end

Class Method Details

.format_value(value) ⇒ String

Parameters:

  • value (Object)

Returns:

  • (String)


84
85
86
87
88
89
90
91
# File 'lib/ruby/rego/ast/base.rb', line 84

def self.format_value(value)
  case value
  when String, Symbol, Numeric, Array, Hash, TrueClass, FalseClass, NilClass
    value.inspect
  else
    value.to_s
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/ruby/rego/ast/base.rb', line 62

def ==(other)
  return false unless other.instance_of?(self.class)

  instance_variables.sort.all? do |variable|
    instance_variable_get(variable) == other.instance_variable_get(variable)
  end
end

#accept(visitor) ⇒ Object

Parameters:

  • visitor (Object)

Returns:

  • (Object)


21
22
23
# File 'lib/ruby/rego/ast/base.rb', line 21

def accept(visitor)
  visitor.visit(self)
end

#deconstruct_keys(keys) ⇒ Hash<Symbol, Object>

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash<Symbol, Object>)


27
28
29
30
31
32
# File 'lib/ruby/rego/ast/base.rb', line 27

def deconstruct_keys(keys)
  attributes = deconstruct_attributes
  allowed = deconstructable_keys
  filtered = attributes.slice(*allowed)
  keys ? filtered.slice(*keys) : filtered
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


72
73
74
# File 'lib/ruby/rego/ast/base.rb', line 72

def eql?(other)
  self == other
end

#hashInteger

Returns:

  • (Integer)


77
78
79
80
# File 'lib/ruby/rego/ast/base.rb', line 77

def hash
  values = instance_variables.sort.map { |variable| instance_variable_get(variable) }
  [self.class, values].hash
end

#to_sString

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
# File 'lib/ruby/rego/ast/base.rb', line 50

def to_s
  klass = self.class
  attributes = instance_variables.sort.map do |variable|
    value = instance_variable_get(variable)
    "#{variable.to_s.delete_prefix("@")}=#{klass.format_value(value)}"
  end

  "#{klass.name}(#{attributes.join(", ")})"
end