Module: Doh

Extended by:
Doh
Included in:
Doh
Defined in:
lib/dohutil/password_helper.rb,
lib/dohutil/env.rb,
lib/dohutil/config.rb,
lib/dohutil/to_display.rb,
lib/dohutil/current_date.rb,
lib/dohutil/array_to_hash.rb,
lib/dohutil/class_basename.rb,
lib/dohutil/disable_verbose.rb

Overview

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

Defined Under Namespace

Classes: PasswordHelper

Constant Summary collapse

ENV_PREFIXES =
%w(DOH RACK RAILS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_to_hash(ary) ⇒ Object



3
4
5
6
7
8
9
10
# 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) ⇒ Object



3
4
5
6
7
8
# 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

Instance Method Details

#clear_current_dateObject



27
28
29
# File 'lib/dohutil/current_date.rb', line 27

def clear_current_date
  @current_date_stack.clear
end

#configObject



13
14
15
# File 'lib/dohutil/config.rb', line 13

def config
  @config ||= {}
end

#current_date(dflt = Date.today) ⇒ Object



7
8
9
10
# 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_stackObject



31
32
33
# File 'lib/dohutil/current_date.rb', line 31

def current_date_stack
  @current_date_stack
end

#current_datetime(dflt = DateTime.zow) ⇒ Object



12
13
14
15
16
17
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



35
36
37
# 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_verboseObject



4
5
6
7
8
9
10
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
# 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) ⇒ Object



63
64
65
66
# 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

#envObject



5
6
7
# File 'lib/dohutil/env.rb', line 5

def env
  @env ||= find_env
end

#find_envObject



9
10
11
12
13
14
15
# 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) ⇒ Object



17
18
19
20
# 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) ⇒ Object



6
7
8
9
10
11
# 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_dateObject



23
24
25
# File 'lib/dohutil/current_date.rb', line 23

def pop_current_date
  @current_date_stack.pop
end

#push_current_date(date_obj) ⇒ Object



19
20
21
# 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) ⇒ Object

keep the current date, but set the time



50
51
52
53
54
55
56
# 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