Skip to content

Commit 5e6e26e

Browse files
committed
feat: more bracket initializers
1 parent 2d167ee commit 5e6e26e

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ permissions:
99

1010
jobs:
1111
release:
12+
needs: rspec
1213
name: Release
1314
runs-on: ubuntu-latest
1415
permissions:

lib/anchor/types.rb

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,58 @@ class BigDecimal; end
77
class Boolean; end
88
class Null; end
99
class Unknown; end
10-
Identity = Data.define(:type)
11-
Record = Data.define(:value_type)
12-
Maybe = Data.define(:type)
13-
Array = Data.define(:type)
10+
11+
Identity = Data.define(:type) do
12+
class << self
13+
alias_method :[], :new
14+
end
15+
end
16+
17+
Record = Data.define(:value_type) do
18+
class << self
19+
alias_method :[], :new
20+
end
21+
end
22+
23+
Maybe = Data.define(:type) do
24+
class << self
25+
alias_method :[], :new
26+
end
27+
end
28+
29+
Array = Data.define(:type) do
30+
class << self
31+
alias_method :[], :new
32+
end
33+
end
34+
1435
Literal = Data.define(:value) do
15-
def [](value)
16-
new(value)
36+
class << self
37+
alias_method :[], :new
1738
end
1839
end
40+
1941
Union = Data.define(:types) do
42+
class << self
43+
def [](*types) = new(types)
44+
end
45+
2046
def |(other)
2147
self.class.new(types + [other])
2248
end
2349
end
24-
Intersection = Data.define(:types)
50+
51+
Intersection = Data.define(:types) do
52+
class << self
53+
def [](*types) = new(types)
54+
end
55+
end
56+
2557
Reference = Data.define(:name) do
58+
class << self
59+
alias_method :[], :new
60+
end
61+
2662
def anchor_schema_name = name
2763

2864
def |(other)

spec/anchor/types_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "rails_helper"
2+
3+
RSpec.describe 'Types' do
4+
let(:string) { Anchor::Types::String }
5+
let(:integer) { Anchor::Types::Integer }
6+
let(:types) { [string, integer] }
7+
8+
describe Anchor::Types::Union do
9+
it 'can be initialized via []' do
10+
union = described_class[string, integer]
11+
expect(union.types).to eql(types)
12+
end
13+
end
14+
15+
describe Anchor::Types::Intersection do
16+
it 'can be initialized via []' do
17+
intersection = described_class[string, integer]
18+
expect(intersection.types).to eql(types)
19+
end
20+
end
21+
end

spec/example/app/resources/exhaustive_resource.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class ExhaustiveResource < ApplicationResource
22
class AssertedObject < Types::Object
3-
property :a, Types::Literal.new("a")
4-
property "b-dash", Types::Literal.new(1)
5-
property "optional-dash", Types::Literal.new(1), optional: true
6-
property :c, Types::Maybe.new(Types::String)
7-
property :d_optional, Types::Maybe.new(Types::String), optional: true
3+
property :a, Types::Literal["a"]
4+
property "b-dash", Types::Literal[1]
5+
property "optional-dash", Types::Literal[1], optional: true
6+
property :c, Types::Maybe[(Types::String)]
7+
property :d_optional, Types::Maybe[Types::String], optional: true
88
end
99

1010
attribute :asserted_string, Types::String, description: "My asserted string."
@@ -13,10 +13,10 @@ class AssertedObject < Types::Object
1313
attribute :asserted_null
1414
attribute :asserted_unknown, Types::Unknown
1515
attribute :asserted_object, AssertedObject
16-
attribute :asserted_maybe_object, Types::Maybe.new(AssertedObject)
17-
attribute :asserted_array_record, Types::Array.new(Types::Record.new(Types::Integer))
16+
attribute :asserted_maybe_object, Types::Maybe[AssertedObject]
17+
attribute :asserted_array_record, Types::Array[Types::Record.new(Types::Integer)]
1818
attribute :asserted_union
19-
attribute :asserted_union_array, Types::Array.new(Types::Union.new([Types::String, Types::Float]))
19+
attribute :asserted_union_array, Types::Array[Types::Union[Types::String, Types::Float]]
2020
attribute :with_description, Types::String, description: "This is a provided description."
2121
attribute :inferred_unknown
2222

0 commit comments

Comments
 (0)