Sha256: f9515043c7ad1ce05ac176ee7c0a4b1d731bc5a5a8a4e6b052d94dd66ba38856

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module BraintreeRails
  class Customer < SimpleDelegator
    Attributes = [:id, :first_name, :last_name, :email, :company, :website, :phone, :fax, :created_at, :updated_at].freeze
    include Model

    validates :id, :format => {:with => /^[-_a-z0-9]*$/i}, :length => {:maximum => 36}, :exclusion => {:in => %w(all new)}
    validates :first_name, :last_name, :company, :website, :phone, :fax, :length => {:maximum => 255}

    attr_reader :addresses, :credit_cards
    
    def initialize(customer = {})
      customer = ensure_customer(customer)
      assign_attributes(extract_values(customer))
      @addresses = Addresses.new(self, customer.try(:addresses))
      @credit_cards = CreditCards.new(self, customer.try(:credit_cards))
      super
    end

    def full_name
      "#{first_name} #{last_name}".strip
    end

    def transactions
      new_record? ? [] : @transactions ||= Transactions.new(self)
    end

    protected
    def ensure_customer(customer)
      case customer
      when String
        @persisted = true
        Braintree::Customer.find(customer)
      when Braintree::Customer
        @persisted = true
        customer
      when Hash
        @persisted = false
        OpenStruct.new(customer.reverse_merge(:addresses => [], :credit_cards => []))
      else
        @persisted = customer.respond_to?(:persisted?) ? customer.persisted? : false
        customer
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
braintree-rails-0.2.0 lib/braintree_rails/customer.rb