Class: Dater::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/dater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = '%Y-%m-%d', lang = "en", today_for_nil = false) ⇒ Resolver

Creates a Dater::Resolver object

Parameters:

  • format (String) (defaults to: '%Y-%m-%d')

    date format

  • lang (String) (defaults to: "en")

    languaje for matching (en=english, es=spanish, pt=portuguese)

  • today_for_nil (Boolean) (defaults to: false)

    Indicates if must return today’s date if given argument is nil



16
17
18
19
20
# File 'lib/dater.rb', line 16

def initialize(format='%Y-%m-%d', lang="en", today_for_nil=false)
	@today_for_nil=today_for_nil
	@format=format
	@lang=lang if ["en","es","pt"].include? lang 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object (private)

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



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/dater.rb', line 382

def method_missing(meth)
	# if meth.to_s =~ /^(next_|próximo_|proximo_|last_|último_|ultimo_|in_|\d_|for).+$/i
	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

Instance Attribute Details

#formatObject

Returns the value of attribute format.



8
9
10
# File 'lib/dater.rb', line 8

def format
  @format
end

#langObject

Returns the value of attribute lang.



8
9
10
# File 'lib/dater.rb', line 8

def lang
  @lang
end

Instance Method Details

#for(period = nil) ⇒ String

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

Parameters:

  • period (String) (defaults to: nil)

    a period of time expreseed in a literal way to convert to the configured format (@format)

Returns:

  • (String)

    converted date to the configured format. If period is nil and @today_for_nil is true, returns date for tomorrow. Else returns nil



28
29
30
31
32
33
34
35
36
# File 'lib/dater.rb', line 28

def for(period=nil)
	if period.nil? or period == ""
		period = now.strftime(@format) if today_for_nil
		return period
	else
			@last_date = @date = time_for_period(period)
		@date.strftime(@format) if @date.respond_to? :strftime
	end
end

#para(period) ⇒ Object

Spanish and portuguese equivalent for ‘for’ method



40
41
42
# File 'lib/dater.rb', line 40

def para(period)
	self.for(period)
end