Module: Saaz::StripeObject

Extended by:
ActiveSupport::Concern
Included in:
Customer, Subscription
Defined in:
lib/saaz/stripe_object.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/saaz/stripe_object.rb', line 52

def method_missing(method_name, *args, &block)
  begin
    # Try the rails magic first
    super
  rescue NoMethodError => e
    # See if the wrapped stripe object defines this
    to_stripe
    if methods.include? method_name
      send(method_name, *args, &block)
    else
      fail e
    end
  end
end

Instance Method Details

#delegate_all_to_stripe(stripe_obj) ⇒ Object



45
46
47
48
49
50
# File 'lib/saaz/stripe_object.rb', line 45

def delegate_all_to_stripe(stripe_obj)
  stripe_obj.keys.each do |attribute|
    delegate_getter(attribute)
    delegate_setter(attribute)
  end
end

#delegate_getter(attribute) ⇒ Object



28
29
30
31
32
33
# File 'lib/saaz/stripe_object.rb', line 28

def delegate_getter(attribute)
  return if self.respond_to?(attribute)
  delegate_to_stripe(attribute) do
    to_stripe.send(attribute)
  end
end

#delegate_setter(attribute) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/saaz/stripe_object.rb', line 35

def delegate_setter(attribute)
  setter_name = (attribute.to_s + '=').to_sym
  return if self.respond_to?(setter_name)

  delegate_to_stripe(setter_name) do |v|
    self.stripe_dirty = true
    to_stripe.send(setter_name, v)
  end
end

#delegate_to_stripe(name, &block) ⇒ Object



22
23
24
25
26
# File 'lib/saaz/stripe_object.rb', line 22

def delegate_to_stripe(name, &block)
  (class << self; self; end).class_eval do
    define_method name, &block
  end
end

#stripe_dirtyObject



9
10
11
# File 'lib/saaz/stripe_object.rb', line 9

def stripe_dirty
  @stripe_dirty || false
end

#update_stripeObject



13
14
15
16
17
18
19
20
# File 'lib/saaz/stripe_object.rb', line 13

def update_stripe
  begin
    to_stripe.save
    self.stripe_dirty = false
  rescue Stripe::StripeError => e
    self.errors[:base] << e.message
  end
end