Class: NaturalDateExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/natural-date/natural_date_expression.rb

Constant Summary collapse

VERSION =
"1.1"

Instance Method Summary collapse

Constructor Details

#initialize(data, reference_date, expression_string) ⇒ NaturalDateExpression

Returns a new instance of NaturalDateExpression.



4
5
6
7
8
# File 'lib/natural-date/natural_date_expression.rb', line 4

def initialize data, reference_date, expression_string
  @data = data
  @reference_date = reference_date
  @expression_string = expression_string
end

Instance Method Details

#dataObject



33
34
35
# File 'lib/natural-date/natural_date_expression.rb', line 33

def data
  @data.dup
end

#fetch_dates(dates_range = nil) ⇒ Object



37
38
39
40
41
# File 'lib/natural-date/natural_date_expression.rb', line 37

def fetch_dates dates_range = nil
  (dates_range || (Date.today..(Date.today + 365))).to_a.select do |date|
    match(date).fetch(:match)
  end
end

#match(date) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/natural-date/natural_date_expression.rb', line 10

def match date
  matches = @data.map do |expression_map|
    match_week?(date, expression_map) &&
      match_month?(date, expression_map) &&
      match_literal?(date, expression_map) &&
      match_day?(date, expression_map) &&
      match_year?(date, expression_map)
  end

  first_matched_expression = if matches.any?
                               @data[matches.each_with_index.find { |exp, index| exp }.last]
                             else
                               nil
                             end

  { match: matches.any?,
    first_matched_expression: first_matched_expression }
end

#recurrent?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/natural-date/natural_date_expression.rb', line 29

def recurrent?
  @data.map { |expression_map| !(expression_map[:day] && expression_map[:month]) }.any?
end