Class: Joyent::Cloud::Pricing::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/pricing/commit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, custom = nil, discount = nil) ⇒ Commit

Returns a new instance of Commit.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pricing/commit.rb', line 16

def initialize(hash = {}, custom = nil, discount = nil)
  @config = hash.symbolize_keys
  self.reserves = {}
  @config.each_pair do |flavor, config|
    self.reserves[flavor] = Reserve.new(flavor, config)
  end
  if custom
    Joyent::Cloud::Pricing::Configuration.instance.merge(custom)
  end
  if discount
    discount = discount.symbolize_keys
    self.discount = Joyent::Cloud::Pricing::Discount.type(discount[:type], discount[:value])
  end
end

Instance Attribute Details

#customObject

map of image names to prices



14
15
16
# File 'lib/pricing/commit.rb', line 14

def custom
  @custom
end

#discountObject

map of image names to prices



14
15
16
# File 'lib/pricing/commit.rb', line 14

def discount
  @discount
end

#reservesObject

map of image names to prices



14
15
16
# File 'lib/pricing/commit.rb', line 14

def reserves
  @reserves
end

Class Method Details

.from_yaml(filename = COMMIT_FILENAME) ⇒ Object



7
8
9
10
# File 'lib/pricing/commit.rb', line 7

def from_yaml(filename = COMMIT_FILENAME)
  hash = YAML.load(File.read(filename))
  new(hash['reserved'] || {}, hash['custom'], hash['discount'])
end

Instance Method Details

#flavor_countsObject



55
56
57
58
59
# File 'lib/pricing/commit.rb', line 55

def flavor_counts
  zone_list = {}
  reserves.keys.each{|zone| zone_list[zone] = reserves[zone].quantity}
  zone_list
end

#monthly_priceObject



35
36
37
# File 'lib/pricing/commit.rb', line 35

def monthly_price
  sum_of &->(reserve) {reserve.monthly }
end

#reserve_for(flavor) ⇒ Object



31
32
33
# File 'lib/pricing/commit.rb', line 31

def reserve_for flavor
  reserves[flavor.to_sym]
end

#total_zonesObject



43
44
45
# File 'lib/pricing/commit.rb', line 43

def total_zones
  sum_of &->(reserve) {1}
end

#upfront_priceObject



39
40
41
# File 'lib/pricing/commit.rb', line 39

def upfront_price
  sum_of &->(reserve) {reserve.prepay}
end

#yearly_priceObject



47
48
49
# File 'lib/pricing/commit.rb', line 47

def yearly_price
  upfront_price + 12 * monthly_price
end

#yearsObject



51
52
53
# File 'lib/pricing/commit.rb', line 51

def years
  reserves.empty? ? 0 : reserves.values.first.years
end