Class: Sus::Identity
- Inherits:
-
Object
- Object
- Sus::Identity
- Defined in:
- lib/sus/identity.rb
Instance Attribute Summary collapse
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#unique ⇒ Object
readonly
Returns the value of attribute unique.
Class Method Summary collapse
- .current ⇒ Object
- .file(parent, path, name = path, **options) ⇒ Object
- .nested(parent, name, location = nil, **options) ⇒ Object
Instance Method Summary collapse
- #each {|_self| ... } ⇒ Object
-
#initialize(path, name = nil, line = nil, parent = nil, unique: true) ⇒ Identity
constructor
A new instance of Identity.
- #inspect ⇒ Object
- #key ⇒ Object
- #match?(other) ⇒ Boolean
-
#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.
- #to_location ⇒ Object
- #to_s ⇒ Object
- #with_line(line) ⇒ Object
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
#line ⇒ Object (readonly)
Returns the value of attribute line.
39 40 41 |
# File 'lib/sus/identity.rb', line 39 def line @line end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
38 39 40 |
# File 'lib/sus/identity.rb', line 38 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
40 41 42 |
# File 'lib/sus/identity.rb', line 40 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
37 38 39 |
# File 'lib/sus/identity.rb', line 37 def path @path end |
#unique ⇒ Object (readonly)
Returns the value of attribute unique.
41 42 43 |
# File 'lib/sus/identity.rb', line 41 def unique @unique end |
Class Method Details
.current ⇒ Object
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, **) self.new(path, name, nil, nil, **) 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, **) location ||= caller_locations(3...4).first self.new(location.path, name, location.lineno, parent, **) end |
Instance Method Details
#each {|_self| ... } ⇒ Object
72 73 74 75 76 |
# File 'lib/sus/identity.rb', line 72 def each(&block) @parent&.each(&block) yield self end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/sus/identity.rb', line 54 def inspect "\#<#{self.class} #{self.to_s}>" end |
#key ⇒ Object
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
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_location ⇒ Object
47 48 49 50 51 52 |
# File 'lib/sus/identity.rb', line 47 def to_location { path: ::File.(@path), line: @line, } end |
#to_s ⇒ Object
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 |