Class: DateSetter

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

Constant Summary collapse

Infinity =
1.0 / 0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DateSetter

Returns a new instance of DateSetter.



5
6
7
8
9
# File 'lib/DateSetter.rb', line 5

def initialize(options = {})
  options.each do |key, value|
    send "#{key}=", value
  end
end

Instance Attribute Details

#end_of_dayObject

Returns the value of attribute end_of_day.



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

def end_of_day
  @end_of_day
end

#max_dateObject

Returns the value of attribute max_date.



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

def max_date
  @max_date
end

#min_dateObject

Returns the value of attribute min_date.



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

def min_date
  @min_date
end

#rangeObject

Returns the value of attribute range.



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

def range
  @range
end

#reference_dateObject

Returns the value of attribute reference_date.



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

def reference_date
  @reference_date
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#start_of_dayObject

Returns the value of attribute start_of_day.



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

def start_of_day
  @start_of_day
end

Class Method Details

.set(reference_date, range, options = {}) ⇒ Object



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

def self.set(reference_date, range, options = {})
  options.merge!(reference_date: reference_date, range: range)
  new(options).result
end

Instance Method Details

#hours_range_when_max_dateObject



86
87
88
# File 'lib/DateSetter.rb', line 86

def hours_range_when_max_date
  (start_of_day..max_date_time_hour)
end

#hours_range_when_min_dateObject



82
83
84
# File 'lib/DateSetter.rb', line 82

def hours_range_when_min_date
  (min_date_time_hour..end_of_day)
end

#intersection(first_range, second_range) ⇒ Object



73
74
75
76
# File 'lib/DateSetter.rb', line 73

def intersection(first_range, second_range)
  return nil if (first_range.max < second_range.begin or second_range.max < first_range.begin)
  [first_range.begin, second_range.begin].max..[first_range.max, second_range.max].min
end

#intersection_of_range_and_hours_rangeObject



78
79
80
# File 'lib/DateSetter.rb', line 78

def intersection_of_range_and_hours_range
  intersection((min_date_time_hour..max_date_time_hour), (start_of_day..end_of_day))
end

#invalid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/DateSetter.rb', line 21

def invalid?
  !valid?
end

#result_dayObject



61
62
63
# File 'lib/DateSetter.rb', line 61

def result_day
  @result_day ||= rand(min_date_in_range..max_date_in_range)
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/DateSetter.rb', line 16

def valid?
  return false if !reference_date || !range || !valid_ranges?
  (min_date.to_f..max_date.to_f).cover?(reference_date.to_f)
end