class Djin::IncludeConfigLoader

Public Class Methods

load!(include_djin_config, **options) click to toggle source
# File lib/djin/include_config_loader.rb, line 8
def self.load!(include_djin_config, **options)
  new(**options).load!(include_djin_config)
end
new(base_directory: '.', remote_directory: '~/.djin/remote', entity_class: Djin::IncludeConfig) click to toggle source
# File lib/djin/include_config_loader.rb, line 12
def initialize(base_directory: '.', remote_directory: '~/.djin/remote', entity_class: Djin::IncludeConfig)
  # TODO: Use chain of responsability
  @base_directory = Pathname.new(base_directory)
  @remote_directory = Pathname.new(remote_directory)
  @entity_class = entity_class
end

Public Instance Methods

load!(include_djin_config) click to toggle source
# File lib/djin/include_config_loader.rb, line 19
def load!(include_djin_config)
  load_configs(include_djin_config)
end

Private Instance Methods

build_entity(params) click to toggle source
# File lib/djin/include_config_loader.rb, line 68
def build_entity(params)
  @entity_class.new(**params.symbolize_keys)
end
load_configs(include_djin_config) click to toggle source
# File lib/djin/include_config_loader.rb, line 25
def load_configs(include_djin_config)
  include_djin_config ||= []

  include_contract = IncludeContract.new

  include_djin_config.map do |include_params|
    result = include_contract.call(include_params)

    raise InvalidSyntaxError, { include: result.errors.to_h } if result.failure?

    resolve_include(include_params)
  end
end
local_handler(params) click to toggle source
# File lib/djin/include_config_loader.rb, line 62
def local_handler(params)
  # TODO: Mark not existing files as missing and handle all the missing files
  missing = !@base_directory.join(params['file']).exist?
  params.merge(missing: missing, base_directory: @base_directory.expand_path.to_s)
end
remote_handler(params) click to toggle source
# File lib/djin/include_config_loader.rb, line 46
def remote_handler(params)
  return if params['git'].blank?

  version = params['version'] || 'master'
  # TODO: Extract RemoteConfig git_folder in IncludeConfig to another place and use it here
  # Maybe create a optional git_folder attribute and fill it in here?
  git_folder = "#{params['git'].split('/').last.chomp('.git')}@#{version}"

  # TODO: Use RemoteConfigRepository
  remote_file = @remote_directory.join(git_folder).join(params['file']).expand_path

  missing = !remote_file.exist?

  params.merge(missing: missing, file: remote_file.to_s, base_directory: @remote_directory.expand_path.to_s)
end
resolve_include(params) click to toggle source
# File lib/djin/include_config_loader.rb, line 39
def resolve_include(params)
  include_config_params = remote_handler(params)
  include_config_params ||= local_handler(params)

  build_entity(include_config_params)
end