Sha256: 55ed84e92d6c7d63f5171df525e5fa02bbe7d6f553ee050ee18eba7099481719

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module StripeLocal
  class Charge < ActiveRecord::Base
    include ObjectAdapter

    belongs_to :card, inverse_of: :charges
    belongs_to :customer, inverse_of: :charges
    has_one :transaction, as: :source
    has_one :invoice, inverse_of: :charge

    self.primary_key = :id

    time_writer :created

    def metadata= so
      MultiJson.dump so.to_hash
    end

    def metadata
      MultiJson.load read_attribute( :metadata ), symbolize_keys: true
    end

    class<<self
      def create object
        super normalize( object )
      end

      def normalize attrs
        attrs.each_with_object({}) do |(k,v),h|
          key = case k.to_sym
          when :customer            then :customer_id
          when :invoice             then :invoice_id
          when :balance_transaction then :transaction_id
          when :card                then h[:card_id] = v.id       and next
          when :created             then h[:created] = Time.at(v) and next
          when :metadata            then h[:metadata] = MultiJson.dump( v.to_hash ) and next
          when ->(x){attribute_method? x} then k.to_sym
          else next
          end
          h[key] = v
        end
      end
    end


  #=!=#>>>
  # string   :id
  # string   :card_id
  # string   :customer_id
  # string   :invoice_id
  # string   :transaction_id
  # integer  :amount
  # boolean  :captured
  # boolean  :refunded
  # boolean  :paid
  # datetime :created
  # string   :currency
  # integer  :amount_refunded
  # string   :description
  # string   :failure_code
  # string   :failure_message
  # text     :metadata
  #=ยก=#>>>
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stripe_local-0.2.1 app/models/stripe_local/charge.rb