class Workpattern::Workpattern
Represents the working and resting periods across a given number of whole years. Each Workpattern
has a unique name so it can be easily identified amongst all the other Workpattern
objects.
This and the Clock
class are the only two that should be referenced by calling applications when using this gem.
Attributes
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
@!attribute [r] name
Name given to the <tt>Workpattern</tt>
@!attribute [r] base
Starting year
@!attribute [r] span
Number of years
@!attribute [r] from
First date in <tt>Workpattern</tt>
@!attribute [r] to
Last date in <tt>Workpattern</tt>
@!attribute [r] weeks
The <tt>Week</tt> objects that make up this workpattern
Public Class Methods
Deletes all Workpattern
objects
# File lib/workpattern/workpattern.rb, line 102 def self.clear workpatterns.clear end
Deletes the specific named Workpattern
@param [String] name of the required Workpattern
@return [Boolean] true if the named Workpattern
existed or false if it doesn't
# File lib/workpattern/workpattern.rb, line 128 def self.delete(name) workpatterns.delete(name).nil? ? false : true end
Returns the specific named Workpattern
@param [String] name of the required Workpattern
@raise [NameError] if a Workpattern
of the supplied name does not exist
# File lib/workpattern/workpattern.rb, line 118 def self.get(name) return workpatterns[name] if workpatterns.key?(name) raise(NameError, "Workpattern '#{name}' doesn't exist so can't be retrieved") end
The new Workpattern
object is created with all working minutes.
@param [String] name Every workpattern has a unique name @param [Integer] base Workpattern
starts on the 1st January of this year. @param [Integer] span Workpattern
spans this number of years ending on 31st December. @raise [NameError] if the given name already exists
# File lib/workpattern/workpattern.rb, line 78 def initialize(name = DEFAULT_NAME, base = DEFAULT_BASE_YEAR, span = DEFAULT_SPAN) if workpatterns.key?(name) raise(NameError, "Workpattern '#{name}' already exists and can't be created again") end offset = span < 0 ? span.abs - 1 : 0 @name = name @base = base @span = span @from = Time.gm(@base.abs - offset) @to = Time.gm(@from.year + @span.abs - 1, 12, 31, 23, 59) @weeks = SortedSet.new @weeks << Week.new(@from, @to) workpatterns[@name] = self @week_pattern = WeekPattern.new(self) end
# File lib/workpattern/workpattern.rb, line 47 def self.persistence? @@persist ||= nil end
Class for handling persistence in user's own way
# File lib/workpattern/workpattern.rb, line 43 def self.persistence_class=(klass) @@persist = klass end
Returns an Array containing all the Workpattern
objects @return [Array] all Workpattern
objects
# File lib/workpattern/workpattern.rb, line 109 def self.to_a workpatterns.to_a end
# File lib/workpattern/workpattern.rb, line 18 def self.workpatterns @@workpatterns end
Public Instance Methods
Calculates the resulting date when the duration
in minutes is added to the start
date. The duration
is always in whole minutes and subtracts from start
when it is a negative number.
@param [DateTime] start date to add or subtract minutes @param [Integer] duration in minutes to add or subtract to date @return [DateTime] the date when duration
is added to start
# File lib/workpattern/workpattern.rb, line 191 def calc(start, duration) return start if duration == 0 a_day = SAME_DAY utc_start = to_utc(start) while duration != 0 if a_day == PREVIOUS_DAY utc_start -= DAY a_day = SAME_DAY utc_start = Time.gm(utc_start.year, utc_start.month, utc_start.day,LAST_TIME_IN_DAY.hour, LAST_TIME_IN_DAY.min) week = find_weekpattern(utc_start) if week.working?(utc_start) duration += 1 end else week = find_weekpattern(utc_start) end utc_start, duration, a_day = week.calc(utc_start, duration, a_day) end to_local(utc_start) end
Returns number of minutes between two dates
@param [DateTime] start is the date to start from @param [DateTime] finish is the date to end with @return [Integer] number of minutes between the two dates
# File lib/workpattern/workpattern.rb, line 232 def diff(start, finish) utc_start = to_utc(start) utc_finish = to_utc(finish) utc_start, utc_finish = utc_finish, utc_start if utc_finish < utc_start minutes = 0 while utc_start != utc_finish week = find_weekpattern(utc_start) r_minutes, utc_start = week.diff(utc_start, utc_finish) minutes += r_minutes end minutes end
Retrieve the correct Week
pattern for the supplied date.
If the supplied date
is outside the span of the Workpattern
object then it returns an all working Week
object for the calculation.
@param [DateTime] date whose containing Week
pattern is required @return [Week] Week
object that includes the supplied date
in it's range
# File lib/workpattern/workpattern.rb, line 256 def find_weekpattern(date) # find the pattern that fits the date # if date < @from result = Week.new(Time.at(0), @from - MINUTE, WORK_TYPE) elsif date > to result = Week.new(@to + MINUTE, Time.new(9999), WORK_TYPE) else date = Time.gm(date.year, date.month, date.day) result = @weeks.find { |week| week.start <= date && week.finish >= date } end result end
Convenience method that calls #workpattern
with the :work_type
specified as resting.
@see workpattern
# File lib/workpattern/workpattern.rb, line 166 def resting(args = {}) args[:work_type] = REST_TYPE workpattern(args) end
Retrieves the local timezone
# File lib/workpattern/workpattern.rb, line 66 def timezone @@tz || @@tz = TZInfo::Timezone.get(Time.now.zone) end
Converts a date like object into local time
# File lib/workpattern/workpattern.rb, line 61 def to_local(date) date.to_time.getgm end
Converts a date like object into utc
# File lib/workpattern/workpattern.rb, line 56 def to_utc(date) date.to_time.utc end
# File lib/workpattern/workpattern.rb, line 96 def week_pattern @week_pattern end
Convenience method that calls #workpattern
with the :work_type
specified as working.
@see workpattern
# File lib/workpattern/workpattern.rb, line 176 def working(args = {}) args[:work_type] = WORK_TYPE workpattern(args) end
Returns true if the given minute is working and false if it is resting.
@param [DateTime] start DateTime being tested @return [Boolean] true if working and false if resting
# File lib/workpattern/workpattern.rb, line 221 def working?(start) utc_start = to_utc(start) find_weekpattern(utc_start).working?(utc_start) end
Applys a working or resting pattern to the Workpattern
object.
The resting
and working
methods are convenience methods that call this with the appropriate :work_type
already set.
@param [Hash] opts the options used to apply a workpattern @option opts [Date] :start The first date to apply the pattern. Defaults
to the <tt>start</tt> attribute.
@option opts [Date] :finish The last date to apply the pattern. Defaults
to the <tt>finish</tt> attribute.
@option opts [DAYNAMES] :days The specific day or days the pattern will apply to.It defaults to :all
@option opts [(hour, min)] :start_time The first time in the selected days to apply the pattern. Defaults to 00:00
. @option opts [(hour, min)] :finish_time The last time in the selected days to apply the pattern. Defaults to 23:59
. @option opts [(WORK_TYPE || REST_TYPE)] :work_type Either working or resting. Defaults to working. @see working
@see resting
# File lib/workpattern/workpattern.rb, line 153 def workpattern(opts = {}) if self.class.persistence? week_pattern.workpattern(opts, @@persistence) else week_pattern.workpattern(opts) end end
# File lib/workpattern/workpattern.rb, line 22 def workpatterns @@workpatterns end