Module: BraintreeRails::Model::InstanceMethods

Defined in:
lib/braintree_rails/model.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
# File 'lib/braintree_rails/model.rb', line 52

def ==(other)
  return false unless other.is_a?(self.class) || other.is_a?(self.class.braintree_model_class)
  id == other.id
end

#add_errors(validation_errors) ⇒ Object



42
43
44
45
46
# File 'lib/braintree_rails/model.rb', line 42

def add_errors(validation_errors)
  Array(extract_errors(validation_errors)).each do |error|
    self.errors.add(error.attribute.to_sym, error.code.to_sym, message:BraintreeRails::ApiError.new(error.message, error.code))
  end
end

#ensure_model(model) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/braintree_rails/model.rb', line 23

def ensure_model(model)
  model = case model
  when String
    self.persisted = true
    self.class.braintree_model_class.find(model)
  when self.class.braintree_model_class
    self.persisted = model.id.present?
    model
  when Hash
    self.persisted = false
    OpenStruct.new(model)
  else
    self.persisted = model.respond_to?(:persisted?) ? model.persisted? : false
    model
  end
  assign_attributes(extract_values(model))
  model
end

#extract_errors(validation_errors) ⇒ Object



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

def extract_errors(validation_errors)
  validation_errors.for(self.class.braintree_model_name.to_sym)
end

#initialize(model = {}) ⇒ Object



19
20
21
# File 'lib/braintree_rails/model.rb', line 19

def initialize(model = {})
  init(model)
end