class Simplepay::Support::Interval
Constants
- ALLOWED_INTERVALS
If set, limits the interval set to a value within this array
- ALLOWED_QUANTITY_RANGE
If set, limits the quantity to a value within this range
- DEFAULT_INTERVAL
Sets the default interval value for new instances
- DEFAULT_QUANTITY
Sets the default quantity value for new instances
Attributes
The interval, or “period”, of time
The numeric number of intervals
Public Class Methods
# File lib/simplepay/support/interval.rb, line 29 def allowed_intervals const_get(:ALLOWED_INTERVALS) end
# File lib/simplepay/support/interval.rb, line 33 def allowed_quantity_range const_get(:ALLOWED_QUANTITY_RANGE) end
# File lib/simplepay/support/interval.rb, line 41 def default_interval const_get(:DEFAULT_INTERVAL) end
# File lib/simplepay/support/interval.rb, line 37 def default_quantity const_get(:DEFAULT_QUANTITY) end
Creates an instance of the Interval
. This can be called in one of several ways:
- no arguments
-
Creates a new interval instance with default values.
- one argument, string
-
Creates a new interval by parsing the given string to set both the quantity and interval. Must be formatted as: “3 day” (quantity, space, interval)
- one argument, hash
-
Creates a new interval and populates it with the given :quantity and :interval. Uses defaults if not given.
- two arguments
-
Creates a new interval with the first argument as the quantity, second argument as the interval.
Examples¶ ↑
All of these are equivalent:
Interval.new("3 day") Interval.new({:quantity => 3, :interval => 'day'}) Interval.new(3, 'day')
# File lib/simplepay/support/interval.rb, line 65 def initialize(*args) parse_arguments(*args) end
Public Instance Methods
Set the interval.
# File lib/simplepay/support/interval.rb, line 72 def interval=(i) raise(ArgumentError, "Interval '#{i}' should be one of: #{allowed_intervals.join(', ')}") if i && allowed_intervals && !allowed_intervals.include?(i) @interval = i end
Set the quantity.
# File lib/simplepay/support/interval.rb, line 80 def quantity=(q) raise(ArgumentError, "Quantity '#{q}' should be in #{allowed_quantity_range}") if q && allowed_quantity_range && !allowed_quantity_range.include?(q) @quantity = q end
Converts the interval into an Amazon-ready string.
Interval.new(3, 'day').to_s # => "3 day"
# File lib/simplepay/support/interval.rb, line 90 def to_s "#{quantity} #{interval}" if interval end
Private Instance Methods
# File lib/simplepay/support/interval.rb, line 114 def parse_arguments(*args) case args.size when 0 self.quantity = self.default_quantity self.interval = self.default_interval when 1 case args.first when String parse_values_from(args.first) when Hash self.quantity = args.first[:quantity] || self.default_quantity self.interval = args.first[:interval] || self.default_interval end else self.quantity = args.first self.interval = args[1] end end
# File lib/simplepay/support/interval.rb, line 133 def parse_values_from(s) if s =~ /\A([\d]+)\s+([\w]+)\Z/ self.quantity = $1.to_i self.interval = $2 else raise ArgumentError, "Unrecognzied initialization string given: #{s.inspect}" end end