Sha256: 74ad45152dafe6362ca721f062ec09b9160cdbbb433dfd72fade47442372d30b

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

class Sinclair
  # @api private
  #
  # Class responsible for setting the values on configuration
  #
  # @example
  #   class MyConfig
  #     attr_reader :name, :config
  #   end
  #
  #   config = MyConfig.new
  #
  #   builder = Sinclair::ConfigBuilder.new(config, :name)
  #
  #   builder.instance_eval { |c| c.name 'John' }
  #
  #   config.name # returns 'John'
  class ConfigBuilder
    # A new instance of ConfigBuilder
    #
    # @param config [Sinclair::Config] config object to be build
    #   config object has no attribute setters (only readers)
    #   so all attributes are set as instance variable
    #
    # @param config_attributes [Array<Symbol>] list of attributes
    #   that can be set on config (expecting that config has
    #   the right attribute readers)
    def initialize(config, *config_attributes)
      @config = config
      @config_attributes = config_attributes
    end

    private

    # @private
    #
    # Method called for methods missing
    #
    # When a method is missing, it is expected that it is the
    # name of a variable to be set on config (as long as it was
    # defined in the config_attributes_array)
    #
    # @param method_name [Symbol] name of the method called
    # @param args [Array<Object>] arguments of the call
    #
    # @return [Object]
    def method_missing(method_name, *args)
      return super unless @config_attributes.include?(method_name)

      @config.instance_variable_set("@#{method_name}", *args)
    end

    # @private
    #
    # Checks if method missing will catch the method called
    #
    # @return [TrueClass,FalseClass]
    #
    # @see #method_missing
    def respond_to_missing?(method_name, include_private)
      @config_attributes.include?(method_name) || super
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sinclair-1.3.4 lib/sinclair/config_builder.rb
sinclair-1.3.3 lib/sinclair/config_builder.rb
sinclair-1.3.2 lib/sinclair/config_builder.rb
sinclair-1.3.1 lib/sinclair/config_builder.rb
sinclair-1.3.0 lib/sinclair/config_builder.rb