Class: NaturalDateExpression
- Inherits:
-
Object
- Object
- NaturalDateExpression
- Defined in:
- lib/natural-date/natural_date_expression.rb
Defined Under Namespace
Classes: DateMatch
Constant Summary collapse
- VERSION =
"1.5"
- MATCHERS =
[ WeekMatcher, MonthMatcher, DayMatcher, YearMatcher ].freeze
Instance Method Summary collapse
- #=~(date) ⇒ Object
- #data ⇒ Object
- #fetch_dates(dates_range = nil) ⇒ Object
-
#initialize(data, reference_date, expression_string) ⇒ NaturalDateExpression
constructor
A new instance of NaturalDateExpression.
- #match(date) ⇒ Object
- #match? ⇒ Boolean
- #recurrent? ⇒ Boolean
Constructor Details
#initialize(data, reference_date, expression_string) ⇒ NaturalDateExpression
Returns a new instance of NaturalDateExpression.
19 20 21 22 23 |
# File 'lib/natural-date/natural_date_expression.rb', line 19 def initialize data, reference_date, expression_string @data = data @reference_date = reference_date @expression_string = expression_string end |
Instance Method Details
#=~(date) ⇒ Object
29 30 31 |
# File 'lib/natural-date/natural_date_expression.rb', line 29 def =~ date match(date).matches? end |
#data ⇒ Object
55 56 57 |
# File 'lib/natural-date/natural_date_expression.rb', line 55 def data @data.dup end |
#fetch_dates(dates_range = nil) ⇒ Object
59 60 61 |
# File 'lib/natural-date/natural_date_expression.rb', line 59 def fetch_dates dates_range = nil (dates_range || (@reference_date..(@reference_date + 365))).to_a.select { |date| self =~ date } end |
#match(date) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/natural-date/natural_date_expression.rb', line 40 def match date matches = @data.map do |expression_map| MATCHERS.map { |matcher| matcher.match?(date, @reference_date, expression_map) }.all? end DateMatch.new(matches.any?, (matches.any?? @data[matches.each_with_index.find { |exp, index| exp }.last] : nil), @date, @reference_date) end |
#match? ⇒ Boolean
25 26 27 |
# File 'lib/natural-date/natural_date_expression.rb', line 25 def match? match(date).matches? end |
#recurrent? ⇒ Boolean
51 52 53 |
# File 'lib/natural-date/natural_date_expression.rb', line 51 def recurrent? @data.map { |expression_map| !(expression_map[:day] && expression_map[:month]) }.any? end |