Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The endpoint handles custom cases and the other cases can be handled … #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/trailblazer/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Trailblazer
class Endpoint
# this is totally WIP as we need to find best practices.
# also, i want this to be easily extendable.
Matcher = Dry::Matcher.new(
DefaultOptions = {
present: Dry::Matcher::Case.new( # DISCUSS: the "present" flag needs some discussion.
match: ->(result) { result.success? && result["present"] },
resolve: ->(result) { result }),
Expand All @@ -24,21 +24,32 @@ class Endpoint
invalid: Dry::Matcher::Case.new(
match: ->(result) { result.failure? && result["result.contract.default"] && result["result.contract.default"].failure? },
resolve: ->(result) { result })
)
}

# `call`s the operation.
def self.call(operation_class, handlers, *args, &block)
result = operation_class.(*args)
new.(result, handlers, &block)
end

def call(result, handlers=nil, &block)
matcher.(result, &block) and return if block_given? # evaluate user blocks first.
matcher.(result, &handlers) # then, generic Rails handlers in controller context.
def call(result, handlers=nil, **args, &block)
options = default_options
options = options.select{|k,v| args[:outcomes].include? k } if args[:outcomes]
options = options.merge(args[:additional_outcomes]) if args[:additional_outcomes]
if block_given? # evaluate user blocks first.
matcher(options).(result, &block)
return
end

matcher(options).(result, &handlers) # then, generic Rails handlers in controller context.
end

def default_options
DefaultOptions
end

def matcher
Matcher
def matcher(options)
Dry::Matcher.new(options)
end

module Controller
Expand Down