Module: Eventual

Defined in:
lib/eventual.rb,
lib/eventual/version.rb,
lib/eventual/syntax_nodes.rb

Defined Under Namespace

Classes: DatePeriod, Day, MonthName, MonthPeriod, Node, Period, Time, Time12, TimeList, TimeRange, TimeRangeList, WdayMatchError, WeekdayConstrain, WeekdayPeriodConstrain, Year

Constant Summary collapse

VERSION =
'0.5.9'
Weekdays =
%w(domingo lunes martes miércoles jueves viernes sábado).freeze
MonthNames =
%w(enero febrero marzo abril mayo junio julio agosto septiembre noviembre).unshift(nil).freeze
ShortMonthNames =
%w(ene feb mar abr may jun jul ago sept oct nov dic).freeze
WdaysR =
[/d/, /l/, /ma/, /mi/, /j/, /v/, /s/].freeze
WdayListR =
/\b(?:#{ WdaysR.join('|') })/.freeze

Class Method Summary collapse

Class Method Details

.parse(text, opts = {}) ⇒ Object

Parses dates specified in natural language and returns an Eventual::Node object

Eventual.parse( 'del 5 al 7 de junio del 2009' ).to_a
=> [#<DateTime: 4909975/2,0,2299161>, #<DateTime: 4909977/2,0,2299161>, #<DateTime: 4909979/2,0,2299161

Options: :lang:

Defaults to 'Es', the language to be used for parsing, currently only spanish is supported, please contribute.

:default_year:

The default year to be used in case year is not specified in the text, defaults to current year

:default_event_span:

The duration in minutes an event has, defaults to 60

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eventual.rb', line 28

def self.parse text, opts = {}
  lang    = opts.delete(:lang) || 'Es'
  year    = opts.delete(:default_year)
  
  raise ArgumentError, "Expected option `:default_year` to be an integer"        if year && !(Integer === year)
  
  parser = const_get("#{ lang.to_s[0..1].capitalize }DatesParser") rescue raise( NotImplementedError.new("Parsing has not yet been implemented for the language '#{lang}'"))

  node   = parser.new.parse text.gsub('sab', 'sáb').gsub('mie', 'mié').downcase

  node.year      = year if year
  node
end