Skip to content

Commit 93a4ac7

Browse files
committed
feat: allow using objects as top level fieldsets
1 parent 1305178 commit 93a4ac7

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/apia/definitions/endpoint.rb

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ def potential_errors
3636
@potential_errors ||= Apia::ErrorSet.new
3737
end
3838

39+
def fields=(fieldset)
40+
unless @fields.empty?
41+
raise Apia::StandardError, 'Cannot set the fieldset on an endpoint that already has fields defined'
42+
end
43+
44+
@fields = fieldset
45+
@fields_overriden = true
46+
end
47+
48+
def fields_overriden?
49+
@fields_overriden == true
50+
end
51+
3952
def dsl
4053
@dsl ||= DSLs::Endpoint.new(self)
4154
end

lib/apia/dsls/endpoint.rb

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def http_status(status)
4141
end
4242

4343
def field(name, *args, type: nil, **options, &block)
44+
if @definition.fields_overriden?
45+
raise Apia::StandardError, 'Cannot add fields to an endpoint that has a separate fieldset'
46+
end
47+
4448
if pagination_options = options.delete(:paginate)
4549

4650
unless @definition.paginated_field.nil?
@@ -64,6 +68,10 @@ def field(name, *args, type: nil, **options, &block)
6468
super(name, *args, type: type, **options, &block)
6569
end
6670

71+
def fields(fieldset)
72+
@definition.fields = fieldset
73+
end
74+
6775
def scopes(*names)
6876
names.each { |name| scope(name) }
6977
end

spec/specs/apia/dsls/endpoint_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@
8989
end
9090

9191
context '#field' do
92+
context 'when an existing fieldset has been provided' do
93+
it 'raises an error if another field is defined' do
94+
dsl.fields Apia::FieldSet.new
95+
expect { dsl.field :name, type: :string }.to raise_error Apia::StandardError
96+
end
97+
end
98+
9299
context 'when paginated' do
93100
it 'should add a `page` argument' do
94101
dsl.field :widgets, type: [:string], paginate: true
@@ -125,6 +132,19 @@
125132
end
126133
end
127134

135+
context '#fields' do
136+
it 'sets the endpoints fieldset to the given field set' do
137+
fieldset = Apia::FieldSet.new
138+
dsl.fields fieldset
139+
expect(endpoint.fields).to eq fieldset
140+
end
141+
142+
it 'raises an error if fields have already been defined' do
143+
dsl.field :example, :string
144+
expect { dsl.fields Apia::FieldSet.new }.to raise_error Apia::StandardError
145+
end
146+
end
147+
128148
context '#scope' do
129149
it 'should add a scope' do
130150
dsl.scope 'example:read'

0 commit comments

Comments
 (0)