Module: Stamp

Defined in:
lib/stamp.rb,
lib/stamp/version.rb,
lib/stamp/translator.rb

Defined Under Namespace

Classes: StrftimeTranslator

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strftime_format(example, target = nil) ⇒ String

Transforms the given example dates/time format to a format string suitable for strftime.

Examples:

Stamp.strftime_format("Jan 1, 1999") #=> "%b %e, %Y"

Parameters:

  • example (String)

    a human-friendly date/time example

  • the (#strftime)

    Date or Time to be formatted. Optional, but may be used to support certain edge cases

Returns:

  • (String)

    a strftime-friendly format



19
20
21
# File 'lib/stamp.rb', line 19

def self.strftime_format(example, target=nil)
  Stamp::StrftimeTranslator.new(target).translate(example)
end

Instance Method Details

#stamp(example) ⇒ String Also known as: stamp_like, format_like

Formats a date/time using a human-friendly example as a template.

Examples:

Date.new(2012, 12, 21).stamp("Jan 1, 1999") #=> "Dec 21, 2012"

Parameters:

  • example (String)

    a human-friendly date/time example

Returns:

  • (String)

    the formatted date or time



30
31
32
# File 'lib/stamp.rb', line 30

def stamp(example)
  strftime(strftime_format(example))
end

#strftime_format(example) ⇒ String

Transforms the given example date/time format to a format string suitable for strftime.

Examples:

Date.today.strftime_format("Jan 1, 1999") #=> "%b %e, %Y"

Parameters:

  • example (String)

    a human-friendly date/time example

Returns:

  • (String)

    a strftime-friendly format



44
45
46
47
48
# File 'lib/stamp.rb', line 44

def strftime_format(example)
  # delegate to the class method, providing self as a target value to
  # support certain edge cases
  Stamp.strftime_format(example, self)
end