Skip to content

Instantly share code, notes, and snippets.

@adityagodbole
Created March 22, 2018 10:29
Show Gist options
  • Save adityagodbole/0166c33253caa0adcf62d40ba2d74970 to your computer and use it in GitHub Desktop.
Save adityagodbole/0166c33253caa0adcf62d40ba2d74970 to your computer and use it in GitHub Desktop.
# dispatch method. can be put in base controller
def op(klass)
logger.info "Params: ", params.to_h
obj = klass.new(params.to_h).register(:keystore, @keystore).register(:configstore, @configstore)
resp = obj.process!(self, @context)
rescue => e
logger.error e.message
raise e
end
class Operation < Types::SafeStruct
include Dry::Container::Mixin
def process!(app) #app is a rack/controller object and is often used to call url_for
begin
res = validate
raise Error400 unless res # the app server specific 400 exception class
pre_process
process(app) || {}
end
def process(app); end
def pre_process; end
def validate; true; end
end
class SampleOperation < Operation
attribute :bank_id, Types::Optional::Int.default(0)
attribute :telco_id, Types::Strict::Int.default(0)
attribute :ambiguous, Types::Form::Bool.optional # accepts nil
def validate
end
def process
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment