Sha256: 05e41cd688aed455358cc531d318fff13b34361baefa2880ee627017efc205ec

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module DiverDown
  class Web
    class DefinitionEnumerator
      include Enumerable

      # @param store [DiverDown::Definition::Store]
      # @param query [String]
      def initialize(store, title: '', source: '')
        @store = store
        @title = title
        @source = source
      end

      # @yield [parent_bit_id, bit_id, definition]
      def each
        return enum_for(__method__) unless block_given?

        definition_groups = @store.definition_groups
        definition_groups.each do |definition_group|
          definitions = @store.filter_by_definition_group(definition_group)
          definitions.each do
            next unless match_definition?(_1)

            yield(_1)
          end
        end
      end

      # @return [Integer]
      def size
        @store.size
      end
      alias length size

      private

      def match_definition?(definition)
        matched = true

        unless @title.empty?
          matched &&= definition.title.include?(@title)
        end

        unless @source.empty?
          matched &&= definition.sources.any? { _1.source_name.include?(@source) }
        end

        matched
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
diver_down-0.0.1.alpha2 lib/diver_down/web/definition_enumerator.rb
diver_down-0.0.1.alpha1 lib/diver_down/web/definition_enumerator.rb