module Shaf::Utils

Constants

SHAF_VERSION_FILE

Public Class Methods

deep_symbolize_keys(value) click to toggle source
# File lib/shaf/utils.rb, line 67
def deep_symbolize_keys(value)
  deep_transform_keys(value) { |key| key.to_sym }
end
deep_transform_keys(hash, &block) click to toggle source
# File lib/shaf/utils.rb, line 71
def deep_transform_keys(hash, &block)
  case hash
  when Hash
    hash.each_with_object({}) do |(k, v), h|
      key = block.call(k)
      h[key] = deep_transform_keys(v, &block)
    end
  when Array
    hash.map { |v| deep_transform_keys(v, &block) }
  else
    hash
  end
end
deep_transform_values(hash, &block) click to toggle source
# File lib/shaf/utils.rb, line 85
def deep_transform_values(hash, &block)
  case hash
  when Hash
    hash.each_with_object({}) do |(k, v), h|
      h[k] = deep_transform_values(v, &block)
    end
  when Array
    hash.map { |v| deep_transform_values(v, &block) }
  else
    block.call(hash)
  end
end
environment() click to toggle source
# File lib/shaf/utils.rb, line 55
def environment
  require 'sinatra/base'
  Sinatra::Application.settings.environment
end
gem_root() click to toggle source
# File lib/shaf/utils.rb, line 23
def gem_root
  File.expand_path('../..', __dir__)
end
model_name(name) click to toggle source
# File lib/shaf/utils.rb, line 19
def model_name(name)
  name.capitalize.gsub(/[_-](\w)/) { $1.upcase }
end
pluralize(noun) click to toggle source

FIXME!!!

# File lib/shaf/utils.rb, line 28
def pluralize(noun)
  return pluralize(noun.to_s).to_sym if noun.is_a? Symbol
  noun + 's'
end
rackify_header(str) click to toggle source
# File lib/shaf/utils.rb, line 60
def rackify_header(str)
  return if str.nil?
  str.upcase.tr('-', '_').tap do |key|
    key.prepend('HTTP_') unless key.start_with? 'HTTP_'
  end
end
read_config(file, erb: false, erb_binding: nil) click to toggle source
# File lib/shaf/utils.rb, line 98
def read_config(file, erb: false, erb_binding: nil)
  return {} unless File.exist? file

  yaml = File.read(file)
  yaml = erb(yaml, binding: erb_binding) if erb || erb_binding
  if RUBY_VERSION < '2.6.0'
    deep_symbolize_keys(YAML.safe_load(yaml, [], [], true))
  else
    YAML.safe_load(yaml, aliases: true, symbolize_names: true)
  end
end
singularize(noun) click to toggle source

FIXME!!!

# File lib/shaf/utils.rb, line 34
def singularize(noun)
  return singularize(noun.to_s).to_sym if noun.is_a? Symbol
  return noun unless noun.end_with? 's'
  noun[0..-2]
end
symbol_or_quoted_string(obj) click to toggle source
# File lib/shaf/utils.rb, line 44
def symbol_or_quoted_string(obj)
  case obj
  when Symbol
    obj.inspect
  when Numeric
    obj
  else
    "'#{obj.to_s}'"
  end
end
symbol_string(str) click to toggle source
# File lib/shaf/utils.rb, line 40
def symbol_string(str)
  str.to_sym.inspect
end

Private Class Methods

erb(content, binding: nil) click to toggle source
# File lib/shaf/utils.rb, line 117
def erb(content, binding: nil)
  bindings = binding ? [binding] : []
  return ERB.new(content, 0, '%-<>').result(*bindings) if RUBY_VERSION < '2.6.0'
  ERB.new(content, trim_mode: '-<>').result(*bindings)
end

Public Instance Methods

bootstrap(env: 'development') { || ... } click to toggle source
# File lib/shaf/utils.rb, line 155
def bootstrap(env: 'development')
  in_project_root do
    ENV['RACK_ENV'] = env
    require 'config/bootstrap'
    yield if block_given?
  end
end
in_project_root() { || ... } click to toggle source
# File lib/shaf/utils.rb, line 147
def in_project_root
  return unless block_given?
  Dir.chdir(project_root!) do
    $:.unshift Dir.pwd
    yield
  end
end
in_project_root?() click to toggle source
# File lib/shaf/utils.rb, line 143
def in_project_root?
  is_project_root?(Dir.pwd)
end
is_project_root?(dir) click to toggle source
# File lib/shaf/utils.rb, line 139
def is_project_root?(dir)
  File.exist? File.expand_path(".shaf", dir)
end
project_root() click to toggle source
# File lib/shaf/utils.rb, line 126
def project_root
  return @project_root if defined? @project_root
  dir = Dir.pwd
  while dir != '/' do
    return @project_root = dir if is_project_root?(dir)
    dir = File.expand_path("..", dir)
  end
end
project_root!() click to toggle source
# File lib/shaf/utils.rb, line 135
def project_root!
  project_root or raise ProjectRootNotFound
end
read_shaf_file() click to toggle source
# File lib/shaf/utils.rb, line 163
def read_shaf_file
  return {} unless File.exist? SHAF_VERSION_FILE
  str = File.read(SHAF_VERSION_FILE)
  YAML.load(str) || {}
end
read_shaf_file!() click to toggle source
# File lib/shaf/utils.rb, line 169
def read_shaf_file!
  in_project_root do
    read_shaf_file
  end
end
read_shaf_upgrade_failure_version() click to toggle source
# File lib/shaf/utils.rb, line 188
def read_shaf_upgrade_failure_version
  read_shaf_file['failed_upgrade']
end
read_shaf_version() click to toggle source
# File lib/shaf/utils.rb, line 184
def read_shaf_version
  read_shaf_file['version']
end
write_shaf_file(data = {}) click to toggle source
# File lib/shaf/utils.rb, line 175
def write_shaf_file(data = {})
  write_shaf_file! read_shaf_file.merge(data)
end
write_shaf_file!(data = {}) click to toggle source
# File lib/shaf/utils.rb, line 179
def write_shaf_file!(data = {})
  File.write SHAF_VERSION_FILE,
    YAML.dump(data)
end
write_shaf_upgrade_failure(version) click to toggle source
# File lib/shaf/utils.rb, line 199
def write_shaf_upgrade_failure(version)
  write_shaf_file 'failed_upgrade' => version.to_s
end
write_shaf_version(version = nil) click to toggle source
# File lib/shaf/utils.rb, line 192
def write_shaf_version(version = nil)
  version ||= Shaf::VERSION
  data = read_shaf_file.merge('version' => version.to_s)
  data.delete('failed_upgrade')
  write_shaf_file! data
end