Class: Timely::TrackableDateSet
- Inherits:
-
Object
- Object
- Timely::TrackableDateSet
- Defined in:
- lib/timely/trackable_date_set.rb
Instance Attribute Summary collapse
-
#dates ⇒ Object
readonly
Returns the value of attribute dates.
-
#dates_to_do ⇒ Object
readonly
Returns the value of attribute dates_to_do.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
Class Method Summary collapse
-
.from_params(date_string, duration_string = nil) ⇒ Object
TODO: remove Initialize from a date + duration.
- .new_for_date(date, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#action_applied?(action) ⇒ Boolean
Check if an action has been applied.
- #all_done? ⇒ Boolean
-
#apply_action(action) ⇒ Object
Remember an action has been applied across the whole date set.
-
#dates_done ⇒ Object
(also: #find_done)
Find which dates ARE done.
-
#do_once(action_name, opts = {}) ⇒ Object
Do something once within this tracked period.
- #done_dates?(date_or_date_range) ⇒ Boolean
- #duration ⇒ Object (also: #number_of_nights)
-
#each_date ⇒ Object
Yield each date in the whole set.
-
#each_date_to_do ⇒ Object
Yield each date to do.
-
#find_to_do ⇒ Object
Find the set of dates which are YET to do.
-
#initialize(dates) ⇒ TrackableDateSet
constructor
Pass in dates as array, range or any kind of enumerable.
-
#set_all_done! ⇒ Object
Set all done!.
- #set_date_done!(date) ⇒ Object
-
#set_dates_done!(date_enum) ⇒ Object
Set dates as done.
Constructor Details
#initialize(dates) ⇒ TrackableDateSet
Pass in dates as array, range or any kind of enumerable
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/timely/trackable_date_set.rb', line 31 def initialize(dates) # Sort so that start/end date are correct sorted_dates = dates.sort # Have to do this, because Set doesn't respond to :last # ...but .sort returns an array which does @start_date = sorted_dates.first @end_date = sorted_dates.last @dates = Set.new(sorted_dates) # track dates_to_do instead of dates_done... better fits common access patterns @dates_to_do = @dates.dup end |
Instance Attribute Details
#dates ⇒ Object (readonly)
Returns the value of attribute dates.
58 59 60 |
# File 'lib/timely/trackable_date_set.rb', line 58 def dates @dates end |
#dates_to_do ⇒ Object (readonly)
Returns the value of attribute dates_to_do.
28 29 30 |
# File 'lib/timely/trackable_date_set.rb', line 28 def dates_to_do @dates_to_do end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
28 29 30 |
# File 'lib/timely/trackable_date_set.rb', line 28 def end_date @end_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
28 29 30 |
# File 'lib/timely/trackable_date_set.rb', line 28 def start_date @start_date end |
Class Method Details
.from_params(date_string, duration_string = nil) ⇒ Object
TODO: remove Initialize from a date + duration
52 53 54 55 56 |
# File 'lib/timely/trackable_date_set.rb', line 52 def self.from_params(date_string, duration_string = nil) duration = duration_string.to_i duration = 1 if duration.zero? new_for_date(date_string.to_date, duration: duration) end |
.new_for_date(date, opts = {}) ⇒ Object
45 46 47 48 |
# File 'lib/timely/trackable_date_set.rb', line 45 def self.new_for_date(date, opts = {}) duration = opts[:duration] || 1 TrackableDateSet.new(date..(date + duration - 1)) end |
Instance Method Details
#action_applied?(action) ⇒ Boolean
Check if an action has been applied
128 129 130 |
# File 'lib/timely/trackable_date_set.rb', line 128 def action_applied?(action) actions_applied.include? action end |
#all_done? ⇒ Boolean
103 104 105 |
# File 'lib/timely/trackable_date_set.rb', line 103 def all_done? @dates_to_do.empty? end |
#apply_action(action) ⇒ Object
Remember an action has been applied across the whole date set
123 124 125 |
# File 'lib/timely/trackable_date_set.rb', line 123 def apply_action(action) actions_applied << action end |
#dates_done ⇒ Object Also known as: find_done
Find which dates ARE done
66 67 68 |
# File 'lib/timely/trackable_date_set.rb', line 66 def dates_done @dates - @dates_to_do end |
#do_once(action_name, opts = {}) ⇒ Object
Do something once within this tracked period
Will only consider job done when opts is true
action_name => Name to track
{:job_done_when} => Block to call, passed result of yield
113 114 115 116 117 118 119 120 |
# File 'lib/timely/trackable_date_set.rb', line 113 def do_once(action_name, opts = {}) return if action_applied?(action_name) result = yield job_done = opts[:job_done_when].blank? || opts[:job_done_when].call(result) apply_action(action_name) if job_done end |
#done_dates?(date_or_date_range) ⇒ Boolean
95 96 97 98 99 100 101 |
# File 'lib/timely/trackable_date_set.rb', line 95 def done_dates?(date_or_date_range) if date_or_date_range.is_a?(Enumerable) @dates_to_do.none? { |date_to_do| date_or_date_range.include?(date_to_do) } else !@dates_to_do.include? date_or_date_range end end |
#duration ⇒ Object Also known as: number_of_nights
132 133 134 |
# File 'lib/timely/trackable_date_set.rb', line 132 def duration @dates.size end |
#each_date ⇒ Object
Yield each date in the whole set
77 78 79 |
# File 'lib/timely/trackable_date_set.rb', line 77 def each_date @dates.each { |date| yield date } end |
#each_date_to_do ⇒ Object
Yield each date to do
72 73 74 |
# File 'lib/timely/trackable_date_set.rb', line 72 def each_date_to_do @dates_to_do.each { |date| yield date } end |
#find_to_do ⇒ Object
Find the set of dates which are YET to do
61 62 63 |
# File 'lib/timely/trackable_date_set.rb', line 61 def find_to_do @dates_to_do end |
#set_all_done! ⇒ Object
Set all done!
91 92 93 |
# File 'lib/timely/trackable_date_set.rb', line 91 def set_all_done! @dates_to_do.clear end |
#set_date_done!(date) ⇒ Object
86 87 88 |
# File 'lib/timely/trackable_date_set.rb', line 86 def set_date_done!(date) @dates_to_do.delete(date) end |
#set_dates_done!(date_enum) ⇒ Object
Set dates as done
82 83 84 |
# File 'lib/timely/trackable_date_set.rb', line 82 def set_dates_done!(date_enum) @dates_to_do.subtract(date_enum) end |