Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/qonfig/commands/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ module Qonfig::Commands::Definition
require_relative 'definition/expose_yaml'
require_relative 'definition/expose_json'
require_relative 'definition/expose_self'
require_relative 'definition/expose_file'
require_relative 'definition/load_from_file'
end
52 changes: 52 additions & 0 deletions lib/qonfig/commands/definition/expose_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# @api private
# @since 0.24.0
class Qonfig::Commands::Definition::ExposeFile < Qonfig::Commands::Base
# @since 0.24.0
self.inheritable = true

# @return [String, Pathname]
#
# @api private
# @since 0.24.0
attr_reader :file_path

# @return [String, Symbol]
#
# @api private
# @since 0.24.0
attr_reader :env

# @return [Symbol]
#
# @api private
# @since 0.24.0
attr_reader :via

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

# @param file_path [String, Pathname]
# @option env [String, Symbol]
# @option via [Symbol]
# @option strict [Boolean]
# @option format [String, Symbol]
# @return [void]
#
# @api private
# @since 0.24.0
def initialize(file_path, env:, via:, strict:, format:)
end

# @param data_set [Qonfig::DataSet]
# @param settings [Qonfig::Settings]
# @return [void]
#
# @api private
# @since 0.24.0
def call(data_set, settings); end
end
72 changes: 72 additions & 0 deletions lib/qonfig/commands/definition/load_from_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# @api private
# @since 0.24.0
class Qonfig::Commands::Definition::LoadFromFile < Qonfig::Commands::Base
# @since 0.24.0
self.inheritable = true

# @return [Symbol]
#
# @api private
# @since 0.24.0
SELF_FILE_PATH = :self

# @return [String, Pathname]
#
# @api private
# @since 0.24.0
attr_reader :file_path

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

# @return [String, Symbol]
#
# @api private
# @since 0.24.0
attr_reader :format

# @param file_path [String, Pathname]
# @option strcit [Boolean]
# @option format [String, Symbol]
# @return [void]
#
# @api private
# @since 0.24.0
def initialize(file_path, strict: true, format: :dynamic)
unless format.is_a?(String) || format.is_a?(Symbol)
raise Qonfig::ArgumentError, 'Formad should be a type of string or symbol'
end

unless file_path.is_a?(String) || file_path.is_a?(Pathname) || file_path == SELF_FILE_PATH
raise Qonfig::ArgumentError, 'Incorrect file path'
end

unless strict.is_a?(TrueClass) || strict.is_a?(FalseClass)
raise Qonfig::ArgumentError, ':strict should be a type of boolean'
end

@file_path = file_path
@strict = strict
@format = format.tap { Qonfig::Loaders.resolve(format) }
end

# @param data_set [Qonfig::DataSet]
# @param settings [Qonfig::Settings]
# @return [void]
#
# @api private
# @since 0.24.0
def call(data_set, settings)
settings_data = Qonfig::Loaders.resolve(format).load_file(file_path, fail_on_unexist: strict)

raise(
Qonfig::IncompatibleDataStructureError,
'Setting values should be represented as a hash-like structure'
) unless settings_data.is_a?(Hash)
end
end
2 changes: 1 addition & 1 deletion lib/qonfig/commands/definition/load_from_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Qonfig::Commands::Definition::LoadFromSelf < Qonfig::Commands::Base
# @since 0.2.0
def initialize(caller_location, format:)
unless format.is_a?(String) || format.is_a?(Symbol)
raise Qonfig::ArgumentError, 'Format should be a symbol or a string'
raise Qonfig::ArgumentError, 'Format should be a type of string or symbol'
end

@caller_location = caller_location
Expand Down
6 changes: 4 additions & 2 deletions lib/qonfig/commands/instantiation/values_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ def load_settings_values
#
# @api private
# @since 0.17.0
# @version 0.24.0
def load_from_file
Qonfig::Loaders.resolve(format).load_file(file_path, fail_on_unexist: strict).tap do |values|
raise(
Qonfig::IncompatibleDataStructureError,
'Setting values must be a hash-like structure'
'Setting values should be represented as a hash-like structure'
) unless values.is_a?(Hash)
end
end
Expand All @@ -129,13 +130,14 @@ def load_from_file
#
# @api private
# @since 0.17.0
# @version 0.24.0
def load_from_self
end_data = Qonfig::Loaders::EndData.extract(caller_location)

Qonfig::Loaders.resolve(format).load(end_data).tap do |values|
raise(
Qonfig::IncompatibleDataStructureError,
'Setting values must be a hash-like structure'
'Setting values should be represented as a hash-like structure'
) unless values.is_a?(Hash)
end
rescue Qonfig::SelfDataNotFoundError => error
Expand Down
28 changes: 28 additions & 0 deletions lib/qonfig/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ def load_from_json(file_path, strict: true)
definition_commands << Qonfig::Commands::Definition::LoadFromJSON.new(file_path, strict: strict)
end

# @option file_path [String, Pathname]
# @option strict [Boolean]
# @option format [Symbol, String]
# @return [void]
#
# @api public
# @version 0.24.0
def load_from_file(file_path, strict: true, format: :dynamic)
definition_commands << Qonfig::Commands::Definition::LoadFroMfile.new(
file_path, strict: strict, format: format
)
end

# @param file_path [String, Pathname]
# @option strict [Boolean]
# @option via [Symbol]
Expand Down Expand Up @@ -261,6 +274,21 @@ def expose_self(env:, format: :dynamic)
)
end

# @option [String, Pathname]
# @option via [Symbol]
# @option env [Symbol, String]
# @option strict [Boolean]
# @option format [Symbol, String]
# @return [void]
#
# @api public
# @version 0.24.0
def expose_file(file_path, via:, env:, strict: true, format: :dynamic)
definition_commands << Qonfig::Commands::Definition::ExposeFile.new(
file_path, strict: strict, via: via, env: env, format: format
)
end

# @param file_path [String, Pathname]
# @option format [String, Symbol]
# @option strict [Boolean]
Expand Down