Sha256: fd3b4d16b210b66cd9f1e5236210e36c2dbbc0e4af9b1f79c6b595c32b9ddc25

Contents?: true

Size: 1.75 KB

Versions: 11

Compression:

Stored size: 1.75 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
        add_filds_to_equals

        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

      # Add the fields to equals comparation
      #
      # @return (see Sinclair::EqualsChecker#add)
      def add_filds_to_equals
        klass.comparable_by(*attributes.keys)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sinclair-1.16.1 lib/sinclair/options/builder.rb
sinclair-1.16.0 lib/sinclair/options/builder.rb
sinclair-1.15.0 lib/sinclair/options/builder.rb
sinclair-1.14.2 lib/sinclair/options/builder.rb
sinclair-1.14.1 lib/sinclair/options/builder.rb
sinclair-1.14.0 lib/sinclair/options/builder.rb
sinclair-1.13.0 lib/sinclair/options/builder.rb
sinclair-1.12.1 lib/sinclair/options/builder.rb
sinclair-1.12.0 lib/sinclair/options/builder.rb
sinclair-1.11.0 lib/sinclair/options/builder.rb
sinclair-1.10.0 lib/sinclair/options/builder.rb