Class: LangDate

Inherits:
Resyma::Language show all
Defined in:
lib/resyma/nise/date.rb

Overview

DSL reading dates

Instance Attribute Summary

Attributes inherited from Resyma::Language

#engine

Instance Method Summary collapse

Methods inherited from Resyma::Language

#build_language, #built?, #initialize, #load, load, #load_ast, #load_parsetree!

Constructor Details

This class inherits a constructor from Resyma::Language

Instance Method Details

#syntaxObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/resyma/nise/date.rb', line 8

def syntax
  id("today") >> Date.today

  (int; id("/"); int; id("/"); int) >> begin
    year = nodes[0].to_ruby
    month = nodes[2].to_ruby
    day = nodes[4].to_ruby
    Date.new(year, month, day)
  end

  (numop; numbase; "."; [id("day"), id("month"), id("year")]) >> begin
    op, num, _, unit = nodes
    sig = op.to_literal == "+" ? 1 : -1
    val = num.to_literal.to_i * sig
    case unit.to_literal
    when "day" then Date.today.next_day(val)
    when "month" then Date.today.next_month(val)
    when "year" then Date.today.next_year(val)
    end
  end

  id("yesterday") >> LangDate.load { -1.day }
  id("tomorrow") >> LangDate.load { +1.day }
end