Class: Span::DateDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/span/date_diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_date_input, start_date_input) ⇒ DateDiff

Returns a new instance of DateDiff.



13
14
15
16
# File 'lib/span/date_diff.rb', line 13

def initialize( end_date_input , start_date_input )
  @end_date   = end_date_input.to_date
  @start_date = start_date_input.to_date
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



5
6
7
# File 'lib/span/date_diff.rb', line 5

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



5
6
7
# File 'lib/span/date_diff.rb', line 5

def start_date
  @start_date
end

Class Method Details

.compute(*args, &block) ⇒ Object



9
10
11
# File 'lib/span/date_diff.rb', line 9

def self.compute( *args , &block )
  new( *args , &block ).to_hash
end

Instance Method Details

#build_diff_hashObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/span/date_diff.rb', line 18

def build_diff_hash
  check_from = start_date

  [ :years , :months , :weeks , :days ].reduce Hash.new do | memo , part |
    check_from , num_of_part , part_singular =
      send \
        :"diff_#{ part }",
        check_from,
        part

    memo[ part_singular ] = num_of_part
    memo
  end
end

#default_diff(check_from, part, part_singular) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/span/date_diff.rb', line 33

def default_diff( check_from , part , part_singular )
  num_of_part      = 0
  part_next_method = :"next_#{ part_singular }"

  loop do
    check = check_from.send part_next_method

    break if check > end_date

    check_from = check
    num_of_part += 1
  end

  [ check_from , num_of_part , part_singular ]
end

#diff_days(*args) ⇒ Object



49
50
51
# File 'lib/span/date_diff.rb', line 49

def diff_days( *args )
  default_diff *args , :day
end

#diff_months(check_from) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/span/date_diff.rb', line 53

def diff_months( check_from , * )
  num_of_part = 0
  part_singular = :month
  part_next_method = :"next_#{ part_singular }"

  loop do
    check = check_from.send part_next_method

    if check > end_date
      if start_date.day != check_from.day
        day   = start_date.strftime '%d'
        month = check_from.strftime '%m'
        year  = check_from.strftime '%Y'

        date_to_try =
          begin
            Date.parse "#{ day }/#{ month }/#{ year }"
          rescue ArgumentError
            false
          end

        check_from =
          if date_to_try
            date_to_try
          else
            next_month = check_from.next_month.strftime '%m'
            Date.parse( "01/#{ next_month }/#{ year }" ).prev_day
          end
      end

      break
    end

    check_from = check
    num_of_part += 1
  end

  [ check_from , num_of_part , part_singular ]
end

#diff_weeks(check_from) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/span/date_diff.rb', line 93

def diff_weeks( check_from , * )
  num_of_part = 0
  part_singular = :week
  part_next_method = :next_day
  check_from = check_from.prev_day

  loop do
    check = check_from.send part_next_method
    6.times { check = check.send part_next_method }

    break if check > end_date

    check_from = check
    num_of_part += 1
  end

  [ check_from , num_of_part , part_singular ]
end

#diff_years(*args) ⇒ Object



112
113
114
# File 'lib/span/date_diff.rb', line 112

def diff_years( *args )
  default_diff *args , :year
end

#to_hashObject



116
117
118
# File 'lib/span/date_diff.rb', line 116

def to_hash
  @_hash ||= build_diff_hash
end