Sha256: 878fe7bfffed621906f77a046b668927569933b786e129c5721d369eaa4d75ae

Contents?: true

Size: 1.41 KB

Versions: 9

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Qonfig
  module Commands
    # @api private
    # @since 0.2.0
    class LoadFromYAML < Base
      # @return [String]
      #
      # @api private
      # @since 0.2.0
      attr_reader :file_path

      # @return [Boolean]
      #
      # @api private
      # @since 0.2.0
      attr_reader :strict

      # @param file_path [String]
      # @option strict [Boolean]
      #
      # @api private
      # @since 0.2.0
      def initialize(file_path, strict: true)
        @file_path = file_path
        @strict = strict
      end

      # @param settings [Qonfig::Settings]
      # @return [void]
      #
      # @raise [Qonfig::IncompatibleYAMLStructureError]
      #
      # @api private
      # @since 0.2.0
      def call(settings)
        yaml_data = Qonfig::Loaders::YAML.load_file(file_path, fail_on_unexist: strict)

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

        yaml_based_settings = build_data_set_class(yaml_data).new.settings

        settings.__append_settings__(yaml_based_settings)
      end

      private

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
qonfig-0.10.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.9.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.8.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.7.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.6.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.5.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.4.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.3.0 lib/qonfig/commands/load_from_yaml.rb
qonfig-0.2.0 lib/qonfig/commands/load_from_yaml.rb