Class: Recurly::Subscription::AddOns

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/subscription/add_ons.rb

Instance Method Summary collapse

Constructor Details

#initialize(subscription, add_ons = []) ⇒ AddOns

Returns a new instance of AddOns.

Parameters:

  • subscription (Subscription)
  • add_ons (Array, nil) (defaults to: [])


10
11
12
13
# File 'lib/recurly/subscription/add_ons.rb', line 10

def initialize subscription, add_ons = []
  @subscription, @add_ons = subscription, []
  add_ons and add_ons.each { |a| self << a }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



65
66
67
68
69
70
71
# File 'lib/recurly/subscription/add_ons.rb', line 65

def method_missing name, *args, &block
  if @add_ons.respond_to? name
    return @add_ons.send name, *args, &block
  end

  super
end

Instance Method Details

#<<(add_on) ⇒ self

Examples:

pp subscription.add_ons << '1YEARWAR' << '1YEARWAR' << :BONUS
[
  {:add_on_code => "1YEARWAR", :quantity => 2},
  {:add_on_code => "BONUS"}
]

Parameters:

  • add_on (AddOn, String, Symbol, Hash)

    A Plan add-on, add_on_code, or hash with optional :quantity and :unit_amount_in_cents keys.

Returns:

  • (self)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/recurly/subscription/add_ons.rb', line 25

def << add_on
  case add_on
    when AddOn then add_on = { :add_on_code => add_on.add_on_code }
    when String, Symbol then add_on = { :add_on_code => add_on.to_s }
  end
  add_on = Helper.hash_with_indifferent_read_access add_on

  exist = @add_ons.find { |a| a[:add_on_code] == add_on[:add_on_code] }
  if exist
    exist[:quantity] ||= 1 and exist[:quantity] += 1

    if add_on[:unit_amount_in_cents]
      exist[:unit_amount_in_cents] = add_on[:unit_amount_in_cents]
    end
  else
    @add_ons << add_on
  end

  @subscription[:subscription_add_ons] = to_a and self
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/recurly/subscription/add_ons.rb', line 59

def respond_to? method_name, include_private = false
  super || @add_ons.respond_to?(method_name, include_private)
end

#to_aObject



46
47
48
# File 'lib/recurly/subscription/add_ons.rb', line 46

def to_a
  @add_ons.dup
end

#to_xml(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/recurly/subscription/add_ons.rb', line 50

def to_xml options = {}
  builder = options[:builder] || XML.new('<subscription_add_ons/>')
  @add_ons.each do |add_on|
    node = builder.add_element 'subscription_add_on'
    add_on.each_pair { |k, v| node.add_element k.to_s, v }
  end
  builder.to_s
end