class Dater::Resolver

Constants

TIME_IN_SECONDS
WEEKDAYS

Attributes

format[RW]

Public Class Methods

new(format='%Y-%m-%d', lang="en", today_for_nil=false) click to toggle source

Creates a Dater::Resolver object

@param [String] format = date format @param [String] lang = languaje for matching (en=english, es=spanish, pt=portuguese) @param [Boolean] today_for_nil = Indicates if must return today's date if given argument is nil

# File lib/dater.rb, line 17
def initialize(format='%Y-%m-%d', lang="en", today_for_nil=false)
        @today_for_nil=today_for_nil
        @format=format
        @translate=Dater::Translator.new(lang) 
end

Public Instance Methods

for(period=nil) click to toggle source

Convert the period of time passed as argument to the configured format

@param [String] period = a period of time expreseed in a literal way to convert to the configured format (@format) @return [String] converted date to the configured format. If period is nil and @today_for_nil is true, returns date for tomorrow. Else returns nil

# File lib/dater.rb, line 29
def for(period=nil)
        if period.nil? or period == ""
                period = now.strftime(@format) if today_for_nil
                return period
        else
                translated = @translate.this period
                @last_date = @date = time_for_period(translated)
                @date.strftime(@format) if @date.respond_to? :strftime
        end
end
lang=(lang) click to toggle source
# File lib/dater.rb, line 46
def lang= lang
        @translate=Dater::Translator.new(lang) 
end
para(period) click to toggle source

Spanish and portuguese equivalent for 'for' method

# File lib/dater.rb, line 42
def para(period)
        self.for(period)
end

Private Instance Methods

a_day_backward() click to toggle source

Returns the amount in seconds for a day (negative)

# File lib/dater.rb, line 193
def a_day_backward
        -TIME_IN_SECONDS[:day]
end
a_day_forward() click to toggle source

Returns the amount in seconds for a day (positive)

# File lib/dater.rb, line 188
def a_day_forward
        TIME_IN_SECONDS[:day]
end
day_mult(period) click to toggle source

Multiplication factor for a day

@param [String] period @return [Fixnum] multiplication factor for a day

# File lib/dater.rb, line 209
def day_mult(period)
        TIME_IN_SECONDS[:day] if is_day? period               
end
extract_day_from(word) click to toggle source
# File lib/dater.rb, line 165
def extract_day_from word
        WEEKDAYS.select{ |day| day if day==word.scan(/[a-zA-Z]+/).last }.join
end
is_day?(period) click to toggle source

Scans if period has day word

@param [String] period @return [Boolean] true if perdiod contains the word day

# File lib/dater.rb, line 201
def is_day?(period)
        period.scan(/day/i).size > 0
end
is_month?(period) click to toggle source

Scans if period has month word

@param [String] period @return [Boolean] true if perdiod contains the word month

# File lib/dater.rb, line 233
def is_month?(period)
        period.scan(/month/).size > 0
end
is_required_day?(time, day) click to toggle source

Method to know if the day is the required day

@param [Time] time @param [String] day = to match in case statement @return [Boolean] = true if day is the required day

# File lib/dater.rb, line 174
def is_required_day?(time, day)
        day_to_ask = "#{day}?"
        result = eval("time.#{day_to_ask}") if time.respond_to? day_to_ask.to_sym
        return result
end
is_week?(period) click to toggle source

Scans if period has week word

@param [String] period @return [Boolean] true if perdiod contains the word week

# File lib/dater.rb, line 217
def is_week?(period)
        period.scan(/week/i).size > 0
end
is_year?(period) click to toggle source

Scans if period string contain year word

@param [String] period to scan @return [Boolean] true if perdiod contains the word year

# File lib/dater.rb, line 249
def is_year?(period)
        period.scan(/year/i).size > 0
end
last(period) click to toggle source

Returns one week/month/year of difference from today's date. Formatted or not according to formatted param

@param [String] period = the factor of difference (day, week, month, year) from today's date @return [Time]

# File lib/dater.rb, line 146
def last(period)
        Time.now - multiply_by(period)
end
method_missing(meth) click to toggle source

Try to convert Missing methods to string and call to for method with converted string

# File lib/dater.rb, line 293
def method_missing(meth)
        self.class.send :define_method, meth do
                string = meth.to_s.gsub("_"," ")
                self.for("for('#{string}')")
        end
        begin
                self.send(meth.to_s)
        rescue
                raise "Method does not exists (#{meth})."
        end
end
month_mult(period) click to toggle source

Multiplication factor for a month

@param [String] period @return [Fixnum] multiplication factor for a month

# File lib/dater.rb, line 241
def month_mult(period)
        TIME_IN_SECONDS[:month] if is_month? period
end
move_a_day(word) click to toggle source

Return a day to add or substrac according to the given word Substract if word contains 'last' word @return +/-[Fixnum] time in seconds for a day (+/-)

# File lib/dater.rb, line 183
def move_a_day(word)
        word.scan(/last/i).size > 0 ? a_day_backward : a_day_forward
end
move_to(word) click to toggle source

Returns the number of seconds for the given string to add or substract according to argument If argument has the word 'last' it goes backward, else forward

# File lib/dater.rb, line 152
def move_to(word)
        word.scan(/last/i).size>0 ? self.last(word) : self.next(word)
end
multiply_by(string) click to toggle source

Returns seconds to multiply by for the given string

@param [String] period = the period of time expressed in a literal way @return [Fixnum] number to multiply by

# File lib/dater.rb, line 265
def multiply_by(string)
        return day_mult(string) || week_mult(string) || month_mult(string) || year_mult(string) || 1
end
next(period) click to toggle source

Returns one week/month/year of difference from today's date. Formatted or not according to formatted param

@param [String] period = the factor of difference (day, week, month, year) from today's date @return [Time]

# File lib/dater.rb, line 138
def next(period)
        Time.now + multiply_by(period)
end
now() click to toggle source

Returns now time @return [Time] @last_date = today's time

# File lib/dater.rb, line 121
def now
        @last_date=Time.now
end
period_of_time_from_string(word) click to toggle source

Returns the time according to the given string

@param [String] word = period of time in literal way @return [Fixnum] multiplication factor (seconds)

# File lib/dater.rb, line 286
def period_of_time_from_string(word)
        word.scan(/\d+/)[0].to_i * multiply_by(word)
end
time_for_period(string=nil) click to toggle source

Returns the formatted date according to the given period of time expresed in a literal way

@param [String] period = time expressed literally (e.g: in 2 days) @return [String] formatted time

# File lib/dater.rb, line 68
def time_for_period(string=nil)
        
        
        @last_date = case string
        
        when /today/,/now/
                now

        when /tomorrow/
                tomorrow_time

        when /yesterday/
                yesterday_time

        when /sunday/, /monday/, /tuesday/, /wednesday/, /thursday/, /friday/, /saturday/                            
                time_for_weekday(string)

        when /next/
                now + period_of_time_from_string(string.gsub("next","1"))

        when /last/
                now - period_of_time_from_string(string.gsub("last","1"))                           

        when /\d[\sa-zA-Z]+\sbefore/
                @last_date ||= now 
                @last_date -=  period_of_time_from_string(string)

        when /\d[\sa-zA-Z]+\sago/
                @last_date  ||= now 
                now - period_of_time_from_string(string)

        when /\d[\sa-zA-Z]+\slater/
                @last_date ||= now
                @last_date +=  period_of_time_from_string(string)

        when /in/,/\d\sdays?/, /\d\smonths?/, /\d\sweeks?/, /\d\syears?/
                now + period_of_time_from_string(string)

        when /\d+.\d+.\d+/
                time_from_date(string)      

        when /rand/,/future/
                now + rand(100_000_000)

        when /past/
                now - rand(100_000_000)
        end
                
                return @last_date
end
time_for_weekday(word) click to toggle source
# File lib/dater.rb, line 156
def time_for_weekday(word)
        day = extract_day_from word
        @count = Time.now
        begin
                @count+= move_a_day(word)
        end until is_required_day?(@count, day)
        @count
end
time_from_date(date) click to toggle source

Return the Time object according to the splitted date in the given array

@param [String] date @return [Time]

# File lib/dater.rb, line 275
def time_from_date(date)
        numbers=date.scan(/\d+/).map!{|i| i.to_i}
        day=numbers[2-numbers.index(numbers.max)]
        Date.new(numbers.max,numbers[1],day).to_time 
end
tomorrow_time() click to toggle source
# File lib/dater.rb, line 129
def tomorrow_time
        @last_date = Time.now + TIME_IN_SECONDS[:day]
end
week_mult(period) click to toggle source

Multiplication factor for a week

@param [String] period @return [Fixnum] multiplication factor for a week

# File lib/dater.rb, line 225
def week_mult(period)
        TIME_IN_SECONDS[:week] if is_week? period
end
year_mult(period) click to toggle source

Multiplication factor for a year

@param [String] period = the string to convert to @return [Fixnum] multiplication factor for a year

# File lib/dater.rb, line 257
def year_mult(period)
        TIME_IN_SECONDS[:year] if is_year? period
end
yesterday_time() click to toggle source
# File lib/dater.rb, line 125
def yesterday_time
        @last_date = Time.now - TIME_IN_SECONDS[:day]
end