Sha256: f4cde15660b96797df1ace02f5f5fd81fff2e067acbedd9588a64774c97ec0d6

Contents?: true

Size: 956 Bytes

Versions: 12

Compression:

Stored size: 956 Bytes

Contents

module Yoti
  class MultiValue
    def initialize(items)
      raise(TypeError, "Arrays must be passed to #{self.class.name}") unless items.is_a?(Array)

      @original_items = items
      @items = items.dup
      @filters = []
    end

    def allow_type(type)
      filter { |value| value.is_a?(type) }
      self
    end

    def filter(&callback)
      @filters.push(callback)
      apply_filters
      self
    end

    def items
      apply_filters
      @items.clone.freeze
    end

    private

    def apply_filters
      @items = @original_items.dup

      if @filters.count.positive?
        @items.keep_if do |item|
          match = @filters.select { |callback| callback.call(item) }
          match.count.positive?
        end
      end

      @items.each do |item|
        next unless item.is_a?(MultiValue)

        @filters.each do |callback|
          item.filter { |value| callback.call(value) }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
yoti-1.10.0 lib/yoti/data_type/multi_value.rb
yoti-1.9.0 lib/yoti/data_type/multi_value.rb
yoti-1.8.0 lib/yoti/data_type/multi_value.rb
yoti-1.7.1 lib/yoti/data_type/multi_value.rb
yoti-1.7.0 lib/yoti/data_type/multi_value.rb
yoti-1.6.4 lib/yoti/data_type/multi_value.rb
yoti-1.6.3 lib/yoti/data_type/multi_value.rb
yoti-1.6.2 lib/yoti/data_type/multi_value.rb
yoti-1.6.1 lib/yoti/data_type/multi_value.rb
yoti-1.6.0 lib/yoti/data_type/multi_value.rb
yoti-1.5.0 lib/yoti/data_type/multi_value.rb
yoti-1.4.0 lib/yoti/data_type/multi_value.rb