Skip to content

Commit

Permalink
Change to document and hide private interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Oct 16, 2018
1 parent b169a40 commit cdd4594
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions lib/tty/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def self.generate(data, separator: '=')
# @api public
attr_accessor :env_prefix

# Create a configuration instance
#
# @api public
def initialize(settings = {})
@settings = settings
@location_paths = []
Expand Down Expand Up @@ -329,7 +332,12 @@ def alias_setting(*keys, to: nil)
@aliases[alias_key] = flat_setting
end

# Register validation for a nested key
# Register a validation rule for a nested key
#
# @param [Array[String]] keys
# a deep nested keys
# @param [Proc] validator
# the logic to use to validate given nested key
#
# @api public
def validate(*keys, &validator)
Expand All @@ -339,26 +347,8 @@ def validate(*keys, &validator)
validators[key] = values
end

# Check if key passes all registered validations
# Find configuration file matching filename and extension
#
# @api private
def assert_valid(key, value)
validators[key].each do |validator|
validator.call(key, value)
end
end

# Delay key validation
#
# @api private
def delay_validation(key, callback)
-> do
val = callback.()
assert_valid(key, val)
val
end
end

# @api private
def find_file
@location_paths.each do |location_path|
Expand Down Expand Up @@ -438,22 +428,57 @@ def to_hash

private

# Ensure that value is set either through parameter or block
#
# @api private
def assert_either_value_or_block(value, block)
if value.nil? && block.nil?
raise ArgumentError, "Need to set either value or block"
elsif !(value.nil? || block.nil?)
raise ArgumentError, "Can't set both value and block"
end
end

# Check if object is a proc with no arguments
#
# @return [Boolean]
#
# @api private
def callable_without_params?(object)
object.respond_to?(:call) &&
(!object.respond_to?(:arity) || object.arity.zero?)
end

def assert_keys_with_block(keys, block)
if keys.size > 1 && block.nil?
raise ArgumentError, "Need to set env var in block"
# Wrap callback in a proc object that includes validation
# that will be performed at point when a new proc is invoked.
#
# @param [String] key
# @param [Proc] callback
#
# @api private
def delay_validation(key, callback)
-> do
val = callback.()
assert_valid(key, val)
val
end
end

def assert_either_value_or_block(value, block)
if value.nil? && block.nil?
raise ArgumentError, "Need to set either value or block"
elsif !(value.nil? || block.nil?)
raise ArgumentError, "Can't set both value and block"
# Check if key passes all registered validations for a key
#
# @param [String] key
# @param [Object] value
#
# @api private
def assert_valid(key, value)
validators[key].each do |validator|
validator.call(key, value)
end
end

def assert_keys_with_block(keys, block)
if keys.size > 1 && block.nil?
raise ArgumentError, "Need to set env var in block"
end
end

Expand Down

0 comments on commit cdd4594

Please sign in to comment.