module NdrSupport

NdrSupport module

This defines the NdrSupport version. If you change it, rebuild and commit the gem. Use “rake build” to build the gem, see rake -T for all bundler rake tasks.

Constants

VERSION

Public Class Methods

apply_era_date_formats!() click to toggle source

Within the NDR, we change default date formatting, as below. This can cause problems with YAML emitted by syck, so we have to patch Date#to_yaml too.

# File lib/ndr_support/date_and_time_extensions.rb, line 105
def apply_era_date_formats!
  update_date_formats!
  update_time_formats!

  attempt_date_patch!
end
attempt_date_patch!() click to toggle source
# File lib/ndr_support/date_and_time_extensions.rb, line 112
def attempt_date_patch!
  # There are potential load order issues with this patch,
  # as it needs to be applied once syck has loaded.
  fail('Date#to_yaml must exist to be patched!') unless Date.respond_to?(:to_yaml)
  apply_date_patch!
end

Private Class Methods

apply_date_patch!() click to toggle source
# File lib/ndr_support/date_and_time_extensions.rb, line 121
def apply_date_patch!
  # Ensure we emit "yaml-formatted" string, instead of the revised default format.
  Psych::Visitors::YAMLTree.class_eval do
    def visit_Date(o) # rubocop:disable Naming/MethodName, Naming/MethodParameterName
      @emitter.scalar o.to_fs(:yaml), nil, nil, true, false, Psych::Nodes::Scalar::ANY
    end
  end
end
update_date_formats!() click to toggle source

Override default date and time formats:

# File lib/ndr_support/date_and_time_extensions.rb, line 131
def update_date_formats!
  Date::DATE_FORMATS.update(
    :db      => '%Y-%m-%d %H:%M:%S',
    :ui      => '%d.%m.%Y',
    :yaml    => '%Y-%m-%d', # For Dates
    :default => '%d.%m.%Y'
  )
end
update_time_formats!() click to toggle source

Rails 2 loads Oracle dates (with timestamps) as DateTime or Time values (before or after 1970) whereas Rails 1.2 treated them as Date objects. Therefore we have a formatting challenge, which we overcome by hiding the time if it’s exactly midnight

# File lib/ndr_support/date_and_time_extensions.rb, line 144
def update_time_formats!
  Time::DATE_FORMATS.update(
    :db      => '%Y-%m-%d %H:%M:%S',
    :ui      => '%d.%m.%Y %H:%M',
    :yaml    => '%Y-%m-%d %H:%M:%S %:z', # For DateTimes
    :default => lambda do |time|
      non_zero_time = time.hour != 0 || time.min != 0 || time.sec != 0
      time.strftime(non_zero_time ? '%d.%m.%Y %H:%M' : '%d.%m.%Y')
    end
  )
end
visit_Date(o) click to toggle source
# File lib/ndr_support/date_and_time_extensions.rb, line 124
def visit_Date(o) # rubocop:disable Naming/MethodName, Naming/MethodParameterName
  @emitter.scalar o.to_fs(:yaml), nil, nil, true, false, Psych::Nodes::Scalar::ANY
end