Class: ActivePeriod::Period
- Includes:
- Comparable, HasMany::Holidays
- Defined in:
- lib/active_period/period.rb
Direct Known Subclasses
Instance Method Summary collapse
- #+(duration) ⇒ Object
- #-(duration) ⇒ Object
-
#==(other) ⇒ Boolean
True if period are equals, false otherwise.
- #beginless? ⇒ Boolean
- #boundless? ⇒ Boolean
- #calculated_begin ⇒ Object
-
#calculated_end ⇒ DateTime
The real value of end acording to exclude_end.
- #endless? ⇒ Boolean
- #i18n ⇒ Object
- #infinite? ⇒ Boolean
-
#initialize(range, allow_beginless: true, allow_endless: true) ⇒ self
constructor
A new instance of ActivePeriod::FreePeriod.
- #next ⇒ Object (also: #succ)
- #prev ⇒ Object
- #strftime ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Methods included from HasMany::Holidays
Methods included from Comparable
Methods inherited from Range
Constructor Details
#initialize(range, allow_beginless: true, allow_endless: true) ⇒ self
Returns A new instance of ActivePeriod::FreePeriod.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_period/period.rb', line 15 def initialize(range, allow_beginless: true, allow_endless: true) I18n.t(:base_class_is_abstract, scope: %i[active_period period]) if self.class == ActivePeriod::Period raise ::ArgumentError, I18n.t(:param_must_be_a_range, scope: %i[active_period period]) unless range.class.ancestors.include?(Range) from = time_parse(range.begin, I18n.t(:begin_date_is_invalid, scope: %i[active_period period])) raise ::ArgumentError, I18n.t(:begin_date_is_invalid, scope: %i[active_period period]) if !allow_beginless && from.nil? from = from.try(:beginning_of_day) || from to = time_parse(range.end, I18n.t(:end_date_is_invalid, scope: %i[active_period period])) raise ::ArgumentError, I18n.t(:end_date_is_invalid, scope: %i[active_period period]) if !allow_endless && to.nil? to = to.try(:end_of_day) || to # raise ::ArgumentError, I18n.t(:endless_excluded_end_is_forbiden, scope: %i[active_period period]) if to.nil? && range.exclude_end? # to = to.prev_day if range.exclude_end? if range.exclude_end? && from && to && from.to_date == to.to_date raise ::ArgumentError, I18n.t(:start_is_equal_to_end_excluded, scope: %i[active_period period]) end if from && to && from > to raise ::ArgumentError, I18n.t(:start_is_greater_than_end, scope: %i[active_period period]) end super(from, to, range.exclude_end?) end |
Instance Method Details
#+(duration) ⇒ Object
69 70 71 |
# File 'lib/active_period/period.rb', line 69 def +(duration) raise NotImplementedError end |
#-(duration) ⇒ Object
64 65 66 |
# File 'lib/active_period/period.rb', line 64 def -(duration) raise NotImplementedError end |
#==(other) ⇒ Boolean
Returns true if period are equals, false otherwise.
76 77 78 79 80 81 82 83 84 |
# File 'lib/active_period/period.rb', line 76 def ==(other) if other.class.ancestors.include?(ActivePeriod::Period) from == other.from && self.calculated_end == other.calculated_end elsif other.is_a?(ActiveSupport::Duration) || other.is_a?(Numeric) super(other) else raise ArgumentError end end |
#beginless? ⇒ Boolean
127 128 129 |
# File 'lib/active_period/period.rb', line 127 def beginless? self.begin.nil? end |
#boundless? ⇒ Boolean
131 132 133 |
# File 'lib/active_period/period.rb', line 131 def boundless? beginless? && endless? end |
#calculated_begin ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/active_period/period.rb', line 115 def calculated_begin if beginless? -Date::Infinity.new else self.begin end end |
#calculated_end ⇒ DateTime
Returns The real value of end acording to exclude_end.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_period/period.rb', line 103 def calculated_end if endless? Date::Infinity.new else if exclude_end? self.end.prev_day else self.end end end end |
#endless? ⇒ Boolean
123 124 125 |
# File 'lib/active_period/period.rb', line 123 def endless? self.end.nil? end |
#i18n ⇒ Object
97 98 99 |
# File 'lib/active_period/period.rb', line 97 def i18n raise NotImplementedError end |
#infinite? ⇒ Boolean
135 136 137 |
# File 'lib/active_period/period.rb', line 135 def infinite? beginless? || endless? end |
#next ⇒ Object Also known as: succ
48 49 50 |
# File 'lib/active_period/period.rb', line 48 def next raise NotImplementedError end |
#prev ⇒ Object
54 55 56 |
# File 'lib/active_period/period.rb', line 54 def prev raise NotImplementedError end |
#strftime ⇒ Object
87 88 89 |
# File 'lib/active_period/period.rb', line 87 def strftime raise NotImplementedError end |
#to_i ⇒ Object
59 60 61 |
# File 'lib/active_period/period.rb', line 59 def to_i raise NotImplementedError end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/active_period/period.rb', line 92 def to_s raise NotImplementedError end |