Class: Sus::Identity
- Inherits:
-
Object
- Object
- Sus::Identity
- Defined in:
- lib/sus/identity.rb
Overview
Represents a unique identity for a test or context, used for identification and location tracking.
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.
- #The file path.(filepath.) ⇒ Object readonly
- #The line number.(linenumber.) ⇒ Object readonly
- #The name.(name.) ⇒ Object readonly
-
#unique ⇒ Object
readonly
Returns the value of attribute unique.
Class Method Summary collapse
-
.current ⇒ Object
Create an identity for the current location.
-
.file(parent, path, name = path, **options) ⇒ Object
Create an identity for a file.
-
.nested(parent, name, location = nil, **options) ⇒ Object
Create a nested identity.
Instance Method Summary collapse
-
#each {|_self| ... } ⇒ Object
Iterate over this identity and all its parents.
-
#initialize(path, name = nil, line = nil, parent = nil, unique: true) ⇒ Identity
constructor
Initialize a new Identity.
- #inspect ⇒ Object
- #key ⇒ Object
-
#match?(other) ⇒ Boolean
Check if this identity matches another.
-
#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.
- #The parent identity.=(parentidentity. = (value)) ⇒ Object
- #to_location ⇒ Object
- #to_s ⇒ Object
- #Whether this identity is unique.=(thisidentityisunique. = (value)) ⇒ Object
-
#with_line(line) ⇒ Object
Create a new identity with a different line number.
Constructor Details
#initialize(path, name = nil, line = nil, parent = nil, unique: true) ⇒ Identity
Initialize a new Identity.
43 44 45 46 47 48 49 50 51 |
# File 'lib/sus/identity.rb', line 43 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.
67 68 69 |
# File 'lib/sus/identity.rb', line 67 def line @line end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
64 65 66 |
# File 'lib/sus/identity.rb', line 64 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
70 71 72 |
# File 'lib/sus/identity.rb', line 70 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
61 62 63 |
# File 'lib/sus/identity.rb', line 61 def path @path end |
#The file path.(filepath.) ⇒ Object (readonly)
61 |
# File 'lib/sus/identity.rb', line 61 attr :path |
#The line number.(linenumber.) ⇒ Object (readonly)
67 |
# File 'lib/sus/identity.rb', line 67 attr :line |
#The name.(name.) ⇒ Object (readonly)
64 |
# File 'lib/sus/identity.rb', line 64 attr :name |
#unique ⇒ Object (readonly)
Returns the value of attribute unique.
73 74 75 |
# File 'lib/sus/identity.rb', line 73 def unique @unique end |
Class Method Details
.current ⇒ Object
Create an identity for the current location.
33 34 35 |
# File 'lib/sus/identity.rb', line 33 def self.current self.nested(nil, nil, caller_locations(1...2).first) end |
.file(parent, path, name = path, **options) ⇒ Object
Create an identity for a file.
15 16 17 |
# File 'lib/sus/identity.rb', line 15 def self.file(parent, path, name = path, **) self.new(path, name, nil, nil, **) end |
.nested(parent, name, location = nil, **options) ⇒ Object
Create a nested identity.
25 26 27 28 29 |
# File 'lib/sus/identity.rb', line 25 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
Iterate over this identity and all its parents.
112 113 114 115 116 |
# File 'lib/sus/identity.rb', line 112 def each(&block) @parent&.each(&block) yield self end |
#inspect ⇒ Object
89 90 91 |
# File 'lib/sus/identity.rb', line 89 def inspect "\#<#{self.class} #{self.to_s}>" end |
#key ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sus/identity.rb', line 119 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
Check if this identity matches another.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sus/identity.rb', line 96 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/sus/identity.rb', line 135 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 |
#The parent identity.=(parentidentity. = (value)) ⇒ Object
70 |
# File 'lib/sus/identity.rb', line 70 attr :parent |
#to_location ⇒ Object
81 82 83 84 85 86 |
# File 'lib/sus/identity.rb', line 81 def to_location { path: ::File.(@path), line: @line, } end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/sus/identity.rb', line 76 def to_s self.key end |
#Whether this identity is unique.=(thisidentityisunique. = (value)) ⇒ Object
73 |
# File 'lib/sus/identity.rb', line 73 attr :unique |
#with_line(line) ⇒ Object
Create a new identity with a different line number.
56 57 58 |
# File 'lib/sus/identity.rb', line 56 def with_line(line) self.class.new(@path, @name, line, @parent, unique: @unique) end |