Sha256: 527bed73c920f708170d80b91a44ba5413d9fa2c907df86e0d24eaa8f2e744a6

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.2.0
class Qonfig::Commands::LoadFromSelf < Qonfig::Commands::Base
  # @return [String]
  #
  # @api private
  # @since 0.2.0
  attr_reader :caller_location

  # @param caller_location [String]
  #
  # @api private
  # @since 0.2.0
  def initialize(caller_location)
    @caller_location = caller_location
  end

  # @param settings [Qonfig::Settings]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def call(settings)
    yaml_data = load_self_placed_yaml_data

    yaml_based_settings = build_data_set_klass(yaml_data).new.settings

    settings.__append_settings__(yaml_based_settings)
  end

  private

  # @return [Hash]
  #
  # @raise [Qonfig::SelfDataNotFound]
  # @raise [Qonfig::IncompatibleYAMLStructureError]
  #
  # @api private
  # @since 0.2.0
  def load_self_placed_yaml_data
    caller_file = caller_location.split(':').first

    raise(
      Qonfig::SelfDataNotFoundError,
      "Caller file does not exist! (location: #{caller_location})"
    ) unless File.exist?(caller_file)

    data_match = IO.read(caller_file).match(/\n__END__\n(?<end_data>.*)/m)
    raise Qonfig::SelfDataNotFoundError, '__END__ data not found!' unless data_match

    end_data = data_match[:end_data]
    raise Qonfig::SelfDataNotFoundError, '__END__ data not found!' unless end_data

    yaml_data = Qonfig::Loaders::YAML.load(end_data)
    raise(
      Qonfig::IncompatibleYAMLStructureError,
      'YAML content should have a hash-like structure'
    ) unless yaml_data.is_a?(Hash)

    yaml_data
  end

  # @param self_placed_yaml_data [Hash]
  # @return [Class<Qonfig::DataSet>]
  #
  # @api private
  # @since 0.2.0
  def build_data_set_klass(self_placed_yaml_data)
    Qonfig::DataSet::ClassBuilder.build_from_hash(self_placed_yaml_data)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qonfig-0.12.0 lib/qonfig/commands/load_from_self.rb
qonfig-0.11.0 lib/qonfig/commands/load_from_self.rb