Class: Span::TimeDiff

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_date_input, start_date_input) ⇒ TimeDiff

Returns a new instance of TimeDiff.



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

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.



11
12
13
# File 'lib/span.rb', line 11

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



11
12
13
# File 'lib/span.rb', line 11

def start_date
  @start_date
end

Class Method Details

.compute(*args, &block) ⇒ Object



15
16
17
# File 'lib/span.rb', line 15

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

Instance Method Details

#build_diff_hashObject



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

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



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

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



55
56
57
# File 'lib/span.rb', line 55

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

#diff_months(check_from) ⇒ Object



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

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



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

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



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

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

#to_hashObject



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

def to_hash
  @_hash ||= build_diff_hash
end