Sha256: 8fdea82efc9d9561d28280f175a66826b91bfdf2403ab8c0b467099a0fec3bdb

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'forwardable'

module Itsf
  module Services
    module V2
      module Service
        class Base
          include ActiveModel::Validations
          extend Forwardable

          def_delegator :@instrumenter, :instrument
          attr_accessor :instrumenter
          attr_reader :response

          def self.i18n_scope
            'activerecord'
          end

          def self.call(attributes = {}, *args)
            new(attributes, *args).send(:do_work)
          end

          def initialize(attributes = {}, *args)
            options = args.extract_options!
            options.reverse_merge!(instrumenter: ActiveSupport::Notifications)

            @errors = ActiveModel::Errors.new(self)

            initialize_instrumenter(options[:instrumenter])
            instrument('initialize.export_to_sap.payment.dzb') do
              initialize_attributes if respond_to?(:initialize_attributes)
              send_attributes(attributes)
              initialize_response
            end
          end

          private

          def send_attributes(attributes)
            attributes.each do |name, value|
              send("#{name}=", value)
            end
          end

          def initialize_instrumenter(instrumenter)
            @instrumenter = instrumenter
          end

          def initialize_response
            if self.class.const_defined?('Response')
              @response = "#{self.class}::Response".constantize.new
            else
              @response = Services::V2::Response::Base.new
            end
          end

          def respond
            response
          end

          def response
            set_errors_on_response
            @response
          end

          def set_errors_on_response
            @response.send(:'errors=', @errors)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itsf_services-0.0.2 lib/itsf/services/v2/service/base.rb