Class: Timely::TrackableDateSet
- Inherits:
-
Object
- Object
- Timely::TrackableDateSet
- Defined in:
- lib/timely/trackable_date_set.rb
Instance Attribute Summary collapse
-
#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 ⇒ Object
-
#dates_done ⇒ Object
(also: #find_done)
Find which dates ARE done.
-
#do_once(action_name, opts = {}) ⇒ Object
Do something once within this tracked period.
- #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.
- #has_done?(date_or_date_range) ⇒ Boolean
-
#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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/timely/trackable_date_set.rb', line 29 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_to_do ⇒ Object (readonly)
Returns the value of attribute dates_to_do.
26 27 28 |
# File 'lib/timely/trackable_date_set.rb', line 26 def dates_to_do @dates_to_do end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
26 27 28 |
# File 'lib/timely/trackable_date_set.rb', line 26 def end_date @end_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
26 27 28 |
# File 'lib/timely/trackable_date_set.rb', line 26 def start_date @start_date end |
Class Method Details
.from_params(date_string, duration_string = nil) ⇒ Object
Todo: remove Initialize from a date + duration
50 51 52 53 54 |
# File 'lib/timely/trackable_date_set.rb', line 50 def self.from_params(date_string, duration_string=nil) duration = duration_string.to_i duration = 1 if duration == 0 new_for_date(date_string.to_date, :duration => duration) end |
.new_for_date(date, opts = {}) ⇒ Object
43 44 45 46 |
# File 'lib/timely/trackable_date_set.rb', line 43 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
129 130 131 |
# File 'lib/timely/trackable_date_set.rb', line 129 def action_applied?(action) actions_applied.include? action end |
#all_done? ⇒ Boolean
105 106 107 |
# File 'lib/timely/trackable_date_set.rb', line 105 def all_done? @dates_to_do.empty? end |
#apply_action(action) ⇒ Object
Remember an action has been applied across the whole date set
124 125 126 |
# File 'lib/timely/trackable_date_set.rb', line 124 def apply_action(action) actions_applied << action end |
#dates ⇒ Object
56 57 58 |
# File 'lib/timely/trackable_date_set.rb', line 56 def dates @dates 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
115 116 117 118 119 120 121 |
# File 'lib/timely/trackable_date_set.rb', line 115 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 |
#duration ⇒ Object Also known as: number_of_nights
133 134 135 |
# File 'lib/timely/trackable_date_set.rb', line 133 def duration @dates.size end |
#each_date ⇒ Object
Yield each date in the whole set
78 79 80 81 |
# File 'lib/timely/trackable_date_set.rb', line 78 def each_date # Sort method needed as Ruby 1.8 set's aren't ordered @dates.sort.each{|date| yield date} end |
#each_date_to_do ⇒ Object
Yield each date to do
72 73 74 75 |
# File 'lib/timely/trackable_date_set.rb', line 72 def each_date_to_do # Sort method needed as Ruby 1.8 set's aren't ordered @dates_to_do.sort.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 |
#has_done?(date_or_date_range) ⇒ Boolean
97 98 99 100 101 102 103 |
# File 'lib/timely/trackable_date_set.rb', line 97 def has_done?(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 |
#set_all_done! ⇒ Object
Set all done!
93 94 95 |
# File 'lib/timely/trackable_date_set.rb', line 93 def set_all_done! @dates_to_do.clear end |
#set_date_done!(date) ⇒ Object
88 89 90 |
# File 'lib/timely/trackable_date_set.rb', line 88 def set_date_done!(date) @dates_to_do.delete(date) end |
#set_dates_done!(date_enum) ⇒ Object
Set dates as done
84 85 86 |
# File 'lib/timely/trackable_date_set.rb', line 84 def set_dates_done!(date_enum) @dates_to_do.subtract(date_enum) end |