Sha256: 66175c75e55eb58f21b2ef02ff3abfd56ce0286f7b4fea1ed839ecf3e8185bf8

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module ChartMogul
  module Import
    class Invoice < ChartMogul::Object
      readonly_attr :uuid

      writeable_attr :date, type: :time
      writeable_attr :currency
      writeable_attr :line_items, default: []
      writeable_attr :transactions, default: []
      writeable_attr :external_id
      writeable_attr :due_date, type: :time

      def serialize_line_items
        line_items.map(&:serialize_for_write)
      end

      def serialize_transactions
        transactions.map(&:serialize_for_write)
      end

      private

      def set_line_items(line_items_attributes)
        @line_items = line_items_attributes.map.with_index do |line_item_attributes, index|
          existing_line_item = line_items[index]

          if existing_line_item
            existing_line_item.assign_all_attributes(line_item_attributes)
          else
            line_item_class(line_item_attributes[:type])
              .new_from_json(line_item_attributes.merge(invoice_uuid: uuid))
          end
        end
      end

      def set_transactions(transactions_attributes)
        @transactions = transactions_attributes.map.with_index do |transaction_attributes, index|
          existing_transaction = transactions[index]

          if existing_transaction
            existing_transaction.assign_all_attributes(transaction_attributes)
          else
            transaction_class(transaction_attributes[:type])
              .new_from_json(transaction_attributes.merge(invoice_uuid: uuid))
          end
        end
      end

      def line_item_class(type)
        case type
        when 'subscription' then ChartMogul::Import::LineItems::Subscription
        when 'one_time' then ChartMogul::Import::LineItems::OneTime
        end
      end

      def transaction_class(type)
        case type
        when 'payment' then ChartMogul::Import::Transactions::Payment
        when 'refund' then ChartMogul::Import::Transactions::Refund
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chartmogul-ruby-0.1.4 lib/chartmogul/import/invoice.rb
chartmogul-ruby-0.1.3 lib/chartmogul/import/invoice.rb
chartmogul-ruby-0.1.2 lib/chartmogul/import/invoice.rb
chartmogul-ruby-0.1.1 lib/chartmogul/import/invoice.rb