Skip to content

Commit

Permalink
fix: ruby 3.0 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Feb 17, 2021
1 parent 175d596 commit a03c6c5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- 2.5
- 2.6
- 2.7
- 3.0
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
Expand Down
4 changes: 2 additions & 2 deletions lib/rapid/defineable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def name(new_name = nil)

# Passes all other values through to the DSL for the definition if
# the DSL supoprts it.
def method_missing(name, *args, &block)
def method_missing(name, *args, **kwargs, &block)
if definition.dsl.respond_to?(name)
definition.dsl.send(name, *args, &block)
definition.dsl.send(name, *args, **kwargs, &block)
else
super
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rapid/dsls/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def potential_error(klass, &block)
@definition.potential_errors << klass
end

def argument(*args, &block)
@definition.argument_set.argument(*args, &block)
def argument(*args, **kwargs, &block)
@definition.argument_set.argument(*args, **kwargs, &block)
end

def action(&block)
Expand Down
22 changes: 11 additions & 11 deletions spec/specs/rapid/argument_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
argument :name, type: :string
argument :age, type: :integer
end
as_instance = as.new(name: 'Adam', age: 1234)
as_instance = as.new({ name: 'Adam', age: 1234 })
expect(as_instance[:name]).to eq 'Adam'
expect(as_instance['name']).to eq 'Adam'
expect(as_instance[:age]).to eq 1234
Expand All @@ -108,7 +108,7 @@
argument :age, type: :integer, required: true
end
expect do
as.new(name: 'Adam')
as.new({ name: 'Adam' })
end.to raise_error Rapid::MissingArgumentError
end

Expand All @@ -125,7 +125,7 @@
argument :name, type: :string
end
expect do
as.new(name: 1234)
as.new({ name: 1234 })
end.to raise_error Rapid::InvalidArgumentError do |e|
expect(e.issue).to eq :invalid_scalar
end
Expand All @@ -136,7 +136,7 @@
argument :name, type: :date
end
expect do
as.new(name: '2029-22-34')
as.new({ name: '2029-22-34' })
end.to raise_error Rapid::InvalidArgumentError do |e|
expect(e.issue).to eq :parse_error
end
Expand All @@ -151,7 +151,7 @@
end
end
expect do
as.new(name: 'Not Dave')
as.new({ name: 'Not Dave' })
end.to raise_error Rapid::InvalidArgumentError do |e|
expect(e.argument.name).to eq :name
expect(e.issue).to eq :validation_errors
Expand All @@ -163,7 +163,7 @@
as = Rapid::ArgumentSet.create('ExampleSet') do
argument :names, type: [:string]
end
as_instance = as.new(names: %w[Adam Charlie])
as_instance = as.new({ names: %w[Adam Charlie] })
expect(as_instance[:names]).to be_a Array
expect(as_instance[:names]).to include 'Adam'
expect(as_instance[:names]).to include 'Charlie'
Expand All @@ -174,7 +174,7 @@
argument :names, type: [:string]
end
expect do
as.new(names: ['Adam', 1323])
as.new({ names: ['Adam', 1323] })
end.to raise_error Rapid::InvalidArgumentError do |e|
expect(e.argument.name).to eq :names
expect(e.issue).to eq :invalid_scalar
Expand All @@ -190,7 +190,7 @@
argument :title, type: :string
argument :user, type: as1
end
instance = as2.new(title: 'My title', user: { name: 'Michael' })
instance = as2.new({ title: 'My title', user: { name: 'Michael' } })
expect(instance[:title]).to eq 'My title'
expect(instance[:user]).to be_a Rapid::ArgumentSet
expect(instance[:user][:name]).to eq 'Michael'
Expand All @@ -204,7 +204,7 @@
argument :title, type: :string
argument :user, type: as1
end
instance = as2.new(title: 'My title')
instance = as2.new({ title: 'My title' })
expect(instance[:user]).to be nil
end

Expand All @@ -215,7 +215,7 @@
argument :premium, :boolean
argument :in_debt, :boolean
end
instance = as.new(active: true, premium: false)
instance = as.new({ active: true, premium: false })
expect(instance[:admin]).to eq false
expect(instance[:active]).to eq true
expect(instance[:premium]).to eq false
Expand All @@ -235,7 +235,7 @@
argument :book, type: as2
end
expect do
as3.new(age: 12, book: { title: 'Book', user: { name: 1234 } })
as3.new({ age: 12, book: { title: 'Book', user: { name: 1234 } } })
end.to raise_error Rapid::InvalidArgumentError do |e|
expect(e.index).to be nil
expect(e.argument.name).to eq :name
Expand Down
8 changes: 4 additions & 4 deletions spec/specs/rapid/definitions/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@
field = Rapid::Definitions::Field.new(:id)
field.type = :integer
expect do
field.value(id: '444')
field.value({ id: '444' })
end.to raise_error(Rapid::InvalidScalarValueError)
end

it 'should return an array if defined as an array' do
field = Rapid::Definitions::Field.new(:names)
field.type = :string
field.array = true
value = field.value(names: %w[Adam Michael])
value = field.value({ names: %w[Adam Michael] })
expect(value).to be_a Array
expect(value[0]).to eq 'Adam'
expect(value[1]).to eq 'Michael'
Expand All @@ -158,10 +158,10 @@
field = Rapid::Definitions::Field.new(:users)
field.type = type
field.array = true
value = field.value(users: [
value = field.value({ users: [
{ name: 'Adam', age: 20 },
{ name: 'Michael', age: 25 }
])
] })
expect(value).to be_a Array
expect(value[0]).to be_a Hash

Expand Down
24 changes: 12 additions & 12 deletions spec/specs/rapid/field_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
field.null = true
field_set.add field

hash = field_set.generate_hash(name: nil)
hash = field_set.generate_hash({ name: nil })
expect(hash['name']).to eq nil
end

Expand All @@ -29,11 +29,11 @@
field.condition = proc { |value| value[:name] == 'Sarah' }
field_set.add field

hash = field_set.generate_hash(name: 'Michael', age: 123)
hash = field_set.generate_hash({ name: 'Michael', age: 123 })
expect(hash['name']).to eq 'Michael'
expect(hash.keys).to_not include 'age'

hash = field_set.generate_hash(name: 'Sarah', age: 123)
hash = field_set.generate_hash({ name: 'Sarah', age: 123 })
expect(hash['age']).to eq 123
end

Expand All @@ -51,7 +51,7 @@
field.type = :integer
field_set.add field

hash = field_set.generate_hash(number: 99, thing: { name: 'John' })
hash = field_set.generate_hash({ number: 99, thing: { name: 'John' } })
expect(hash['number']).to eq 99
expect(hash.keys).to_not include 'thing'
end
Expand All @@ -65,7 +65,7 @@
field.type = type
field_set.add field

hash = field_set.generate_hash(user: { name: 'John' })
hash = field_set.generate_hash({ user: { name: 'John' } })
expect(hash['user']['name']).to eq 'John'
end

Expand All @@ -74,7 +74,7 @@
field.type = :string
field_set.add field

hash = field_set.generate_hash(name: :John)
hash = field_set.generate_hash({ name: :John })
expect(hash['name']).to eq 'John'
end

Expand All @@ -84,7 +84,7 @@
field.array = true
field_set.add field

hash = field_set.generate_hash(names: %w[Matthew Mark Michael])
hash = field_set.generate_hash({ names: %w[Matthew Mark Michael] })
expect(hash['names']).to be_a Array
expect(hash['names'].size).to eq 3
expect(hash['names']).to include 'Matthew'
Expand All @@ -102,7 +102,7 @@
field.array = true
field_set.add field

hash = field_set.generate_hash(users: [{ name: 'Matthew' }, { name: 'Mark' }, { name: 'Michael' }])
hash = field_set.generate_hash({ users: [{ name: 'Matthew' }, { name: 'Mark' }, { name: 'Michael' }] })
expect(hash['users']).to be_a Array
expect(hash['users'].size).to eq 3
expect(hash['users'][0]['name']).to include 'Matthew'
Expand All @@ -120,12 +120,12 @@
field.type = polymorph
field_set.add field

hash = field_set.generate_hash(string_or_int: 'Adam')
hash = field_set.generate_hash({ string_or_int: 'Adam' })
expect(hash['string_or_int']).to be_a Hash
expect(hash['string_or_int']['type']).to eq 'string'
expect(hash['string_or_int']['value']).to eq 'Adam'

hash = field_set.generate_hash(string_or_int: 1234)
hash = field_set.generate_hash({ string_or_int: 1234 })
expect(hash['string_or_int']).to be_a Hash
expect(hash['string_or_int']['type']).to eq 'integer'
expect(hash['string_or_int']['value']).to eq 1234
Expand All @@ -142,7 +142,7 @@
field.array = true
field_set.add field

hash = field_set.generate_hash(string_or_int: ['Adam', 1, 'Gavin', 2])
hash = field_set.generate_hash({ string_or_int: ['Adam', 1, 'Gavin', 2] })
expect(hash['string_or_int']).to be_a Array
expect(hash['string_or_int'][0]['type']).to eq 'string'
expect(hash['string_or_int'][0]['value']).to eq 'Adam'
Expand All @@ -164,7 +164,7 @@
field_set.add field

expect do
field_set.generate_hash(value: 1234)
field_set.generate_hash({ value: 1234 })
end.to raise_error Rapid::InvalidPolymorphValueError do |e|
expect(e.polymorph.definition.id).to eq 'MyPolymorph'
end
Expand Down

0 comments on commit a03c6c5

Please sign in to comment.