Class: Characterize::Collection
- Inherits:
-
Object
- Object
- Characterize::Collection
- Extended by:
- Forwardable
- Includes:
- Casting::Enum
- Defined in:
- lib/characterize/collection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#behaviors ⇒ Object
readonly
Returns the value of attribute behaviors.
-
#casted_enum ⇒ Object
readonly
Returns the value of attribute casted_enum.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Class Method Summary collapse
-
.for(collection, *behaviors) ⇒ Object
Return an object handling the collection’s type.
-
.register(klass, type) ⇒ Object
Register a collection class to manage the given type.
- .registry ⇒ Object
- .safe_command(*classes, receiver:) ⇒ Object
Instance Method Summary collapse
-
#initialize(collection, *behaviors) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
Constructor Details
#initialize(collection, *behaviors) ⇒ Collection
Returns a new instance of Collection.
44 45 46 47 |
# File 'lib/characterize/collection.rb', line 44 def initialize(collection, *behaviors) @behaviors = behaviors reset_collection(collection) end |
Instance Attribute Details
#behaviors ⇒ Object (readonly)
Returns the value of attribute behaviors.
48 49 50 |
# File 'lib/characterize/collection.rb', line 48 def behaviors @behaviors end |
#casted_enum ⇒ Object (readonly)
Returns the value of attribute casted_enum.
48 49 50 |
# File 'lib/characterize/collection.rb', line 48 def casted_enum @casted_enum end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
48 49 50 |
# File 'lib/characterize/collection.rb', line 48 def collection @collection end |
Class Method Details
.for(collection, *behaviors) ⇒ Object
Return an object handling the collection’s type
This will select the first matching class from which the collection’s class inherits.
24 25 26 27 28 29 |
# File 'lib/characterize/collection.rb', line 24 def for(collection, *behaviors) klass = registry.find { |key, _value| collection.class < key }&.last || self klass.new(collection, *behaviors) end |
.register(klass, type) ⇒ Object
Register a collection class to manage the given type
15 16 17 |
# File 'lib/characterize/collection.rb', line 15 def register(klass, type) registry[type] = klass end |
.registry ⇒ Object
10 11 12 |
# File 'lib/characterize/collection.rb', line 10 def registry @registry ||= {} end |
.safe_command(*classes, receiver:) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/characterize/collection.rb', line 31 def safe_command(*classes, receiver:) standard = [:object_id, :__send__] + Enumerator.instance_methods(false) + Enumerable.instance_methods(false) classes.each do |klass| names = klass.instance_methods - standard - instance_methods(false) command names => :collection end end |
Instance Method Details
#inspect ⇒ Object
53 54 55 |
# File 'lib/characterize/collection.rb', line 53 def inspect %(#<#{self.class} #{object_id}>) end |