Sha256: 66ebb523f8fb84cfd559787601bef7f18f8cff99c383ba9f0162de5e971bfe8b

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require_relative '../operation'
require_relative 'resource'

module LedgerSync
  module Domains
    class Operation
      class Update < Resource
        class Contract < LedgerSync::Ledgers::Contract
          params do
            required(:id).filled(:integer)
            required(:query).value(:hash)
          end
        end

        private

        def operate
          return failure('Resource not found') unless resource

          if resource.update(params.except(:id))
            success
          else
            failure(
              'Please review the problems below:',
              data: serialize(resource: resource)
            )
          end
        end

        def resource
          @resource ||= resource_class.where(params[:query]).find_by(id: params[:id])
        end

        def success
          super(resource)
        end

        def failure(message, data: nil)
          super(
              LedgerSync::Error::OperationError.new(
                operation: self,
                message: message,
                response: data
              )
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-domains-1.0.2 lib/ledger_sync/domains/operation/update.rb