class Serverkit::Loaders::BaseLoader

Constants

YAML_EXTNAMES

Public Class Methods

new(path) click to toggle source

@param [String] path

# File lib/serverkit/loaders/base_loader.rb, line 14
def initialize(path)
  @path = path
end

Public Instance Methods

load() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 18
def load
  if !pathname.exist?
    raise Errors::NonExistentPathError, pathname
  elsif has_directory_path?
    load_from_directory
  elsif has_erb_path?
    load_from_erb
  else
    load_from_data
  end
end

Private Instance Methods

binding_for_erb() click to toggle source

@return [Binding]

# File lib/serverkit/loaders/base_loader.rb, line 33
def binding_for_erb
  TOPLEVEL_BINDING
end
create_empty_loadable() click to toggle source

@note For override

# File lib/serverkit/loaders/base_loader.rb, line 38
def create_empty_loadable
  loaded_class.new({})
end
erb() click to toggle source

@return [ERB]

# File lib/serverkit/loaders/base_loader.rb, line 43
def erb
  _erb = ERB.new(pathname.read, trim_mode: "-")
  _erb.filename = pathname.to_s
  _erb
end
execute() click to toggle source

@return [String]

# File lib/serverkit/loaders/base_loader.rb, line 50
def execute
  `#{pathname}`
end
expand_erb() click to toggle source

@return [String]

# File lib/serverkit/loaders/base_loader.rb, line 55
def expand_erb
  erb.result(binding_for_erb)
end
expanded_erb_path_suffix() click to toggle source

@return [String]

# File lib/serverkit/loaders/base_loader.rb, line 60
def expanded_erb_path_suffix
  "." + pathname.basename(".erb").to_s.split(".", 2).last
end
expanded_erb_tempfile() click to toggle source

@note Memoizing to prevent GC @return [Tempfile]

# File lib/serverkit/loaders/base_loader.rb, line 66
def expanded_erb_tempfile
  @expanded_erb_tempfile ||= Tempfile.new(["", expanded_erb_path_suffix]).tap do |tempfile|
    tempfile << expand_erb
    tempfile.close
  end
end
has_directory_path?() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 73
def has_directory_path?
  pathname.directory?
end
has_erb_path?() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 77
def has_erb_path?
  pathname.extname == ".erb"
end
has_executable_path?() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 81
def has_executable_path?
  pathname.executable?
end
has_yaml_path?() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 85
def has_yaml_path?
  YAML_EXTNAMES.include?(pathname.extname)
end
load_data() click to toggle source

@return [Hash]

# File lib/serverkit/loaders/base_loader.rb, line 90
def load_data
  if has_executable_path?
    load_data_from_executable
  elsif has_yaml_path?
    load_data_from_yaml
  else
    load_data_from_json
  end
end
load_data_from_executable() click to toggle source

@return [Hash]

# File lib/serverkit/loaders/base_loader.rb, line 120
def load_data_from_executable
  JSON.parse(execute)
end
load_data_from_json() click to toggle source

@return [Hash]

# File lib/serverkit/loaders/base_loader.rb, line 125
def load_data_from_json
  JSON.parse(pathname.read)
end
load_data_from_yaml() click to toggle source

@return [Hash]

# File lib/serverkit/loaders/base_loader.rb, line 130
def load_data_from_yaml
  YAML.load_file(pathname)
end
load_from_data() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 100
def load_from_data
  loaded_class.new(load_data)
end
load_from_directory() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 104
def load_from_directory
  loads_from_directory.inject(create_empty_loadable, :merge)
end
load_from_erb() click to toggle source
# File lib/serverkit/loaders/base_loader.rb, line 108
def load_from_erb
  self.class.new(expanded_erb_tempfile.path).load
end
loads_from_directory() click to toggle source

@return [Array]

# File lib/serverkit/loaders/base_loader.rb, line 113
def loads_from_directory
  Dir.glob(pathname.join("*")).sort.flat_map do |path|
    self.class.new(path).load
  end
end
pathname() click to toggle source

@return [Pathname]

# File lib/serverkit/loaders/base_loader.rb, line 135
def pathname
  @pathname ||= Pathname.new(@path)
end