Sha256: 65c082ed66784489b50a51024cf55eb5b8ca1ef4b563a83a429ab0cf3a344a31

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

# typed: true

module Parlour
  module Mixin
    # Extends a particular type system's Namespace class to provide searchable
    # children.
    module Searchable
      extend T::Sig
      extend T::Generic

      Child = type_member {{ upper: TypedObject }}

      abstract!

      sig { abstract.returns(T::Array[Child]) }
      def children; end

      sig { params(name: T.nilable(String), type: T.nilable(Class)).returns(Child) }
      # Finds the first child matching the given predicates.
      #
      # @param [String, nil] name The name of the child to filter on, or nil.
      # @param [Class, nil] type The type of the child to filter on, or nil. The
      #   type is compared using #is_a?.
      def find(name: nil, type: nil)
        T.unsafe(children).find { |c| searchable_child_matches(c, name, type) }
      end

      sig { params(name: T.nilable(String), type: T.nilable(Class)).returns(T::Array[Child]) }
      # Finds the first child matching the given predicates.
      #
      # @param [String, nil] name The name of the child to filter on, or nil.
      # @param [Class, nil] type The type of the child to filter on, or nil. The
      #   type is compared using #is_a?.
      def find_all(name: nil, type: nil)
        T.unsafe(children).select { |c| searchable_child_matches(c, name, type) }
      end

      private

      sig do
        params(
          child: Child,
          name: T.nilable(String),
          type: T.nilable(Class)
        )
        .returns(T::Boolean)
      end
      def searchable_child_matches(child, name, type)
        (name.nil? ? true : child.name == name) \
        && (type.nil? ? true : child.is_a?(type))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
parlour-9.0.0 lib/parlour/mixin/searchable.rb
parlour-8.1.0 lib/parlour/mixin/searchable.rb
parlour-8.0.0 lib/parlour/mixin/searchable.rb