Class: Zillion::FixedMeteredPlan

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

Constant Summary

Constants inherited from Plan

Plan::TYPES

Instance Method Summary collapse

Methods inherited from TieredPlan

#matching_tier_for

Methods inherited from Plan

#<=>, factory, #features, #has?, #initialize, #key, #limit_for, #limits, #monthly_fee_for, #name

Constructor Details

This class inherits a constructor from Zillion::Plan

Instance Method Details

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



234
235
236
237
238
# File 'lib/zillion/plan.rb', line 234

def amounts_for(counts, custom_monthly_fee: nil, fallback: false)
  obj = super
  obj[:base_fee] = custom_monthly_fee.nil? ? base_fee : custom_monthly_fee
  obj
end

#available_for?(counts) ⇒ Boolean

a fixed tiered plan is available either if the count is below the fixed limit, or, if above that limit but a tier exists for that type

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/zillion/plan.rb', line 242

def available_for?(counts)
  limits.each do |key, limit|
    val = counts[key] or raise MissingCountError.new("Count required for #{key}")

    # above limit, so see if a matching tier is found
    if val.to_i <= limit.to_i # below limit
      next
    elsif tiers = tier_groups[key]
      begin
        find_tier_for(tiers, val.to_i)
        next # tier found, so next one please
      rescue TierError => e
       # no tier found
      end
    end

    return false
  end

  true
end

#base_feeObject



230
231
232
# File 'lib/zillion/plan.rb', line 230

def base_fee
  spec[:monthly_fee] ? spec[:monthly_fee].to_f : 0
end

#matching_tiers_for(counts, fallback: false) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/zillion/plan.rb', line 281

def matching_tiers_for(counts, fallback: false)
  tier_groups.map do |key, tiers|
    units = counts[key] or raise MissingCountError.new("Unit count required for #{key}")
    next if limits[key] and limits[key] >= units.to_i

    begin
      find_tier_for(tiers, units.to_i) + [key]
    rescue TierNotFoundError
      raise unless fallback # to_highest_tier
      last_existing_tier_for(key)
    end
  end.compact
end

#tier_cost_keyObject



277
278
279
# File 'lib/zillion/plan.rb', line 277

def tier_cost_key
  :monthly_unit_cost
end

#units_for(type, counts) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/zillion/plan.rb', line 264

def units_for(type, counts)
  units = counts[type]

  # if a hard limit exists for that type, it means it's included
  # so we should reduce the count of actual units
  units -= limits[type] if limits[type]

  # but ensure we never go below zero
  units = 0 if units < 0

  units
end