Sha256: 41508ed1cd48ac1fe0fb4c5c348371a75f1ae05ef38ee0389579731d2d6f9767

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require "stamp/version"
require "date"

module Stamp
  def self.included(klass)
    klass.class_eval do
      # extend ClassMethods
      include InstanceMethods
    end
  end

  # module ClassMethods
  # end

  module InstanceMethods
    MONTHNAMES_REGEXP = /(#{Date::MONTHNAMES.compact.join('|')})/i
    ABBR_MONTHNAMES_REGEXP = /(#{Date::ABBR_MONTHNAMES.compact.join('|')})/i

    def stamp(example)
      strftime(strftime_directives(example))
    end

    def strftime_directives(example)
      directives = []
      terms = example.split(/\b/)

      terms.each_with_index do |term, index|
        previous_term = (index == 0)            ? nil : terms[index - 1]
        next_term     = (index == terms.size-1) ? nil : terms[index + 1]

        directives << (strftime_directive(term, previous_term, next_term) || term)
      end

      directives.join
    end
    private :strftime_directives

    def strftime_directive(term, previous_term=nil, next_term=nil)
      case term
      when MONTHNAMES_REGEXP
        '%B'
      when ABBR_MONTHNAMES_REGEXP
        '%b'
      when /\d{4}/
        '%Y'
      when /\d{2}/
        '%d'
      when /\d{1}/
        '%e'
      end
    end
    private :strftime_directive
  end
end

Date.send(:include, ::Stamp)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stamp-0.0.1 lib/stamp.rb