Class: Sus::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/identity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name = nil, line = nil, parent = nil, unique: true) ⇒ Identity

Returns a new instance of Identity.



23
24
25
26
27
28
29
30
31
# File 'lib/sus/identity.rb', line 23

def initialize(path, name = nil, line = nil, parent = nil, unique: true)
	@path = path
	@name = name
	@line = line
	@parent = parent
	@unique = unique
	
	@key = nil
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



39
40
41
# File 'lib/sus/identity.rb', line 39

def line
  @line
end

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/sus/identity.rb', line 38

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



40
41
42
# File 'lib/sus/identity.rb', line 40

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



37
38
39
# File 'lib/sus/identity.rb', line 37

def path
  @path
end

#uniqueObject (readonly)

Returns the value of attribute unique.



41
42
43
# File 'lib/sus/identity.rb', line 41

def unique
  @unique
end

Class Method Details

.currentObject



18
19
20
# File 'lib/sus/identity.rb', line 18

def self.current
	self.nested(nil, nil, caller_locations(1...2).first)
end

.file(parent, path, name = path, **options) ⇒ Object



8
9
10
# File 'lib/sus/identity.rb', line 8

def self.file(parent, path, name = path, **options)
	self.new(path, name, nil, nil, **options)
end

.nested(parent, name, location = nil, **options) ⇒ Object



12
13
14
15
16
# File 'lib/sus/identity.rb', line 12

def self.nested(parent, name, location = nil, **options)
	location ||= caller_locations(3...4).first
	
	self.new(location.path, name, location.lineno, parent, **options)
end

Instance Method Details

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Sus::Identity)

    the object that the method was called on



72
73
74
75
76
# File 'lib/sus/identity.rb', line 72

def each(&block)
	@parent&.each(&block)
	
	yield self
end

#inspectObject



54
55
56
# File 'lib/sus/identity.rb', line 54

def inspect
	"\#<#{self.class} #{self.to_s}>"
end

#keyObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sus/identity.rb', line 78

def key
	unless @key
		key = Array.new
		
		# For a specific leaf node, the last part is not unique, i.e. it must be identified explicitly.
		append_unique_key(key, @unique == true ? false : @unique)
		
		@key = key.join(":")
	end
	
	return @key
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sus/identity.rb', line 58

def match?(other)
	if path = other.path
		return false unless path === @path
	end
	
	if name = other.name
		return false unless name === @name
	end
	
	if line = other.line
		return false unless line === @line
	end
end

#scoped(locations = nil) ⇒ Object

Given a set of locations, find the first one which matches this identity and return a new identity with the updated line number. This can be used to extract a location from a backtrace.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sus/identity.rb', line 92

def scoped(locations = nil)
	if locations
		# This code path is normally taken if we've got an exception with a backtrace:
		locations.each do |location|
			if location.path == @path
				return self.with_line(location.lineno)
			end
		end
	else
		# In theory this should be a bit faster:
		each_caller_location do |location|
			if location.path == @path
				return self.with_line(location.lineno)
			end
		end
	end
	
	return self
end

#to_locationObject



47
48
49
50
51
52
# File 'lib/sus/identity.rb', line 47

def to_location
	{
		path: ::File.expand_path(@path),
		line: @line,
	}
end

#to_sObject



43
44
45
# File 'lib/sus/identity.rb', line 43

def to_s
	self.key
end

#with_line(line) ⇒ Object



33
34
35
# File 'lib/sus/identity.rb', line 33

def with_line(line)
	self.class.new(@path, @name, line, @parent, unique: @unique)
end