@@ -44,15 +44,16 @@ class ShelveBookOperation < ::TypedOperation::Base
44
44
# Or if you prefer:
45
45
# `param :description, String`
46
46
47
- named_param :author_id , Integer , & :to_i
48
- named_param :isbn , String
47
+ # `param` creates named parameters by default
48
+ param :author_id , Integer , & :to_i
49
+ param :isbn , String
49
50
50
51
# Optional parameters are specified by wrapping the type constraint in the `optional` method, or using the `optional:` option
51
- named_param :shelf_code , optional(Integer )
52
+ param :shelf_code , optional(Integer )
52
53
# Or if you prefer:
53
54
# `named_param :shelf_code, Integer, optional: true`
54
55
55
- named_param :category , String , default: " unknown" .freeze
56
+ param :category , String , default: " unknown" .freeze
56
57
57
58
# optional hook called when the operation is initialized, and after the parameters have been set
58
59
def prepare
@@ -521,7 +522,8 @@ This is an example of a `ApplicationOperation` in a Rails app that uses `Dry::Mo
521
522
522
523
class ApplicationOperation < ::TypedOperation ::Base
523
524
# We choose to use dry-monads for our operations, so include the required modules
524
- include Dry ::Monads [:result , :do ]
525
+ include Dry ::Monads [:result ]
526
+ include Dry ::Monads ::Do .for(:perform )
525
527
526
528
class << self
527
529
# Setup our own preferred names for the DSL methods
@@ -711,55 +713,21 @@ end
711
713
712
714
Note you are provided the ActionPolicy error object, but you cannot stop the error from being re-raised.
713
715
714
- ### Using with ` literal ` monads
715
-
716
- You can use the ` literal ` gem to provide a ` Result ` type for your operations.
717
-
718
- ``` ruby
719
- class MyOperation < ::TypedOperation ::Base
720
- param :account_name , String
721
- param :owner , String
722
-
723
- def perform
724
- create_account.bind do |account |
725
- associate_owner(account).map { account }
726
- end
727
- end
728
-
729
- private
730
-
731
- def create_account
732
- # ...
733
- # Literal::Failure.new(:cant_create_account)
734
- Literal ::Success .new (account_name)
735
- end
736
-
737
- def associate_owner (account )
738
- # ...
739
- Literal ::Failure .new (:cant_associate_owner )
740
- # Literal::Success.new("ok")
741
- end
742
- end
743
-
744
- MyOperation .new (account_name: " foo" , owner: " bar" ).call
745
- # => Literal::Failure(:cant_associate_owner)
746
- ```
747
-
748
716
### Using with ` Dry::Monads `
749
717
750
718
As per the example in [ ` Dry::Monads ` documentation] ( https://dry-rb.org/gems/dry-monads/1.0/do-notation/ )
751
719
752
720
``` ruby
753
721
class MyOperation < ::TypedOperation ::Base
754
722
include Dry ::Monads [:result ]
755
- include Dry ::Monads ::Do .for(:call )
723
+ include Dry ::Monads ::Do .for(:perform , :create_account )
756
724
757
725
param :account_name , String
758
726
param :owner , ::Owner
759
727
760
728
def perform
761
729
account = yield create_account(account_name)
762
- yield associate_owner (account, owner)
730
+ yield AnotherOperation .call (account, owner)
763
731
764
732
Success (account)
765
733
end
0 commit comments