Class: TimeBoss::Calendar::Support::Unit

Inherits:
Object
  • Object
show all
Includes:
Clampable, Navigable, Shiftable, Translatable
Defined in:
lib/timeboss/calendar/support/unit.rb

Direct Known Subclasses

Day, MonthlyUnit, Week

Constant Summary collapse

UnsupportedUnitError =
Class.new(StandardError)

Constants included from Translatable

Translatable::PERIODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Clampable

#clamp

Methods included from Shiftable

#days_ago, #days_ahead, #halves_ago, #halves_ahead, #in_day, #in_half, #in_month, #in_quarter, #in_week, #in_year, #last_day, #last_half, #last_month, #last_quarter, #last_week, #last_year, #months_ago, #months_ahead, #next_day, #next_half, #next_month, #next_quarter, #next_week, #next_year, #quarters_ago, #quarters_ahead, #this_day, #this_half, #this_month, #this_quarter, #this_week, #this_year, #weeks_ago, #weeks_ahead, #years_ago, #years_ahead

Methods included from Translatable

#day, #days, #half, #halves, #month, #months, #quarter, #quarters, #week, #weeks, #year, #years

Methods included from Navigable

#ago, #ahead, #next, #previous, #until

Constructor Details

#initialize(calendar, start_date, end_date) ⇒ Unit

Returns a new instance of Unit.



25
26
27
28
29
# File 'lib/timeboss/calendar/support/unit.rb', line 25

def initialize(calendar, start_date, end_date)
  @calendar = calendar
  @start_date = start_date
  @end_date = end_date
end

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



17
18
19
# File 'lib/timeboss/calendar/support/unit.rb', line 17

def calendar
  @calendar
end

#end_dateObject (readonly)

Returns the value of attribute end_date.



17
18
19
# File 'lib/timeboss/calendar/support/unit.rb', line 17

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



17
18
19
# File 'lib/timeboss/calendar/support/unit.rb', line 17

def start_date
  @start_date
end

Class Method Details

.typeObject



21
22
23
# File 'lib/timeboss/calendar/support/unit.rb', line 21

def self.type
  name.demodulize.underscore
end

Instance Method Details

#+(other) ⇒ Unit

Move some number of units forward from this unit.

Parameters:

  • value (Integer)

Returns:



72
73
74
# File 'lib/timeboss/calendar/support/unit.rb', line 72

def +(other)
  offset(other)
end

#-(other) ⇒ Unit

Move some number of units backward from this unit.

Parameters:

  • value (Integer)

Returns:



79
80
81
# File 'lib/timeboss/calendar/support/unit.rb', line 79

def -(other)
  offset(-other)
end

#==(other) ⇒ Boolean

Is the specified unit equal to this one, based on its unit type and date range?

Parameters:

  • entry (Unit)

    the unit to compare

Returns:

  • (Boolean)

    true when periods are equal



34
35
36
# File 'lib/timeboss/calendar/support/unit.rb', line 34

def ==(other)
  self.class == other.class && start_date == other.start_date && end_date == other.end_date
end

#current?Boolean

Does this period cover the current date?

Returns:

  • (Boolean)


54
55
56
# File 'lib/timeboss/calendar/support/unit.rb', line 54

def current?
  Date.today.between?(start_date, end_date)
end

#datesObject (protected)



95
96
97
# File 'lib/timeboss/calendar/support/unit.rb', line 95

def dates
  @_dates ||= to_range.to_a
end

#format(*periods) ⇒ String

Format this period based on specified granularities.

Parameters:

  • periods (Array<Symbol, String>)

    the periods to include (‘half, week`, or `quarter`)

Returns:

  • (String)

    (e.g. “2020H2W7” or “2020Q3”)



41
42
43
# File 'lib/timeboss/calendar/support/unit.rb', line 41

def format(*periods)
  Formatter.new(self, periods.presence || Formatter::PERIODS).to_s
end

#inspectObject



89
90
91
# File 'lib/timeboss/calendar/support/unit.rb', line 89

def inspect
  "#<#{self.class.name} start_date=#{start_date}, end_date=#{end_date}>"
end

#offset(value) ⇒ Unit

Return the unit relative to this one by the specified offset. Offset values can be positive or negative.

Parameters:

  • value (Integer)

Returns:



62
63
64
65
66
67
# File 'lib/timeboss/calendar/support/unit.rb', line 62

def offset(value)
  method = value.negative? ? :previous : :next
  base = self
  value.abs.times { base = base.public_send(method) }
  base
end

#thru(unit) ⇒ Period

Starting from this unit of time, build a period extending through the specified time unit.

Parameters:

  • unit (Unit)

    the period to extend through

Returns:



48
49
50
# File 'lib/timeboss/calendar/support/unit.rb', line 48

def thru(unit)
  Period.new(calendar, self, unit)
end

#to_rangeRange<Date, Date>

Express this period as a date range.

Returns:

  • (Range<Date, Date>)


85
86
87
# File 'lib/timeboss/calendar/support/unit.rb', line 85

def to_range
  @_to_range ||= start_date..end_date
end