Sha256: 12a91ecf823dc1f4616797e7b66aee7987f7d82d437891daa3634b4655a629b7

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require "date"
require "time"

require "stamp/emitters/modifiable"
require "stamp/emitters/am_pm"
require "stamp/emitters/ambiguous"
require "stamp/emitters/composite"
require "stamp/emitters/delegate"
require "stamp/emitters/lookup"
require "stamp/emitters/ordinal"
require "stamp/emitters/string"
require "stamp/emitters/two_digit"
require "stamp/disambiguator"
require "stamp/translator"
require "stamp/version"

module Stamp
  # Formats a date/time using a human-friendly example as a template.
  #
  # @param [String] example a human-friendly date/time example
  # @param [Hash] options
  # @option options [Boolean] :memoize (true)
  #
  # @return [String] the formatted date or time
  #
  # @example
  #   Date.new(2012, 12, 21).stamp("Jan 1, 1999") #=> "Dec 21, 2012"
  def stamp(example)
    memoize_stamp_emitters(example).format(self)
  end
  alias :stamp_like  :stamp
  alias :format_like :stamp

  private

  # Memoizes the set of emitter objects for the given +example+ in
  # order to improve performance.
  def memoize_stamp_emitters(example)
    @@memoized_stamp_emitters ||= {}
    @@memoized_stamp_emitters[example] ||= stamp_emitters(example)
  end

  def stamp_emitters(example)
    emitters = Translator.new.translate(example)
    Disambiguator.new(emitters).disambiguate!
  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stamp-0.6.0 lib/stamp.rb