Class: TimeBoss::Calendar::Support::Unit
- Inherits:
-
Object
- Object
- TimeBoss::Calendar::Support::Unit
- Includes:
- Navigable, Shiftable, Translatable
- Defined in:
- lib/timeboss/calendar/support/unit.rb
Direct Known Subclasses
Constant Summary collapse
- UnsupportedUnitError =
Class.new(StandardError)
Constants included from Translatable
Instance Attribute Summary collapse
-
#calendar ⇒ Object
readonly
Returns the value of attribute calendar.
-
#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
Instance Method Summary collapse
-
#+(other) ⇒ Unit
Move some number of units forward from this unit.
-
#-(other) ⇒ Unit
Move some number of units backward from this unit.
-
#==(other) ⇒ Boolean
Is the specified unit equal to this one, based on its unit type and date range?.
-
#current? ⇒ Boolean
Does this period cover the current date?.
- #dates ⇒ Object protected
-
#format(*periods) ⇒ String
Format this period based on specified granularities.
-
#initialize(calendar, start_date, end_date) ⇒ Unit
constructor
A new instance of Unit.
- #inspect ⇒ Object
-
#offset(value) ⇒ Unit
Return the unit relative to this one by the specified offset.
-
#thru(unit) ⇒ Period
Starting from this unit of time, build a period extending through the specified time unit.
-
#to_range ⇒ Range<Date, Date>
Express this period as a date range.
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.
23 24 25 26 27 |
# File 'lib/timeboss/calendar/support/unit.rb', line 23 def initialize(calendar, start_date, end_date) @calendar = calendar @start_date = start_date @end_date = end_date end |
Instance Attribute Details
#calendar ⇒ Object (readonly)
Returns the value of attribute calendar.
15 16 17 |
# File 'lib/timeboss/calendar/support/unit.rb', line 15 def calendar @calendar end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
15 16 17 |
# File 'lib/timeboss/calendar/support/unit.rb', line 15 def end_date @end_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
15 16 17 |
# File 'lib/timeboss/calendar/support/unit.rb', line 15 def start_date @start_date end |
Class Method Details
.type ⇒ Object
19 20 21 |
# File 'lib/timeboss/calendar/support/unit.rb', line 19 def self.type name.demodulize.underscore end |
Instance Method Details
#+(other) ⇒ Unit
Move some number of units forward from this unit.
70 71 72 |
# File 'lib/timeboss/calendar/support/unit.rb', line 70 def +(other) offset(other) end |
#-(other) ⇒ Unit
Move some number of units backward from this unit.
77 78 79 |
# File 'lib/timeboss/calendar/support/unit.rb', line 77 def -(other) offset(-other) end |
#==(other) ⇒ Boolean
Is the specified unit equal to this one, based on its unit type and date range?
32 33 34 |
# File 'lib/timeboss/calendar/support/unit.rb', line 32 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?
52 53 54 |
# File 'lib/timeboss/calendar/support/unit.rb', line 52 def current? Date.today.between?(start_date, end_date) end |
#dates ⇒ Object (protected)
93 94 95 |
# File 'lib/timeboss/calendar/support/unit.rb', line 93 def dates @_dates ||= to_range.to_a end |
#format(*periods) ⇒ String
Format this period based on specified granularities.
39 40 41 |
# File 'lib/timeboss/calendar/support/unit.rb', line 39 def format(*periods) Formatter.new(self, periods.presence || Formatter::PERIODS).to_s end |
#inspect ⇒ Object
87 88 89 |
# File 'lib/timeboss/calendar/support/unit.rb', line 87 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.
60 61 62 63 64 65 |
# File 'lib/timeboss/calendar/support/unit.rb', line 60 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.
46 47 48 |
# File 'lib/timeboss/calendar/support/unit.rb', line 46 def thru(unit) Period.new(calendar, self, unit) end |
#to_range ⇒ Range<Date, Date>
Express this period as a date range.
83 84 85 |
# File 'lib/timeboss/calendar/support/unit.rb', line 83 def to_range @_to_range ||= start_date..end_date end |