Sha256: 94967975b77d329ebba16e68dc8966772f626f0d9ef059dc7144e90496f14c4d

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

module ChartMogul
  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::LineItems::Subscription
      when 'one_time' then ChartMogul::LineItems::OneTime
      end
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chartmogul-ruby-1.1.0 lib/chartmogul/invoice.rb
chartmogul-ruby-1.0.2 lib/chartmogul/invoice.rb
chartmogul-ruby-1.0.1 lib/chartmogul/invoice.rb
chartmogul-ruby-1.0.0 lib/chartmogul/invoice.rb