Class: BraintreeRails::Address

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/braintree_rails/address.rb

Direct Known Subclasses

BillingAddress, ShippingAddress

Constant Summary collapse

CountryNames =
{
  :country_name => 0,
  :country_code_alpha2 => 1,
  :country_code_alpha3 => 2,
  :country_code_numeric => 3
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Class Method Details

.delete(customer_id, id) ⇒ Object



34
35
36
# File 'lib/braintree_rails/address.rb', line 34

def self.delete(customer_id, id)
  braintree_model_class.delete(customer_id, id)
end

.find(customer_id, id) ⇒ Object



30
31
32
# File 'lib/braintree_rails/address.rb', line 30

def self.find(customer_id, id)
  new(braintree_model_class.find(customer_id, id))
end

.sync_country_name_attributes(country_attribute, index) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/braintree_rails/address.rb', line 20

def self.sync_country_name_attributes(country_attribute, index)
  define_method("#{country_attribute}=") do |value|
    CountryNames.except(country_attribute).each do |other_attribute, other_index|
      other_value = Braintree::Address::CountryNames.find{|country_name| country_name[index] == value}.try(:[], other_index)
      instance_variable_set("@#{other_attribute}", other_value)
    end
    instance_variable_set("@#{country_attribute}", value)
  end
end

Instance Method Details

#destroyObject



52
53
54
55
56
57
58
59
60
# File 'lib/braintree_rails/address.rb', line 52

def destroy
  if persisted?
    run_callbacks :destroy do
      self.class.delete(customer_id, id)
    end
  end
  self.persisted = false unless frozen?
  freeze
end

#ensure_model(model) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/braintree_rails/address.rb', line 38

def ensure_model(model)
  if Braintree::Transaction::AddressDetails === model
    assign_attributes(extract_values(model))
    self.persisted = model.id.present?
    model
  else
    super
  end
end

#full_nameObject



48
49
50
# File 'lib/braintree_rails/address.rb', line 48

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