lib/nanoc/base/entities/identifiable_collection.rb in nanoc-4.7.0 vs lib/nanoc/base/entities/identifiable_collection.rb in nanoc-4.7.1
- old
+ new
@@ -2,10 +2,11 @@
# @api private
class IdentifiableCollection
include Nanoc::Int::ContractsSupport
include Enumerable
+ extend Nanoc::Int::Memoization
extend Forwardable
def_delegator :@objects, :each
def_delegator :@objects, :size
@@ -27,19 +28,14 @@
super
end
contract C::Any => C::Maybe[C::RespondTo[:identifier]]
def [](arg)
- case arg
- when Nanoc::Identifier
- object_with_identifier(arg)
- when String
- object_with_identifier(arg) || object_matching_glob(arg)
- when Regexp
- @objects.find { |i| i.identifier.to_s =~ arg }
+ if frozen?
+ get_memoized(arg)
else
- raise ArgumentError, "don’t know how to fetch objects by #{arg.inspect}"
+ get_unmemoized(arg)
end
end
contract C::None => C::ArrayOf[C::RespondTo[:identifier]]
def to_a
@@ -58,9 +54,29 @@
def reject(&block)
self.class.new(@config, @objects.reject(&block))
end
protected
+
+ contract C::Any => C::Maybe[C::RespondTo[:identifier]]
+ def get_unmemoized(arg)
+ case arg
+ when Nanoc::Identifier
+ object_with_identifier(arg)
+ when String
+ object_with_identifier(arg) || object_matching_glob(arg)
+ when Regexp
+ @objects.find { |i| i.identifier.to_s =~ arg }
+ else
+ raise ArgumentError, "don’t know how to fetch objects by #{arg.inspect}"
+ end
+ end
+
+ contract C::Any => C::Maybe[C::RespondTo[:identifier]]
+ def get_memoized(arg)
+ get_unmemoized(arg)
+ end
+ memoize :get_memoized
def object_with_identifier(identifier)
if frozen?
@mapping[identifier.to_s]
else