Class: Extent

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/extensional/extent.rb

Overview

An Extent is a thin Array wrapper with optional associative access.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, &block) ⇒ Extent

Creates a new Extent. The block is called when an instance is added to the extent.



12
13
14
15
16
17
# File 'lib/extensional/extent.rb', line 12

def initialize(hash=nil, &block) # :yields: hash, instance
  @extent = Set.new
  @association = hash
  @association ||= {} if block_given?
  @callback = block
end

Instance Attribute Details

#associationObject (readonly)

The key=>instance association hash.



9
10
11
# File 'lib/extensional/extent.rb', line 9

def association
  @association
end

Instance Method Details

#<<(obj) ⇒ Object

Adds the given obj to this extent.



20
21
22
23
# File 'lib/extensional/extent.rb', line 20

def <<(obj)
  @callback.call(@association, obj) if @callback
  @extent << obj
end

#associative?Boolean

Returns whether this Extent implements associative access by key.

Returns:

  • (Boolean)


31
32
33
# File 'lib/extensional/extent.rb', line 31

def associative?
  not @association.nil?
end

#clearObject

Removes all instances from this Extent’s extent and association.



44
45
46
47
# File 'lib/extensional/extent.rb', line 44

def clear
  @extent.clear
  @association.clear if associative?
end

#each(&block) ⇒ Object

Calls the given block an each instance in this extent.



26
27
28
# File 'lib/extensional/extent.rb', line 26

def each(&block)
  @extent.each(&block)
end

#find(key) ⇒ Object

Returns the instance for the given key.

Raises NotImplementedError if this extent does not implement associative access.

Raises:

  • (NotImplementedError)


38
39
40
41
# File 'lib/extensional/extent.rb', line 38

def find(key)
  raise NotImplementedError.new("Associative access by key is not implemented for this class extent") unless associative?
  @association[key]
end