Class: DateSetter
- Inherits:
-
Object
- Object
- DateSetter
- Defined in:
- lib/DateSetter.rb
Constant Summary collapse
- Infinity =
1.0 / 0.0
Instance Attribute Summary collapse
-
#end_of_day ⇒ Object
Returns the value of attribute end_of_day.
-
#max_date ⇒ Object
Returns the value of attribute max_date.
-
#min_date ⇒ Object
Returns the value of attribute min_date.
-
#range ⇒ Object
Returns the value of attribute range.
-
#reference_date ⇒ Object
Returns the value of attribute reference_date.
-
#result ⇒ Object
Returns the value of attribute result.
-
#start_of_day ⇒ Object
Returns the value of attribute start_of_day.
Class Method Summary collapse
Instance Method Summary collapse
- #hours_range_when_max_date ⇒ Object
- #hours_range_when_min_date ⇒ Object
-
#initialize(options = {}) ⇒ DateSetter
constructor
A new instance of DateSetter.
- #intersection(first_range, second_range) ⇒ Object
- #intersection_of_range_and_hours_range ⇒ Object
- #invalid? ⇒ Boolean
- #result_day ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ DateSetter
Returns a new instance of DateSetter.
5 6 7 8 9 |
# File 'lib/DateSetter.rb', line 5 def initialize( = {}) .each do |key, value| send "#{key}=", value end end |
Instance Attribute Details
#end_of_day ⇒ Object
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_date ⇒ Object
Returns the value of attribute max_date.
3 4 5 |
# File 'lib/DateSetter.rb', line 3 def max_date @max_date end |
#min_date ⇒ Object
Returns the value of attribute min_date.
3 4 5 |
# File 'lib/DateSetter.rb', line 3 def min_date @min_date end |
#range ⇒ Object
Returns the value of attribute range.
3 4 5 |
# File 'lib/DateSetter.rb', line 3 def range @range end |
#reference_date ⇒ Object
Returns the value of attribute reference_date.
3 4 5 |
# File 'lib/DateSetter.rb', line 3 def reference_date @reference_date end |
#result ⇒ Object
Returns the value of attribute result.
3 4 5 |
# File 'lib/DateSetter.rb', line 3 def result @result end |
#start_of_day ⇒ Object
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, = {}) .merge!(reference_date: reference_date, range: range) new().result end |
Instance Method Details
#hours_range_when_max_date ⇒ Object
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_date ⇒ Object
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_range ⇒ Object
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
21 22 23 |
# File 'lib/DateSetter.rb', line 21 def invalid? !valid? end |
#result_day ⇒ Object
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
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 |