Sha256: 00a78f945a92dca2524cca85d845e7d0b3600cad1bceb539d5faf9fbc8e3932d
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
# frozen_string_literal: true module Pragma module Operation module Macro def self.Decorator(name: :instance) step = ->(input, options) { Decorator.for(input, name, options) } [step, name: "decorator.#{name}"] end module Decorator class << self def for(_input, name, options) unless validate_params(options) handle_invalid_contract(options) return false end options["result.decorator.#{name}"] = options["decorator.#{name}.class"].new( options['model'] ) validate_expansion(options, name) end private def validate_params(options) options['contract.expand'] = Dry::Validation.Schema do optional(:expand).each(:str?) end options['result.contract.expand'] = options['contract.expand'].call(options['params']) options['result.contract.expand'].errors.empty? end def handle_invalid_contract(options) options['result.response'] = Response::UnprocessableEntity.new( errors: options['result.contract.expand'].errors ).decorate_with(Pragma::Decorator::Error) end def validate_expansion(options, name) return true unless options["result.decorator.#{name}"].respond_to?(:validate_expansion) options["result.decorator.#{name}"].validate_expansion(options['params'][:expand]) true rescue Pragma::Decorator::Association::ExpansionError => e options['result.response'] = Response::BadRequest.new( entity: Pragma::Operation::Error.new( error_type: :expansion_error, error_message: e.message ) ).decorate_with(Pragma::Decorator::Error) false end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pragma-2.0.0 | lib/pragma/operation/macro/decorator.rb |