Skip to content

Commit

Permalink
Merge pull request #50 from pabloh/rename_auto_wire_options
Browse files Browse the repository at this point in the history
Rename :auto_wire_options to :auto_wire
  • Loading branch information
pabloh authored Aug 7, 2024
2 parents 9fe850e + 3905ee0 commit ea09d45
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.12.3] - 2024-08-07
### Changed
- Renamed config option `:auto_wire_options` to `:auto_wire` at `:dry_validation` plugin

## [0.12.2] - 2024-08-06
### Added
- Add `Pathway::State#unwrap` and `Pathway::State#u` to access internal state
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ end

If you are familiar with `dry-validation` you probably know it provides a way to [inject options](https://dry-rb.org/gems/dry-validation/1.4/external-dependencies/) before calling the contract.

In those scenarios, you must either set the `auto_wire_options: true` plugin argument or specify how to map options from the execution state to the contract when calling `step :validate`.
In those scenarios, you must either set the `auto_wire: true` plugin argument or specify how to map options from the execution state to the contract when calling `step :validate`.
Lets see and example for the first case:

```ruby
class CreateNugget < Pathway::Operation
plugin :dry_validation, auto_wire_options: true
plugin :dry_validation, auto_wire: true

context :user_name

Expand All @@ -438,7 +438,7 @@ class CreateNugget < Pathway::Operation
end
```

Here the defined contract needs a `:user_name` option, so we tell the operation to grab the attribute with the same name from the state by activating `:auto_wire_options`, afterwards, when the validation runs, the contract will already have the user name available.
Here the defined contract needs a `:user_name` option, so we tell the operation to grab the attribute with the same name from the state by activating `:auto_wire`, afterwards, when the validation runs, the contract will already have the user name available.

Mind you, this option is `false` by default, so be sure to set it to `true` at `Pathway::Operation` if you'd rather have it enabled for all your operations.

Expand Down
22 changes: 16 additions & 6 deletions lib/pathway/plugins/dry_validation/v0_11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module DryValidation
module V0_11
module ClassMethods
attr_reader :form_class, :form_options
attr_accessor :auto_wire_options
attr_accessor :auto_wire

alias_method :auto_wire_options, :auto_wire
alias_method :auto_wire_options=, :auto_wire=

def form(base = nil, **opts, &block)
if block_given?
Expand All @@ -32,7 +35,7 @@ def build_form(opts = {})
def inherited(subclass)
super
subclass.form_class = form_class
subclass.auto_wire_options = auto_wire_options
subclass.auto_wire = auto_wire
end

private
Expand All @@ -58,10 +61,11 @@ module InstanceMethods
extend Forwardable

delegate %i[build_form form_options auto_wire_options] => 'self.class'
alias :form :build_form
delegate %i[build_form form_options auto_wire_options auto_wire] => 'self.class'
alias_method :form, :build_form

def validate(state, with: nil)
if auto_wire_options && form_options.any?
if auto_wire && form_options.any?
with ||= form_options.zip(form_options).to_h
end
opts = Hash(with).map { |opt, key| [opt, state[key]] }.to_h
Expand All @@ -76,9 +80,15 @@ def validate_with(params, opts = {})
end
end

def self.apply(operation, auto_wire_options: false)
def self.apply(operation, auto_wire_options: (auto_wire_options_was_not_used=true; false), auto_wire: auto_wire_options)
#:nocov:
unless auto_wire_options_was_not_used
warn "[DEPRECATION] `auto_wire_options` is deprecated. Please use `auto_wire` instead"
end
#:nocov:

operation.auto_wire = auto_wire
operation.form_class = Dry::Validation::Schema::Form
operation.auto_wire_options = auto_wire_options
end
end
end
Expand Down
23 changes: 16 additions & 7 deletions lib/pathway/plugins/dry_validation/v0_12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module DryValidation
module V0_12
module ClassMethods
attr_reader :form_class, :form_options
attr_accessor :auto_wire_options
attr_accessor :auto_wire

alias_method :auto_wire_options, :auto_wire
alias_method :auto_wire_options=, :auto_wire=

def form(base = nil, **opts, &block)
if block_given?
Expand All @@ -32,7 +35,7 @@ def build_form(opts = {})
def inherited(subclass)
super
subclass.form_class = form_class
subclass.auto_wire_options = auto_wire_options
subclass.auto_wire = auto_wire
end

private
Expand All @@ -57,11 +60,11 @@ def _block_definition(base, opts, &block)
module InstanceMethods
extend Forwardable

delegate %i[build_form form_options auto_wire_options] => 'self.class'
alias :form :build_form
delegate %i[build_form form_options auto_wire_options auto_wire] => 'self.class'
alias_method :form, :build_form

def validate(state, with: nil)
if auto_wire_options && form_options.any?
if auto_wire && form_options.any?
with ||= form_options.zip(form_options).to_h
end
opts = Hash(with).map { |opt, key| [opt, state[key]] }.to_h
Expand All @@ -76,9 +79,15 @@ def validate_with(params, opts = {})
end
end

def self.apply(operation, auto_wire_options: false)
def self.apply(operation, auto_wire_options: (auto_wire_options_was_not_used=true; false), auto_wire: auto_wire_options)
#:nocov:
unless auto_wire_options_was_not_used
warn "[DEPRECATION] `auto_wire_options` is deprecated. Please use `auto_wire` instead"
end
#:nocov:

operation.auto_wire = auto_wire
operation.form_class = Dry::Validation::Schema::Params
operation.auto_wire_options = auto_wire_options
end
end
end
Expand Down
25 changes: 17 additions & 8 deletions lib/pathway/plugins/dry_validation/v1_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module DryValidation
module V1_0
module ClassMethods
attr_reader :contract_class, :contract_options
attr_accessor :auto_wire_options
attr_accessor :auto_wire

alias_method :auto_wire_options, :auto_wire
alias_method :auto_wire_options=, :auto_wire=

def contract(base = nil, &block)
if block_given?
Expand All @@ -24,7 +27,7 @@ def contract(base = nil, &block)
end

def contract_class= klass
@contract_class = klass
@contract_class = klass
@contract_options = (klass.dry_initializer.options - Dry::Validation::Contract.dry_initializer.options).map(&:target)
@builded_contract = @contract_options.empty? && klass.schema ? klass.new : nil
end
Expand All @@ -35,8 +38,8 @@ def build_contract(**opts)

def inherited(subclass)
super
subclass.auto_wire = auto_wire
subclass.contract_class = contract_class
subclass.auto_wire_options = auto_wire_options
end

private
Expand All @@ -49,11 +52,11 @@ def _base_contract
module InstanceMethods
extend Forwardable

delegate %i[build_contract contract_options auto_wire_options] => 'self.class'
alias :contract :build_contract
delegate %i[build_contract contract_options auto_wire_options auto_wire] => 'self.class'
alias_method :contract, :build_contract

def validate(state, with: nil)
if auto_wire_options && contract_options.any?
if auto_wire && contract_options.any?
with ||= contract_options.zip(contract_options).to_h
end
opts = Hash(with).map { |to, from| [to, state[from]] }.to_h
Expand All @@ -68,9 +71,15 @@ def validate_with(input, **opts)
end
end

def self.apply(operation, auto_wire_options: false)
def self.apply(operation, auto_wire_options: (auto_wire_options_was_not_used=true; false), auto_wire: auto_wire_options)
#:nocov:
unless auto_wire_options_was_not_used
warn "[DEPRECATION] `auto_wire_options` is deprecated. Please use `auto_wire` instead"
end
#:nocov:

operation.auto_wire = auto_wire
operation.contract_class = Dry::Validation::Contract
operation.auto_wire_options = auto_wire_options
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/plugins/dry_validation/1_0_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class OperationWithOpt < Operation
end

class OperationWithAutoWire < Operation
plugin :dry_validation, auto_wire_options: true
plugin :dry_validation, auto_wire: true

context :baz

Expand Down

0 comments on commit ea09d45

Please sign in to comment.