Sha256: a1a2b181acf95f74ef412a76f301380bf5ab2e47536382b71d23c5f89921220c

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require "date"
require "time"

require "stamp/translator"
require "stamp/version"

module Stamp

  # Transforms the given example dates/time format to a format string
  # suitable for strftime.
  #
  # @param  [String] example a human-friendly date/time example
  # @param  [#strftime] the Date or Time to be formatted. Optional, but may
  #                     be used to support certain edge cases
  # @return [String] a strftime-friendly format
  #
  # @example
  #   Stamp.strftime_format("Jan 1, 1999") #=> "%b %e, %Y"
  def self.strftime_format(example, target=nil)
    Stamp::StrftimeTranslator.new(target).translate(example)
  end

  # Formats a date/time using a human-friendly example as a template.
  #
  # @param  [String] example a human-friendly date/time example
  # @return [String] the formatted date or time
  #
  # @example
  #   Date.new(2012, 12, 21).stamp("Jan 1, 1999") #=> "Dec 21, 2012"
  def stamp(example)
    strftime(strftime_format(example))
  end
  alias :stamp_like  :stamp
  alias :format_like :stamp

  # Transforms the given example date/time format to a format string
  # suitable for strftime.
  #
  # @param  [String] example a human-friendly date/time example
  # @return [String] a strftime-friendly format
  #
  # @example
  #   Date.today.strftime_format("Jan 1, 1999") #=> "%b %e, %Y"
  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

end

Date.send(:include, ::Stamp)
Time.send(:include, ::Stamp)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stamp-0.4.0 lib/stamp.rb
stamp-0.3.0 lib/stamp.rb
stamp-0.2.0 lib/stamp.rb
stamp-0.1.6 lib/stamp.rb