Class: Characterize::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Casting::Enum
Defined in:
lib/characterize/collection.rb

Direct Known Subclasses

RelationCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#behaviorsObject (readonly)

Returns the value of attribute behaviors.



48
49
50
# File 'lib/characterize/collection.rb', line 48

def behaviors
  @behaviors
end

#casted_enumObject (readonly)

Returns the value of attribute casted_enum.



48
49
50
# File 'lib/characterize/collection.rb', line 48

def casted_enum
  @casted_enum
end

#collectionObject (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

.registryObject



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

#inspectObject



53
54
55
# File 'lib/characterize/collection.rb', line 53

def inspect
  %(#<#{self.class} #{object_id}>)
end