Class: Financial::Parcels

Inherits:
Cost
  • Object
show all
Defined in:
lib/financial/parcels.rb

Instance Attribute Summary collapse

Attributes inherited from Cost

#date, #method_name

Instance Method Summary collapse

Methods inherited from Cost

#+, #==, #format_value, #in_date, #is_a_received_deposit?

Constructor Details

#initialize(number_of_parcels) ⇒ Parcels

Returns a new instance of Parcels.



6
7
8
9
10
11
12
# File 'lib/financial/parcels.rb', line 6

def initialize(number_of_parcels)
  @name = Financial.locale.name_of_the_parcel
  @number = number_of_parcels
  @day_of_the_parcels = 1
  @value_of_the_parcels = 0
  @beginning_month = Date.today.month
end

Instance Attribute Details

#beginning_monthObject (readonly)

Returns the value of attribute beginning_month.



4
5
6
# File 'lib/financial/parcels.rb', line 4

def beginning_month
  @beginning_month
end

#day_of_the_parcelsObject (readonly)

Returns the value of attribute day_of_the_parcels.



4
5
6
# File 'lib/financial/parcels.rb', line 4

def day_of_the_parcels
  @day_of_the_parcels
end

#month_numberObject (readonly)

Returns the value of attribute month_number.



4
5
6
# File 'lib/financial/parcels.rb', line 4

def month_number
  @month_number
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/financial/parcels.rb', line 3

def name
  @name
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/financial/parcels.rb', line 3

def number
  @number
end

#value_of_the_parcelsObject (readonly)

Returns the value of attribute value_of_the_parcels.



4
5
6
# File 'lib/financial/parcels.rb', line 4

def value_of_the_parcels
  @value_of_the_parcels
end

Instance Method Details

#beginning(month) ⇒ Object



24
25
26
27
# File 'lib/financial/parcels.rb', line 24

def beginning(month)
  @beginning_month = Financial.locale.month_for(month)
  self
end

#choose_month_for(parcel_number) ⇒ Object

TODO: Change this procedural code :\



46
47
48
49
50
51
52
53
54
# File 'lib/financial/parcels.rb', line 46

def choose_month_for(parcel_number)
  months = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  index = @beginning_month - 1 + parcel_number
  if index >= 12
    months[index - 12]
  else
    months[index]
  end
end

#choose_year_for(options) ⇒ Object

TODO: Change this procedural code :\



58
59
60
61
62
63
64
65
66
67
# File 'lib/financial/parcels.rb', line 58

def choose_year_for(options)
  parcel_number = options[:parcel_number]
  @current_year = current_date.year
  @month_number = @beginning_month + parcel_number
  if @month_number >= 12
    find_year_for_parcel
  else
    @current_year
  end
end

#day_of_the_parcel_is_in_this_month?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/financial/parcels.rb', line 40

def day_of_the_parcel_is_in_this_month?
  @day_of_the_parcels >= @today.day
end

#every_day(day) ⇒ Object



19
20
21
22
# File 'lib/financial/parcels.rb', line 19

def every_day(day)
  @day_of_the_parcels = day
  self
end

#of(value) ⇒ Object



14
15
16
17
# File 'lib/financial/parcels.rb', line 14

def of(value)
  @value_of_the_parcels = value
  self
end

#to_costObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/financial/parcels.rb', line 29

def to_cost
  @today = Date.today
  number.times.collect do |parcel_number|
    day = @day_of_the_parcels
    month = choose_month_for(parcel_number)
    year  = choose_year_for(:parcel_number => parcel_number)
    date = Financial.locale.right_date_format(day, month, year)
    Cost.new(@name, @value_of_the_parcels).in_date(date)
  end
end

#valueObject



69
70
71
# File 'lib/financial/parcels.rb', line 69

def value
  @value_of_the_parcels * @number
end