Class: TimeBoss::Calendar::Support::Unit
- Inherits:
-
Object
- Object
- TimeBoss::Calendar::Support::Unit
- Includes:
- Clampable, 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 Clampable
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
#calendar ⇒ Object (readonly)
Returns the value of attribute calendar.
17 18 19 |
# File 'lib/timeboss/calendar/support/unit.rb', line 17 def calendar @calendar end |
#end_date ⇒ Object (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_date ⇒ Object (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
.type ⇒ Object
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.
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.
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?
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?
54 55 56 |
# File 'lib/timeboss/calendar/support/unit.rb', line 54 def current? Date.today.between?(start_date, end_date) end |
#dates ⇒ Object (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.
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 |
#inspect ⇒ Object
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.
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.
48 49 50 |
# File 'lib/timeboss/calendar/support/unit.rb', line 48 def thru(unit) Period.new(calendar, self, unit) end |
#to_range ⇒ Range<Date, Date>
Express this period as a date range.
85 86 87 |
# File 'lib/timeboss/calendar/support/unit.rb', line 85 def to_range @_to_range ||= start_date..end_date end |