Sha256: a643ab56aee73b0268afc7d5028c0cac8c4920989fd3f22ae2e22e231714b635

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

module StripeLocal
  class Balance < ActiveRecord::Base
    include ObjectAdapter

    @@cache ||= {}
    after_create do |record|
      @@cache = { current: record.available, pending: record.pending }
    end

    class<<self
      def available
        @@cache[:current] ||= all.pluck( :available ).last || 0
      end
      alias :current :available

      def pending
        @@cache[:pending] ||= all.pluck( :pending ).last || 0
      end

      def previous_available
        @@cache[:previous] ||= all.pluck( :available )[-2] || 0
      end
      alias :previous :previous_available

      def previous_pending
        @@cache[:previous_pending] ||= all.pluck( :pending )[-2] || 0
      end

      def changed
        @@cache[:changed] ||= ( self.current - self.previous )
      end

      def event update
        if ( self.pending == update[:pending] ) && ( self.available == update[:available] )
          Balance.last.touch
        else
          Balance.create( update )
        end
      end
    end

    def to_s
      Balance.available.to_money
    end
    alias :inspect :to_s
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stripe_local-0.2.2 app/models/stripe_local/balance.rb
stripe_local-0.2.1 app/models/stripe_local/balance.rb
stripe_local-0.2.0 app/models/stripe_local/balance.rb
stripe_local-0.1.3 app/models/stripe_local/balance.rb
stripe_local-0.1.2 app/models/stripe_local/balance.rb
stripe_local-0.1.1 app/models/stripe_local/balance.rb
stripe_local-0.1.0 app/models/stripe_local/balance.rb
stripe_local-0.0.2 app/models/stripe_local/balance.rb