Sha256: b53a2abe423352a83386338aa734f3ff72bc1cbf7ccb341fa39a17c3da74b118
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
require "date" require "time" require "stamp/emitters/modifiable" require "stamp/emitters/am_pm" 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/translator" require "stamp/version" module Stamp # Limits the number of formats that we memoize to prevent unbounded # memory consumption. MEMOIZATION_CAP = 999 # 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 # 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.clear if @@memoized_stamp_emitters.size > MEMOIZATION_CAP @@memoized_stamp_emitters[example] ||= stamp_emitters(example) end def stamp_emitters(example) Translator.new.translate(example) end end Date.send(:include, ::Stamp) Time.send(:include, ::Stamp)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
stamp-0.5.0 | lib/stamp.rb |