class Serverkit::Loaders::BaseLoader
Constants
- YAML_EXTNAMES
Public Class Methods
Source
# File lib/serverkit/loaders/base_loader.rb, line 16 def initialize(path) @path = path end
@param [String] path
Public Instance Methods
Source
# File lib/serverkit/loaders/base_loader.rb, line 20 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
Source
# File lib/serverkit/loaders/base_loader.rb, line 35 def binding_for_erb TOPLEVEL_BINDING end
@return [Binding]
Source
# File lib/serverkit/loaders/base_loader.rb, line 40 def create_empty_loadable loaded_class.new({}) end
@note For override
Source
# File lib/serverkit/loaders/base_loader.rb, line 45 def erb _erb = ERB.new(pathname.read, trim_mode: "-") _erb.filename = pathname.to_s _erb end
@return [ERB]
Source
# File lib/serverkit/loaders/base_loader.rb, line 52 def execute `#{pathname}` end
@return [String]
Source
# File lib/serverkit/loaders/base_loader.rb, line 57 def expand_erb erb.result(binding_for_erb) end
@return [String]
Source
# File lib/serverkit/loaders/base_loader.rb, line 62 def expanded_erb_path_suffix ".#{pathname.basename('.erb').to_s.split('.', 2).last}" end
@return [String]
Source
# File lib/serverkit/loaders/base_loader.rb, line 68 def expanded_erb_tempfile @expanded_erb_tempfile ||= Tempfile.new(["", expanded_erb_path_suffix]).tap do |tempfile| tempfile << expand_erb tempfile.close end end
@note Memoizing to prevent GC @return [Tempfile]
Source
# File lib/serverkit/loaders/base_loader.rb, line 75 def has_directory_path? pathname.directory? end
Source
# File lib/serverkit/loaders/base_loader.rb, line 79 def has_erb_path? pathname.extname == ".erb" end
Source
# File lib/serverkit/loaders/base_loader.rb, line 83 def has_executable_path? pathname.executable? end
Source
# File lib/serverkit/loaders/base_loader.rb, line 87 def has_yaml_path? YAML_EXTNAMES.include?(pathname.extname) end
Source
# File lib/serverkit/loaders/base_loader.rb, line 92 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
@return [Hash]
Source
# File lib/serverkit/loaders/base_loader.rb, line 122 def load_data_from_executable JSON.parse(execute) end
@return [Hash]
Source
# File lib/serverkit/loaders/base_loader.rb, line 127 def load_data_from_json JSON.parse(pathname.read) end
@return [Hash]
Source
# File lib/serverkit/loaders/base_loader.rb, line 132 def load_data_from_yaml YAML.load_file(pathname) end
@return [Hash]
Source
# File lib/serverkit/loaders/base_loader.rb, line 102 def load_from_data loaded_class.new(load_data) end
Source
# File lib/serverkit/loaders/base_loader.rb, line 106 def load_from_directory loads_from_directory.inject(create_empty_loadable, :merge) end
Source
# File lib/serverkit/loaders/base_loader.rb, line 110 def load_from_erb self.class.new(expanded_erb_tempfile.path).load end
Source
# File lib/serverkit/loaders/base_loader.rb, line 115 def loads_from_directory Dir.glob(pathname.join("*")).sort.flat_map do |path| self.class.new(path).load end end
@return [Array]
Source
# File lib/serverkit/loaders/base_loader.rb, line 137 def pathname @pathname ||= Pathname.new(@path) end
@return [Pathname]