class Quando::Parser

Public Class Methods

new() click to toggle source
# File lib/quando/parser.rb, line 6
def initialize
  @config = nil
end

Public Instance Methods

config() click to toggle source

@return [Quando::Config]

# File lib/quando/parser.rb, line 17
def config
  @config || Quando.config
end
configure() { |config ||= config.dup| ... } click to toggle source

@return [Quando::Parser]

# File lib/quando/parser.rb, line 11
def configure
  yield(@config ||= Quando.config.dup)
  self
end
parse(text_date) click to toggle source

@param text_date [String] @return [Date, nil]

# File lib/quando/parser.rb, line 23
def parse(text_date)
  config.formats.each do |regexp|
    @date_parts = text_date.match(regexp)
    next unless @date_parts

    @current_format = regexp
    year, month, day = detect_year, detect_month, detect_day
    next unless (year && month && day)

    return Date.new(year, month, day)
  end

  nil
end

Private Instance Methods

century_to_hundreds() click to toggle source
# File lib/quando/parser.rb, line 97
def century_to_hundreds
  (config.century > 0 ? config.century - 1 : config.century + 1) * 100
end
detect_day() click to toggle source

@return [Integer, nil]

# File lib/quando/parser.rb, line 69
def detect_day
  return 1 unless wanted?(:day)
  return unless found?(:day)

  day = @date_parts[:day].to_i
  day if valid_day?(day)
end
detect_month() click to toggle source

@return [Integer, nil]

# File lib/quando/parser.rb, line 50
def detect_month
  return 1 unless wanted?(:month)
  return unless found?(:month)

  month = @date_parts[:month]

  month_index = Quando::Config::MONTHS.find_index do |month_name|
    config.send(month_name) =~ month
  end

  if month_index
    month_index += 1
    return month_index if valid_month?(month_index)
  end

  month.to_i if valid_month?(month)
end
detect_year() click to toggle source

@return [Integer, nil]

# File lib/quando/parser.rb, line 41
def detect_year
  return Time.now.utc.year unless wanted?(:year)
  return unless found?(:year)

  year = @date_parts[:year].to_i
  year.abs < 100 ? year + century_to_hundreds : year
end
found?(date_part) click to toggle source

@param date_part [Symbol]

# File lib/quando/parser.rb, line 78
def found?(date_part)
  !@date_parts[date_part.to_s].to_s.squeeze.empty?
end
valid_day?(value) click to toggle source

@param value [Integer]

# File lib/quando/parser.rb, line 93
def valid_day?(value)
  (1..31).include?(value.to_i)
end
valid_month?(value) click to toggle source

@param value [Integer]

# File lib/quando/parser.rb, line 88
def valid_month?(value)
  (1..12).include?(value.to_i)
end
wanted?(date_part) click to toggle source

@param date_part [Symbol]

# File lib/quando/parser.rb, line 83
def wanted?(date_part)
  @current_format.names.include?(date_part.to_s)
end