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
17
18
19
20
21
22
23
24
# 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

  if start_date > end_date
    @end_date   = start_date
    @start_date = end_date
  else
    @end_date   = end_date
    @start_date = start_date
  end
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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/span/date_diff.rb', line 26

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/span/date_diff.rb', line 41

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



57
58
59
# File 'lib/span/date_diff.rb', line 57

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

#diff_months(check_from) ⇒ Object



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
92
93
94
95
96
97
98
99
# File 'lib/span/date_diff.rb', line 61

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/span/date_diff.rb', line 101

def diff_weeks( check_from , * )
  num_of_part = 0
  part_singular = :week
  part_next_method = :next_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



119
120
121
# File 'lib/span/date_diff.rb', line 119

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

#to_hashObject



123
124
125
# File 'lib/span/date_diff.rb', line 123

def to_hash
  @_hash ||= build_diff_hash
end