Sha256: 89dfe5eee7ebef811e24a9cd84161b7ede2ee625e79f1e77355535efc66694c2

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module TestIds
  class Configuration
    class Item
      attr_accessor :include, :exclude, :algorithm

      def initialize
        @include = BinArray.new
        @exclude = BinArray.new
      end

      def callback(&block)
        if block_given?
          @callback = block
        else
          @callback
        end
      end

      def empty?
        include.empty? && exclude.empty? && !algorithm && !callback
      end

      def function?
        !!algorithm || !!callback
      end

      def freeze
        @include.freeze
        @exclude.freeze
        super
      end
    end

    attr_reader :allocator

    def initialize(id)
      @id = id
      @allocator = Allocator.new(self)
    end

    def id
      @id
    end

    def bins
      @bins ||= Item.new
    end

    def softbins
      @softbins ||= Item.new
      if block_given?
        @softbins.callback(&block)
      end
      @softbins
    end

    # An alias for config.softbins.algorithm=
    def softbins=(val)
      softbins.algorithm = val
    end

    def numbers(&block)
      @numbers ||= Item.new
      if block_given?
        @numbers.callback(&block)
      end
      @numbers
    end

    # An alias for config.numbers.algorithm=
    def numbers=(val)
      numbers.algorithm = val
    end

    def validate!
      unless validated?
        if bins.algorithm
          fail 'The TestIds bins configuration cannot be set to an algorithm, only a range set by bins.include and bins.exclude is permitted'
        end
        if bins.callback
          fail 'The TestIds bins configuration cannot be set by a callback, only a range set by bins.include and bins.exclude is permitted'
        end
        @validated = true
        freeze
      end
    end

    def empty?
      bins.empty? && softbins.empty? && numbers.empty?
    end

    def validated?
      @validated
    end

    def freeze
      bins.freeze
      softbins.freeze
      numbers.freeze
      super
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
test_ids-0.6.1 lib/test_ids/configuration.rb
test_ids-0.6.0 lib/test_ids/configuration.rb
test_ids-0.5.1 lib/test_ids/configuration.rb
test_ids-0.5.0 lib/test_ids/configuration.rb
test_ids-0.4.1 lib/test_ids/configuration.rb
test_ids-0.4.0 lib/test_ids/configuration.rb
test_ids-0.3.2 lib/test_ids/configuration.rb
test_ids-0.3.1 lib/test_ids/configuration.rb
test_ids-0.3.0 lib/test_ids/configuration.rb