Sha256: 5a1b65ab2892e5aab1dbee36ab13980918e940f4a49c35e46e957915202eba62

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module StripeLocal
  class LineItem < ActiveRecord::Base
    include ObjectAdapter

    self.primary_key = :id

    belongs_to  :invoice, inverse_of: :lines

    belongs_to  :plan, inverse_of: :line_items

    time_writer :period_start, :period_end

    def metadata= so
      MultiJson.dump so.to_hash
    end

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

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

      def normalize attrs
        attrs.each_with_object({}) do |(k,v),h|
          key = case k.to_sym
          when :invoice  then :invoice_id
          when :plan     then h[:plan_id] = v.id and next
          when :metadata then h[:metadata] = MultiJson.dump( v.to_hash ) and next
          when :type     then h[:subscription] = (v == "subscription" ? true : false) and next
          when :period   then h[:period_start] = Time.at(v.start) and h[:period_end] = Time.at(v.end) and next
          when ->(x){attribute_method? x} then k.to_sym
          else next
          end
          h[key] = v
        end
      end
    end

  #=!=#>>>
  # string   :id
  # string   :invoice_id
  # boolean  :subscription
  # integer  :amount
  # string   :currency
  # boolean  :proration
  # datetime :period_start
  # datetime :period_end
  # integer  :quantity
  # string   :plan_id
  # string   :description
  # 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/line_item.rb