module Attributor::Type
It is the abstract base class to hold an attribute, both a leaf and a container (hash/Array…) TODO: should this be a mixin since it is an abstract class?
Attributes
_memoized_collection_classes[RW]
Public Class Methods
[](collection_type=Attributor::Collection)
click to toggle source
Creates a new type that is a collection of this member types By default, T[] is equivalent to Collection.of(T)
But you can pass the collection class to use, e.g. T is equivalent to CSV.of(T)
# File lib/attributor/type.rb, line 41 def self.[](collection_type=Attributor::Collection) member_class = self existing = Attributor::Type.get_memoized_collection_class(member_class,collection_type) return existing if existing unless self.ancestors.include?(Attributor::Type) raise Attributor::AttributorException, 'Collections can only have members that are Attributor::Types' end # Create and memoize it for non-constructable types here (like we do in Type[]) new_class = ::Class.new(collection_type) do @member_type = member_class end if self.constructable? new_class else # Unfortunately we cannot freeze the memoized class as it lazily sets the member_attribute inside Attributor::Type.set_memoized_collection_class(new_class, member_class, collection_type) end end
get_memoized_collection_class(member_class, collection_type=Attributor::Collection)
click to toggle source
# File lib/attributor/type.rb, line 26 def get_memoized_collection_class(member_class, collection_type=Attributor::Collection) # No need to serialize on read, Ruby will ensure we can properly read what's there, even if it's being written _memoized_collection_classes.dig(member_class,collection_type) end
set_memoized_collection_class(klass, member_class, collection_type=Attributor::Collection)
click to toggle source
# File lib/attributor/type.rb, line 30 def set_memoized_collection_class(klass, member_class, collection_type=Attributor::Collection) @_memoized_collection_classes_mutex.synchronize do _memoized_collection_classes[member_class][collection_type] = klass end end