Class: Dater::Resolver
- Inherits:
-
Object
- Object
- Dater::Resolver
- Defined in:
- lib/dater.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#lang ⇒ Object
Returns the value of attribute lang.
Instance Method Summary collapse
-
#for(period = nil) ⇒ String
Convert the period of time passed as argument to the configured format.
-
#initialize(format = '%Y-%m-%d', lang = "en", today_for_nil = false) ⇒ Resolver
constructor
Creates a Dater::Resolver object.
-
#para(period) ⇒ Object
Spanish and portuguese equivalent for ‘for’ method.
Constructor Details
#initialize(format = '%Y-%m-%d', lang = "en", today_for_nil = false) ⇒ Resolver
Creates a Dater::Resolver object
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
#format ⇒ Object
Returns the value of attribute format.
8 9 10 |
# File 'lib/dater.rb', line 8 def format @format end |
#lang ⇒ Object
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
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 |