Class: Acme::Smileage::Discography::Sales

Inherits:
Object
  • Object
show all
Defined in:
lib/acme/smileage/discography/sales.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first_week_sales, total_sales, peak_rank, weeks_on_chart) ⇒ Sales

Returns a new instance of Sales.



22
23
24
25
26
27
# File 'lib/acme/smileage/discography/sales.rb', line 22

def initialize(first_week_sales, total_sales, peak_rank, weeks_on_chart)
  @first_week_sales = first_week_sales
  @total_sales = total_sales
  @peak_rank = peak_rank
  @weeks_on_chart = weeks_on_chart
end

Instance Attribute Details

#first_week_salesObject (readonly)

Returns the value of attribute first_week_sales.



7
8
9
# File 'lib/acme/smileage/discography/sales.rb', line 7

def first_week_sales
  @first_week_sales
end

#peak_rankObject (readonly)

Returns the value of attribute peak_rank.



7
8
9
# File 'lib/acme/smileage/discography/sales.rb', line 7

def peak_rank
  @peak_rank
end

#total_salesObject (readonly)

Returns the value of attribute total_sales.



7
8
9
# File 'lib/acme/smileage/discography/sales.rb', line 7

def total_sales
  @total_sales
end

#weeks_on_chartObject (readonly)

Returns the value of attribute weeks_on_chart.



7
8
9
# File 'lib/acme/smileage/discography/sales.rb', line 7

def weeks_on_chart
  @weeks_on_chart
end

Class Method Details

.records(*records) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/acme/smileage/discography/sales.rb', line 9

def self.records(*records)
  if records.empty?
    return self.new(0, 0, 0, 0)
  end

  first_week_sales = records[0][:sales]
  total_sales = records.inject(0){|acc,e| acc + e[:sales] }
  peak_rank = records.map{|e| e[:rank] }.min
  weeks_on_chart = records.length

  self.new(first_week_sales, total_sales, peak_rank, weeks_on_chart)
end

Instance Method Details

#first_week_to_total_ratioObject



29
30
31
# File 'lib/acme/smileage/discography/sales.rb', line 29

def first_week_to_total_ratio
  self.total_sales.zero? ? 0 : self.first_week_sales.to_f / self.total_sales.to_f
end

#to_aObject



37
38
39
# File 'lib/acme/smileage/discography/sales.rb', line 37

def to_a
  [self.first_week_sales, self.total_sales, self.peak_rank, self.weeks_on_chart]
end

#total_to_first_week_ratioObject



33
34
35
# File 'lib/acme/smileage/discography/sales.rb', line 33

def total_to_first_week_ratio
  self.first_week_sales.zero? ? 0 : self.total_sales.to_f / self.first_week_sales.to_f
end