Sha256: 468068b3fd377df63b208181a007ad6685fb1b9325c473f678fbb5395de7d8bb

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require_relative 'double_entry'
require_relative 'preparation'

module Generalis
  class Transaction < ActiveRecord::Base
    module DSL
      def self.extended(klass)
        super(klass)

        klass.include(Preparation)
        klass.before_validation(:prepare, if: :new_record?)
      end

      def transaction_id(&block)
        prepare_with do
          self.transaction_id = instance_exec(&block)
        end
      end

      def description(&block)
        prepare_with do
          self.description = instance_exec(&block)
        end
      end

      def metadata(&block)
        prepare_with do
          self.metadata = instance_exec(&block)
        end
      end

      def occurred_at(&block)
        prepare_with do
          self.occurred_at = instance_exec(&block)
        end
      end

      # @return [void]
      def credit(&block)
        prepare_with do
          credit = Credit.new
          instance_exec(credit, &block)

          entries << credit
        end
      end

      # @return [void]
      def debit(&block)
        prepare_with do
          debit = Debit.new
          instance_exec(debit, &block)

          entries << debit
        end
      end

      def double_entry(&block)
        prepare_with do
          pair = DoubleEntry.new
          instance_exec(pair, &block)

          entries << pair.entries
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generalis-0.1.0 lib/generalis/transaction/dsl.rb