Class: PeriodsValidator::Periods
- Inherits:
-
Object
- Object
- PeriodsValidator::Periods
- Defined in:
- lib/periods_validator/periods.rb
Constant Summary collapse
- PERIODS =
%i[monthly quarterly semesterly yearly].freeze
- MONTHS_QUARTERLY =
[3, 6, 9, 12].freeze
- MONTHS_SEMESTERLY =
[6, 12].freeze
- YEARLY =
11
- QUARTERLY =
2
- SEMESTERLY =
5
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Periods
constructor
A new instance of Periods.
- #valid? ⇒ Boolean (also: #validate)
Constructor Details
#initialize(args = {}) ⇒ Periods
Returns a new instance of Periods.
12 13 14 15 16 17 18 |
# File 'lib/periods_validator/periods.rb', line 12 def initialize(args={}) @start_range = args.fetch(:start_range) @end_range = args.fetch(:end_range) @periods = args.fetch(:periods, []) @error = nil @months = 0 end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
3 4 5 |
# File 'lib/periods_validator/periods.rb', line 3 def error @error end |
Instance Method Details
#valid? ⇒ Boolean Also known as: validate
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/periods_validator/periods.rb', line 20 def valid? if @periods.empty? || !(@periods - PERIODS).empty? @error = :invalid_define_period return false end if @start_range.nil? || @end_range.nil? @error = :invalid_period return false end @months = (@end_range.year * 12 + @end_range.month) - (@start_range.year * 12 + @start_range.month) @periods.each do |period| return true if send("period_#{period}") end @error = :invalid_period false end |