philiprehberger-enum 0.4.0 → 0.5.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: 2b55ad3be49fe204aa1e0588dca8f25995f0589f49c3c0aa6c77e670e03fa7b0
4
- data.tar.gz: dd879b2ed6f61c12c3805b6b6a86cc4aac4391e383251a3188a60925989685ab
3
+ metadata.gz: 7c70a844631a7b0fb70639905f38d7f4b5d645d46f48a5273c28b0ddf722e0fa
4
+ data.tar.gz: 5870b1e94fcf12b36ec5fd913188bf82aae252f9b3bdaa0e3a23af722dd942df
5
5
  SHA512:
6
- metadata.gz: c29b4d8bb1a41e1a045363596d673febbaf7c6c984d99754ca1caafbbc7535ffdf92cbd3693464af201924f1d1cd2961b135f7c3fd97c4d66f5f34875f93e389
7
- data.tar.gz: 50e33959a328aa0781d2a863df411e76b580ce6788a7b5bbf667078c74dfcb539eaa1b82c92612b2e34e7086dcb32e33de589a43edcc0b6107520c7c72e38e21
6
+ metadata.gz: 1a4d7013cd32642633f2ee97358885cdabe1d166956277f575bc1151656d338a3945a9244ca2d83a1bf128900dacdae78397dbefa53beef0939fd1eb719d6716
7
+ data.tar.gz: 532cee4c349ff698536b9f05b96ec132465c1d8aec1b8b60948a8e0f3485e38a07248bbe553215f065b0af0c41e25804eba53a070d841bbaffc98a53f32e426a
data/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.5.0] - 2026-04-22
11
+
12
+ ### Added
13
+ - `Enum.from_ordinal(ord)` — look up a member by its ordinal position, returning nil if out of range.
14
+ - `Enum.fetch_by_ordinal(ord)` — same lookup that raises `Error` if the ordinal is out of range.
15
+
10
16
  ## [0.4.0] - 2026-04-16
11
17
 
12
18
  ### Added
data/README.md CHANGED
@@ -180,6 +180,8 @@ Status::DRAFT.to_json # => '{"name":"draft","ordinal":0,"value":null}'
180
180
  | `.from_name(name)` | Look up by name (case-insensitive fallback) |
181
181
  | `.from_string(string)` | Look up a member by string name |
182
182
  | `.from_value(val)` | Look up a member by custom value |
183
+ | `.from_ordinal(ord)` | Look up a member by ordinal position (returns nil if out of range) |
184
+ | `.fetch_by_ordinal(ord)` | Same as from_ordinal but raises Error if not found |
183
185
  | `.slice(*names)` | Return members matching the given symbol names, skipping unknowns |
184
186
  | `.sample(n = nil)` | Return a random member, or array of n random members |
185
187
  | `.valid?(name)` | Check if a name is a valid member |
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  class Enum
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -186,6 +186,24 @@ module Philiprehberger
186
186
  from_value(val) || raise(Error, "no member with value #{val.inspect} on #{self}")
187
187
  end
188
188
 
189
+ # Look up a member by its ordinal position
190
+ #
191
+ # @param ord [Integer] the ordinal position (0-indexed)
192
+ # @return [Enum, nil] the member, or nil if the ordinal is out of range
193
+ def from_ordinal(ord)
194
+ freeze_members!
195
+ member_registry.values[ord] if ord.is_a?(Integer) && ord >= 0
196
+ end
197
+
198
+ # Look up a member by ordinal, raising if not found
199
+ #
200
+ # @param ord [Integer] the ordinal position
201
+ # @return [Enum] the member
202
+ # @raise [Error] if the ordinal is not a valid member
203
+ def fetch_by_ordinal(ord)
204
+ from_ordinal(ord) || raise(Error, "no member at ordinal #{ord.inspect} on #{self}")
205
+ end
206
+
189
207
  # Return all member names in declaration order
190
208
  #
191
209
  # @return [Array<Symbol>] frozen array of member names
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-17 00:00:00.000000000 Z
11
+ date: 2026-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Define type-safe enums in Ruby with automatic ordinals, custom values,
14
14
  lookup methods, and Ruby 3.x pattern matching support. A cleaner alternative to