Class: Vernacular::Modifiers::DateSigil

Inherits:
RegexModifier show all
Defined in:
lib/vernacular/modifiers/date_sigil.rb

Overview

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`.

Constant Summary collapse

FORMAT =
'%FT%T%:z'

Instance Attribute Summary

Attributes inherited from RegexModifier

#block, #pattern, #replacement

Instance Method Summary collapse

Methods inherited from RegexModifier

#components, #modify

Constructor Details

#initializeDateSigil

Returns a new instance of DateSigil.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vernacular/modifiers/date_sigil.rb', line 11

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