Sha256: d05439378a2faa05e7be00ab19c13a5faa3aa41e7dc6a427fde7fcef2c29f006

Contents?: true

Size: 641 Bytes

Versions: 5

Compression:

Stored size: 641 Bytes

Contents

module Vernacular
  module Modifiers
    # Extends Ruby syntax to allow date sigils, or ~d(...). The date inside is
    # parsed and as an added benefit if it is a set value it is replaced with
    # the more efficient `strptime`.
    class DateSigil < RegexModifier
      FORMAT = '%FT%T%:z'.freeze

      def initialize
        super(/~d\((.+?)\)/) do |match|
          content = match[3..-2]
          begin
            date = Date.parse(content)
            "Date.strptime('#{date.strftime(FORMAT)}', '#{FORMAT}')"
          rescue ArgumentError
            "Date.parse(#{content})"
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vernacular-0.1.2 lib/vernacular/modifiers/date_sigil.rb
vernacular-0.1.1 lib/vernacular/modifiers/date_sigil.rb
vernacular-0.1.0 lib/vernacular/modifiers/date_sigil.rb
vernacular-0.0.2 lib/vernacular/modifiers/date_sigil.rb
vernacular-0.0.1 lib/vernacular/modifiers/date_sigil.rb