Sha256: 6cda097ec8047b6fcb5cea7677b2a42c2a0459fd2e9ff1aac27e9c63e1fa3e74

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

class Sinclair
  module Settable
    # @api public
    # @author darthjee
    #
    # Class methods availabe inside a Settable module
    #
    # The methods should help configuring the settable
    #
    # @see #read_with
    module ClassMethods
      # Register and return a block for reading a setting
      #
      # @param read_block [Proc] the block that will be used when
      #   reading a setting key
      #
      # When the block is called, it will receive the key and any
      # given options
      #
      # @return [Proc]
      #
      # @example
      #   # 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
      def read_with(&read_block)
        return @read_block = read_block if read_block
        return @read_block if @read_block

        superclass_settable&.read_with
      end

      private

      # @private
      # @api private
      #
      # Returns a {Settable} module representing a superclass
      #
      # @return [Module]
      def superclass_settable
        included_modules.find do |modu|
          modu <= Sinclair::Settable
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/settable/class_methods.rb
sinclair-2.1.0 lib/sinclair/settable/class_methods.rb