Module: Sinclair::Settable::ClassMethods

Included in:
ChainSettable, EnvSettable, Sinclair::Settable
Defined in:
lib/sinclair/settable/class_methods.rb

Overview

Class methods availabe inside a Settable module

The methods should help configuring the settable

See Also:

Author:

  • darthjee

Instance Method Summary collapse

Instance Method Details

#read_with(&read_block) ⇒ Proc

Register and return a block for reading a setting

When the block is called, it will receive the key and any given options

Examples:

# config.yml
# timeout: 10

# yaml_file_settable.rb

module YamlFileSettable
  include Sinclair::Settable
  extend Sinclair::Settable::ClassMethods

  read_with do |key, default: nil|
    loaded_yaml[key.to_s] || default
  end

  def loaded_yaml
    YAML.load_file(setting_file)
  end

  def setting_file(file_path = @setting_file)
    @setting_file = file_path
  end
end

# yaml_file_settings.rb

class YamlFileSettings
  extend YamlFileSettable

  setting_file 'config.yml'

  setting_with_options :timeout, default: 30
end

YamlFileSettings.timeout # returns 10

Parameters:

  • read_block (Proc)

    the block that will be used when reading a setting key

Returns:

  • (Proc)


58
59
60
61
62
63
# File 'lib/sinclair/settable/class_methods.rb', line 58

def read_with(&read_block)
  return @read_block = read_block if read_block
  return @read_block if @read_block

  superclass_settable&.read_with
end