Sha256: 2eedd57199adad2f7ee1c39c20b404d025a63ca8028fc4b860fa8311f40686d9

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

class Sinclair
  class Options
    # @api private
    # @author Darthjee
    #
    # Option Class Builder
    #
    # This class builds methods for options objects
    class Builder < Sinclair
      # @overload initialize(klass, *options)
      #   @param klass [Class] options class to receive
      #     methods
      #   @param options [Array<Symbol>] list of accepted
      #     options
      # @overload initialize(klass, *options, **defaults)
      #   @param klass [Class] options class to receive
      #     methods
      #   @param options [Array<Symbol>] list of accepted
      #     options
      #   @param defaults [Hash<Symbol,Object>] default options
      #     hash
      def initialize(klass, *options)
        super(klass)

        @attributes = Sinclair::InputHash.input_hash(*options)
      end

      # Finish building options
      #
      # Add options to allowed options
      # and adds all methods
      #
      # @see Sinclair#build
      # @return (see Sinclair#build)
      def build
        add_all_methods

        super
      end

      private

      attr_reader :attributes
      # @method attributes
      # @api private
      # @private
      #
      # Options attributes
      #
      # @return [Hash<Symbol.Object>]

      # Add all methods for options
      #
      # @return [Array<MethodDefinition>]
      def add_all_methods
        attributes.each do |option, value|
          add_method(option, cached: :full) { value }
          klass.allow(option)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sinclair-1.9.0 lib/sinclair/options/builder.rb
sinclair-1.8.0 lib/sinclair/options/builder.rb
sinclair-1.7.0 lib/sinclair/options/builder.rb
sinclair-1.6.7 lib/sinclair/options/builder.rb
sinclair-1.6.6 lib/sinclair/options/builder.rb
sinclair-1.6.5 lib/sinclair/options/builder.rb
sinclair-1.6.4 lib/sinclair/options/builder.rb
sinclair-1.6.3 lib/sinclair/options/builder.rb