Skip to content

Commit

Permalink
Add support for Ruby 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed May 30, 2022
1 parent 8dd253b commit 1f4b5be
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 39 deletions.
33 changes: 18 additions & 15 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'ruby2_keywords'
require 'forwardable'
require 'dry/inflector'
require 'contextualizer'
Expand All @@ -9,21 +10,23 @@
module Pathway
Inflector = Dry::Inflector.new
class Operation
def self.plugin(name, *args)
require "pathway/plugins/#{Inflector.underscore(name)}" if name.is_a?(Symbol)
class << self
ruby2_keywords def plugin(name, *args)
require "pathway/plugins/#{Inflector.underscore(name)}" if name.is_a?(Symbol)

plugin = name.is_a?(Module) ? name : Plugins.const_get(Inflector.camelize(name))
plugin = name.is_a?(Module) ? name : Plugins.const_get(Inflector.camelize(name))

self.extend plugin::ClassMethods if plugin.const_defined? :ClassMethods
self.include plugin::InstanceMethods if plugin.const_defined? :InstanceMethods
self::DSL.include plugin::DSLMethods if plugin.const_defined? :DSLMethods
self.extend plugin::ClassMethods if plugin.const_defined? :ClassMethods
self.include plugin::InstanceMethods if plugin.const_defined? :InstanceMethods
self::DSL.include plugin::DSLMethods if plugin.const_defined? :DSLMethods

plugin.apply(self, *args) if plugin.respond_to?(:apply)
end
plugin.apply(self, *args) if plugin.respond_to?(:apply)
end

def self.inherited(subclass)
super
subclass.const_set :DSL, Class.new(self::DSL)
def inherited(subclass)
super
subclass.const_set :DSL, Class.new(self::DSL)
end
end

class DSL
Expand Down Expand Up @@ -90,7 +93,7 @@ def process(&bl)
end
end

def call(ctx, *params)
ruby2_keywords def call(ctx, *params)
new(ctx).call(*params)
end

Expand Down Expand Up @@ -137,7 +140,7 @@ def run(&bl)
end

# Execute step and preserve the former state
def step(callable, *args)
ruby2_keywords def step(callable, *args)
bl = _callable(callable)

@result = @result.tee { |state| bl.call(state, *args) }
Expand Down Expand Up @@ -190,9 +193,9 @@ def wrap(obj)
def _callable(callable)
case callable
when Proc
-> *args { @operation.instance_exec(*args, &callable) }
-> *args { @operation.instance_exec(*args, &callable) }.ruby2_keywords
when Symbol
-> *args { @operation.send(callable, *args) }
-> *args { @operation.send(callable, *args) }.ruby2_keywords
else
callable
end
Expand Down
8 changes: 4 additions & 4 deletions lib/pathway/plugins/dry_validation/v1_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def contract(base = nil, &block)
end
end

def params(*args, &block)
ruby2_keywords def params(*args, &block)
contract { params(*args, &block) }
end

Expand All @@ -29,8 +29,8 @@ def contract_class= klass
@builded_contract = @contract_options.empty? && klass.schema ? klass.new : nil
end

def build_contract(opts = {})
@builded_contract || contract_class.new(opts)
ruby2_keywords def build_contract(opts = {})
@builded_contract || contract_class.new(**opts)
end

def inherited(subclass)
Expand Down Expand Up @@ -61,7 +61,7 @@ def validate(state, with: nil)
.then { |params| state.update(params: params) }
end

def validate_with(input, opts = {})
ruby2_keywords def validate_with(input, opts = {})
result = contract(opts).call(input)

result.success? ? wrap(result.values.to_h) : error(:validation, details: result.errors.to_h)
Expand Down
4 changes: 2 additions & 2 deletions lib/pathway/plugins/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Pathway
module Plugins
module Responder
module ClassMethods
def call(ctx, *params, &bl)
result = super(ctx, *params)
ruby2_keywords def call(*args, &bl)
result = super(*args)
block_given? ? Responder.respond(result, &bl) : result
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pathway/plugins/sequel_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def find_model_with(key, dataset = model_class, column = search_field, error_mes
end
end

def self.apply(operation, model: nil, **args)
operation.model(model, args) if model
def self.apply(operation, model: nil, **kwargs)
operation.model(model, **kwargs) if model
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion pathway.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "dry-validation", ">= 0.11"
spec.add_development_dependency "bundler", "~> 2.3.7"
spec.add_development_dependency "sequel", "~> 5.25.0"
spec.add_development_dependency "sequel", "~> 5.0"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.11"
spec.add_development_dependency "simplecov-lcov", '~> 0.8.0'
Expand Down
7 changes: 4 additions & 3 deletions spec/plugins/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def custom_validate(state)
state[:params] = @validator.call(state)
end

def get_value(params:, **)
@back_end.call(params)
def get_value(state)
@back_end.call(state[:params])
end

def get_aux_value(state)
Expand Down Expand Up @@ -68,7 +68,8 @@ def notify(state)
subject(:operation) { OperationWithSteps.new(ctx) }

before do
allow(validator).to receive(:call) do |input:, **|
allow(validator).to receive(:call) do |state|
input = state[:input]
input.key?(:foo) ? input : Result.failure(:validation)
end

Expand Down
7 changes: 4 additions & 3 deletions spec/plugins/dry_validation/1_0_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ class SimpleOperation < Operation

private

def fetch_profile(params:,**)
wrap_if_present(repository.fetch(params))
def fetch_profile(state)
wrap_if_present(repository.fetch(state[:params]))
end

def create_model(params:, profile:,**)
def create_model(state)
params, profile = state.values_at(:params, :profile)
SimpleModel.new(*params.values, user.role, profile)
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/plugins/sequel_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def as_hash(state)
state[:my_model] = { model: state[:my_model] }
end

def send_emails(my_model:,**)
@mailer.send_emails(my_model) if @mailer
def send_emails(state)
@mailer.send_emails(state[:my_model]) if @mailer
end
end

Expand All @@ -53,8 +53,8 @@ class ChainedOperation < MyOperation
end
end

def chain_operation(input:,**)
MailerOperation.call(context, input)
def chain_operation(state)
MailerOperation.call(context, state[:input])
end
end

Expand Down Expand Up @@ -187,8 +187,8 @@ class SendEmailStepOperation < MyOperation
end
end

def send_emails(my_model:,**)
@mailer.send_emails(my_model) if @mailer
def send_emails(state)
@mailer.send_emails(state[:my_model]) if @mailer
end
end

Expand Down Expand Up @@ -310,7 +310,7 @@ class InvalidOperation < MyOperation
it "fetches an instance through 'model_class' into result key" do
expect(MyModel).to receive(:first).with(email: key).and_return(object)

expect(operation.fetch_model(input: {email: key}).value[:my_model]).to eq(object)
expect(operation.fetch_model({input: {email: key}}).value[:my_model]).to eq(object)
end

context "when proving and external repository through 'from:'" do
Expand Down Expand Up @@ -364,7 +364,7 @@ class InvalidOperation < MyOperation
it 'returns an error when no instance is found', :aggregate_failures do
expect(MyModel).to receive(:first).with(email: key).and_return(nil)

result = operation.fetch_model(input: {email: key})
result = operation.fetch_model({input: {email: key}})

expect(result).to be_an(Result::Failure)
expect(result.error).to be_an(Pathway::Error)
Expand All @@ -375,7 +375,7 @@ class InvalidOperation < MyOperation
it 'returns an error without hitting the database when search key is nil', :aggregate_failures do
expect(MyModel).to_not receive(:first)

result = operation.fetch_model(input: {email: nil})
result = operation.fetch_model({input: {email: nil}})

expect(result).to be_an(Result::Failure)
expect(result.error).to be_an(Pathway::Error)
Expand Down

0 comments on commit 1f4b5be

Please sign in to comment.