module Doh

thin wrapper around HighLine, in case we decide to use something else in the future

Constants

ENV_PREFIXES

Public Class Methods

array_to_hash(ary) { |elem| ... } click to toggle source
# File lib/dohutil/array_to_hash.rb, line 3
def self.array_to_hash(ary)
  retval = {}
  ary.each do |elem|
    key, value = yield(elem)
    retval[key] = value
  end
  retval
end
class_basename(klass) click to toggle source
# File lib/dohutil/class_basename.rb, line 3
def self.class_basename(klass)
  full_name = klass.to_s
  retval = full_name.rpartition('::').last
  retval = full_name if retval.empty?
  retval
end

Public Instance Methods

clear_current_date() click to toggle source
# File lib/dohutil/current_date.rb, line 27
def clear_current_date
  @current_date_stack.clear
end
config() click to toggle source
# File lib/dohutil/config.rb, line 13
def config
  @config ||= {}
end
current_date(dflt = Date.today) click to toggle source
# File lib/dohutil/current_date.rb, line 7
def current_date(dflt = Date.today)
  return dflt if @current_date_stack.empty?
  @current_date_stack.last.to_date
end
current_date_stack() click to toggle source
# File lib/dohutil/current_date.rb, line 31
def current_date_stack
  @current_date_stack
end
current_datetime(dflt = DateTime.zow) click to toggle source
# File lib/dohutil/current_date.rb, line 12
def current_datetime(dflt = DateTime.zow)
  return dflt if @current_date_stack.empty?
  cdo = @current_date_stack.last
  return cdo if cdo.respond_to?(:hour)
  DateTime.new(cdo.year, cdo.month, cdo.day, dflt.hour, dflt.min, dflt.sec, dflt.zone)
end
date_as(date_obj) { || ... } click to toggle source
# File lib/dohutil/current_date.rb, line 39
def date_as(date_obj)
  push_current_date(date_obj)
  begin
    retval = yield
  ensure
    pop_current_date
  end
  retval
end
date_as_start_of_day(date_obj, &block) click to toggle source
# File lib/dohutil/current_date.rb, line 35
def date_as_start_of_day(date_obj, &block)
  date_as(DateTime.new(date_obj.year, date_obj.month, date_obj.day, 0, 0, 0), &block)
end
disable_verbose() { || ... } click to toggle source
# File lib/dohutil/disable_verbose.rb, line 4
def disable_verbose
  old_verbose = $VERBOSE
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = old_verbose
end
display_phone(str, html_format = false) click to toggle source
# File lib/dohutil/to_display.rb, line 48
def display_phone(str, html_format = false)
  return str unless str.to_s.size == 10
  formatted = "#{str[0..2]}-#{str[3..5]}-#{str[6..9]}"
  if html_format
    "<a href='callto:+1#{formatted}'>#{formatted}</a>"
  else
    formatted
  end
end
display_postal(str) click to toggle source
# File lib/dohutil/to_display.rb, line 58
def display_postal(str)
  return str unless str.to_s.size == 9
  return str[0..4] + '-' + str[5..8]
end
display_ssn(str) click to toggle source
# File lib/dohutil/to_display.rb, line 63
def display_ssn(str)
  return str unless str.to_s.size == 9
  str[0..2] + '-' + str[3..4] + '-' + str[5..8]
end
env() click to toggle source
# File lib/dohutil/env.rb, line 5
def env
  @env ||= find_env
end
find_env() click to toggle source
# File lib/dohutil/env.rb, line 9
def find_env
  ENV_PREFIXES.each do |prefix|
    denval = ENV["#{prefix}_ENV"]
    return denval if denval
  end
  raise 'please set DOH_ENV, RACK_ENV or RAILS_ENV'
end
get_required_config_value(value, desc) click to toggle source
# File lib/dohutil/config.rb, line 17
def get_required_config_value(value, desc)
  raise "Attempt to get configuration value: #{value.inspect}, but none exists.  #{desc}" if !config.key?(value)
  config[value]
end
load_config_file(name) click to toggle source
# File lib/dohutil/config.rb, line 6
def load_config_file(name)
  path = File.join(Doh.root, 'config', name) + '.rb'
  return false unless File.exist?(path)
  require(path)
  true
end
pop_current_date() click to toggle source
# File lib/dohutil/current_date.rb, line 23
def pop_current_date
  @current_date_stack.pop
end
push_current_date(date_obj) click to toggle source
# File lib/dohutil/current_date.rb, line 19
def push_current_date(date_obj)
  @current_date_stack.push(date_obj)
end
time_as(hour, minute, second) { || ... } click to toggle source

keep the current date, but set the time

# File lib/dohutil/current_date.rb, line 50
def time_as(hour, minute, second)
  cdo = Doh.current_datetime
  new_time = DateTime.new(cdo.year, cdo.month, cdo.day, hour, minute, second, cdo.zone)
  date_as(new_time) do
    yield
  end
end