Sha256: f12b82b0c622863d4107f3684b04634936375ccb0d860bf7a54dc8698edb36dd

Contents?: true

Size: 1.75 KB

Versions: 6

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

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

      # 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

6 entries across 6 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/options/builder.rb
sinclair-2.1.0 lib/sinclair/options/builder.rb
sinclair-2.0.1 lib/sinclair/options/builder.rb
sinclair-2.0.0 lib/sinclair/options/builder.rb
sinclair-1.16.3 lib/sinclair/options/builder.rb
sinclair-1.16.2 lib/sinclair/options/builder.rb