Class: NaturalDateExpression

Inherits:
Object
  • Object
show all
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

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

#dataObject



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

Returns:

  • (Boolean)


25
26
27
# File 'lib/natural-date/natural_date_expression.rb', line 25

def match?
  match(date).matches?
end

#recurrent?Boolean

Returns:

  • (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