Class: Luoma::StaticAnalysis::StaticVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/luoma/static_analysis.rb,
sig/luoma/static_analysis.rbs

Overview

A variable as a sequence of segments and its location.

Constant Summary collapse

RE_PROPERTY =

Returns:

  • (::Regexp)
/\A[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments, location) ⇒ StaticVariable

Returns a new instance of StaticVariable.

Parameters:



75
76
77
78
# File 'lib/luoma/static_analysis.rb', line 75

def initialize(segments, location)
  @segments = segments
  @location = location
end

Instance Attribute Details

#locationLocation (readonly)

Returns the value of attribute location.

Returns:



71
72
73
# File 'lib/luoma/static_analysis.rb', line 71

def location
  @location
end

#segmentsObject (readonly)

Returns the value of attribute segments.

Returns:

  • (Object)


71
72
73
# File 'lib/luoma/static_analysis.rb', line 71

def segments
  @segments
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/luoma/static_analysis.rb', line 84

def ==(other)
  self.class == other.class &&
    @segments == other.segments
end

#hashObject

Returns:

  • (Object)


91
92
93
# File 'lib/luoma/static_analysis.rb', line 91

def hash
  @segments.hash
end

#rootString

() -> String

Returns:

  • (String)


96
97
98
99
# File 'lib/luoma/static_analysis.rb', line 96

def root
  segment = @segments.first || ""
  segment.is_a?(Array) ? segments_to_s(segment) : segment.to_s
end

#segments_to_s(segments) ⇒ String

Parameters:

  • segments (Object)

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/luoma/static_analysis.rb', line 103

def segments_to_s(segments)
  head, *rest = segments

  head.to_s + rest.map do |segment|
    case segment
    when Array
      "[#{segments_to_s(segment)}]"
    when String
      if segment.match?(RE_PROPERTY) || segment.end_with?("?")
        ".#{segment}"
      else
        "[#{segment.inspect}]"
      end
    else
      "[#{segment}]"
    end
  end.join
end

#to_sString

Returns:

  • (String)


80
81
82
# File 'lib/luoma/static_analysis.rb', line 80

def to_s
  segments_to_s(@segments)
end