Sha256: 75204deb39a3a85c69640d0c48998302002f4fc7a45f8ccb920dcfcef65600fd

Contents?: true

Size: 649 Bytes

Versions: 1

Compression:

Stored size: 649 Bytes

Contents

# frozen_string_literal: true

require_relative 'lookup/version'

require 'memery'

module Magic
	module Lookup
		autoload :Error, 'magic/lookup/error'

		include Memery

		memoize def for object_class
			descendants = self.descendants # cache
					.reverse # most specific first

			object_class.ancestors
					.lazy # optimization
					.filter(&:name)
					.map { name_for _1 }
					.filter_map do |class_name|
						descendants.find { _1.name == class_name }
					end
					.first
		end

		def name_for(object_class) = raise NotImplementedError

		def descendants
			[
					*subclasses,
					*subclasses.flat_map(&__method__),
			]
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magic-lookup-0.1.0 lib/magic/lookup.rb