Class: Zillion::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/zillion/plan.rb

Direct Known Subclasses

FixedPlan, FreePlan, TieredPlan

Defined Under Namespace

Classes: InvalidCountError, InvalidFeeError, MissingCountError, MultipleTiersFoundError, OverLimitsError, PlanArgumentError, TierError, TierNotFoundError

Constant Summary collapse

TYPES =
{
  'free'          => 'FreePlan',
  'fixed'         => 'FixedPlan',
  'custom'        => 'CustomPlan',
  'tiered'        => 'TieredPlan',
  'metered'       => 'MeteredPlan',
  'fixed-metered' => 'FixedMeteredPlan'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec = {}) ⇒ Plan

Returns a new instance of Plan.



36
37
38
# File 'lib/zillion/plan.rb', line 36

def initialize(spec = {})
  @spec = spec
end

Class Method Details

.factory(spec) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zillion/plan.rb', line 24

def self.factory(spec)
  unless type = spec[:type]
    fee = spec[:monthly_fee].to_i
    type = fee == 0 ? 'free' : fee > 0 ? 'fixed' : 'unknown'

    type = 'fixed-metered' if type == 'fixed' && spec[:tiers]
  end

  klass = TYPES[type.to_s] or raise "Invalid type: #{type}"
  Kernel.const_get('Zillion::' + klass).new(spec)
end

Instance Method Details

#<=>(other_plan) ⇒ Object



56
57
58
59
# File 'lib/zillion/plan.rb', line 56

def <=>(other_plan)
  # other_plan = Plan.factory(other_plan) unless other_plan.is_a?(Plan)
  self.monthly_fee <=> other_plan.monthly_fee
end

#amounts_for(counts, custom_monthly_fee: nil, fallback: false) ⇒ Object



61
62
63
# File 'lib/zillion/plan.rb', line 61

def amounts_for(counts, custom_monthly_fee: nil, fallback: false)
  { monthly_fee: custom_monthly_fee.nil? ? monthly_fee : custom_monthly_fee }
end

#available_for?(counts) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/zillion/plan.rb', line 70

def available_for?(counts)
  limits.each do |key, limit|
    val = counts[key] or raise MissingCountError.new("Count required for #{key}")
    raise InvalidCountError.new("Negative count for #{key}: #{val}") if val.to_i < 0

    return false if limit.to_i < val.to_i
  end
  true
end

#featuresObject



80
81
82
# File 'lib/zillion/plan.rb', line 80

def features
  spec[:features] || []
end

#has?(feature) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/zillion/plan.rb', line 48

def has?(feature)
  features.include?(feature.to_s) # features is an array of strings
end

#keyObject



40
41
42
# File 'lib/zillion/plan.rb', line 40

def key
  (spec[:key] || name).to_sym
end

#limit_for(what) ⇒ Object



52
53
54
# File 'lib/zillion/plan.rb', line 52

def limit_for(what)
  limits[what.to_sym]
end

#limitsObject



84
85
86
# File 'lib/zillion/plan.rb', line 84

def limits
  spec[:limits] || {}
end

#monthly_fee_for(counts, custom_monthly_fee: nil, fallback: false) ⇒ Object



65
66
67
68
# File 'lib/zillion/plan.rb', line 65

def monthly_fee_for(counts, custom_monthly_fee: nil, fallback: false)
  ensure_available!(counts) unless fallback
  sum(amounts_for(counts, custom_monthly_fee: custom_monthly_fee, fallback: fallback))
end

#nameObject



44
45
46
# File 'lib/zillion/plan.rb', line 44

def name
  (spec[:name] || spec[:key] || self.class.name.sub('Zillion::', '')).to_s
end