protocol-url 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efcdbe6f1de1ec7c6bdadf70824bdd62e0b351c768896e52c34234e982a3527d
4
- data.tar.gz: 3687c0ff3fe5723b3df989d929326062120307e199b801395faf7026dd49606b
3
+ metadata.gz: ff6accb882e5c993ee454290f6d282d67df6b4d13ee01bdd24b042ea764ee03c
4
+ data.tar.gz: abc0495ee011bccd902d3f60bebcf4d8231e3559b14305fe5594d62df285eeae
5
5
  SHA512:
6
- metadata.gz: cb62cd371a93e7fadf9b51823382d06034365301470336733c3870798c29839a5c8d550706dc1e69fb5de964d442192299f1f8fdd351df836b8852bc0e730fd9
7
- data.tar.gz: e4bee5e0cf8ee2aac01942418e0045cd233b0a901535891933bc274eff4bb9c0fff9edb2164eedbd99dda633a68fd91ce45dc823dabb5f5516d5818752301587
6
+ metadata.gz: 1a1852872d62c65b0d78f8cd698deaaca69ceea65b79fb2e9d04b0b1b347cf6555ba41c27b0c836e2b963981f0c9be9a8feeec55622cdede7018a01321d7bca6
7
+ data.tar.gz: 4c6a01694dda0e059d944e5155f365c27c00c244e8e5ea7310288b875f2f307e4ef0c610dd8ea906e30fd5338d13f33fb1d2b83dcd5c076d57e34a60eb515b3b
checksums.yaml.gz.sig CHANGED
Binary file
@@ -119,6 +119,45 @@ module Protocol
119
119
  return join(simplify(components))
120
120
  end
121
121
 
122
+ # Calculate the relative path from one absolute path to another.
123
+ #
124
+ # This is useful for generating relative URLs from one location to another,
125
+ # such as creating page-specific import maps or relative links.
126
+ #
127
+ # @parameter target [String] The destination path (where you want to go).
128
+ # @parameter from [String] The source path (where you are starting from).
129
+ # @returns [String] The relative path from `from` to `target`.
130
+ #
131
+ # @example Calculate relative path between pages.
132
+ # Path.relative("/_components/app.js", "/foo/bar/")
133
+ # # => "../../_components/app.js"
134
+ #
135
+ # @example Calculate relative path in same directory.
136
+ # Path.relative("/docs/guide.html", "/docs/index.html")
137
+ # # => "guide.html"
138
+ def self.relative(target, from)
139
+ target_components = split(target)
140
+ from_components = split(from)
141
+
142
+ # Remove the last component from 'from' to get the directory
143
+ from_components = from_components[0...-1] if from_components.size > 0
144
+
145
+ # Find the common prefix
146
+ common_length = 0
147
+ [target_components.size, from_components.size].min.times do |i|
148
+ break if target_components[i] != from_components[i]
149
+ common_length = i + 1
150
+ end
151
+
152
+ # Calculate how many levels to go up
153
+ up_levels = from_components.size - common_length
154
+
155
+ # Build the relative path components
156
+ relative_components = [".."] * up_levels + target_components[common_length..-1]
157
+
158
+ return join(relative_components)
159
+ end
160
+
122
161
  # Convert a URL path to a local file system path using the platform's file separator.
123
162
  #
124
163
  # This method splits the URL path on `/` characters, unescapes each component using
@@ -95,14 +95,6 @@ module Protocol
95
95
  [@path, @query, @fragment, @parameters]
96
96
  end
97
97
 
98
- # Compare two references.
99
- #
100
- # @parameter other [Reference] The other reference to compare.
101
- # @returns [Integer] -1, 0, 1 if the reference is less than, equal to, or greater than the other reference.
102
- def <=> other
103
- to_ary <=> other.to_ary
104
- end
105
-
106
98
  # @returns [Boolean] Whether the reference has parameters.
107
99
  def parameters?
108
100
  @parameters and !@parameters.empty?
@@ -135,14 +135,38 @@ module Protocol
135
135
  [@path, @query, @fragment]
136
136
  end
137
137
 
138
+ def hash
139
+ to_ary.hash
140
+ end
141
+
142
+ def equal?(other)
143
+ to_ary == other.to_ary
144
+ end
145
+
138
146
  def <=>(other)
139
147
  to_ary <=> other.to_ary
140
148
  end
141
149
 
150
+ def ==(other)
151
+ to_ary == other.to_ary
152
+ end
153
+
154
+ def ===(other)
155
+ to_s === other
156
+ end
157
+
142
158
  def to_s
143
159
  append
144
160
  end
145
161
 
162
+ def as_json(...)
163
+ to_s
164
+ end
165
+
166
+ def to_json(...)
167
+ as_json.to_json(...)
168
+ end
169
+
146
170
  def inspect
147
171
  "#<#{self.class} #{to_s}>"
148
172
  end
@@ -7,6 +7,6 @@
7
7
  module Protocol
8
8
  # @namespace
9
9
  module URL
10
- VERSION = "0.2.0"
10
+ VERSION = "0.4.0"
11
11
  end
12
12
  end
data/readme.md CHANGED
@@ -34,6 +34,22 @@ This project is best served by a collaborative and respectful environment. Treat
34
34
 
35
35
  Please see the [project releases](https://github.com/socketry/protocol-urlreleases/index) for all releases.
36
36
 
37
+ ### v0.4.0
38
+
39
+ - Add comparison methods to `Protocol::URL::Relative` (and by inheritance to `Protocol::URL::Absolute`):
40
+ - `#==` for structural equality comparison (compares path, query, fragment components).
41
+ - `#===` for string equality comparison (enables case statement matching).
42
+ - `#<=>` for ordering and sorting.
43
+ - `#hash` for hash key support.
44
+ - `#equal?` for component-based equality checking.
45
+ - Add JSON serialization support to `Protocol::URL::Relative`:
46
+ - `#as_json` returns the string representation.
47
+ - `#to_json` returns a JSON-encoded string.
48
+
49
+ ### v0.3.0
50
+
51
+ - Add `relative(target, from)` for computing relative paths between URLs.
52
+
37
53
  ### v0.2.0
38
54
 
39
55
  - Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
data/releases.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Releases
2
2
 
3
+ ## v0.4.0
4
+
5
+ - Add comparison methods to `Protocol::URL::Relative` (and by inheritance to `Protocol::URL::Absolute`):
6
+ - `#==` for structural equality comparison (compares path, query, fragment components).
7
+ - `#===` for string equality comparison (enables case statement matching).
8
+ - `#<=>` for ordering and sorting.
9
+ - `#hash` for hash key support.
10
+ - `#equal?` for component-based equality checking.
11
+ - Add JSON serialization support to `Protocol::URL::Relative`:
12
+ - `#as_json` returns the string representation.
13
+ - `#to_json` returns a JSON-encoded string.
14
+
15
+ ## v0.3.0
16
+
17
+ - Add `relative(target, from)` for computing relative paths between URLs.
18
+
3
19
  ## v0.2.0
4
20
 
5
21
  - Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file