Class: Teapot::IdentitySet
- Inherits:
-
Object
- Object
- Teapot::IdentitySet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/teapot/identity_set.rb
Overview
Very similar to a set but uses a specific callback (defaults to &:name) for object identity.
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #add(object) ⇒ Object (also: #<<)
- #delete(object) ⇒ Object
- #each(&block) ⇒ Object
- #freeze ⇒ Object
- #identity(object) ⇒ Object
- #include?(object) ⇒ Boolean
-
#initialize(contents = []) ⇒ IdentitySet
constructor
A new instance of IdentitySet.
- #initialize_dup(other) ⇒ Object
Constructor Details
#initialize(contents = []) ⇒ IdentitySet
Returns a new instance of IdentitySet.
28 29 30 31 32 33 34 |
# File 'lib/teapot/identity_set.rb', line 28 def initialize(contents = []) @table = {} contents.each do |object| add(object) end end |
Instance Attribute Details
#table ⇒ Object (readonly)
Returns the value of attribute table.
50 51 52 |
# File 'lib/teapot/identity_set.rb', line 50 def table @table end |
Instance Method Details
#add(object) ⇒ Object Also known as: <<
52 53 54 |
# File 'lib/teapot/identity_set.rb', line 52 def add(object) @table[identity(object)] = object end |
#delete(object) ⇒ Object
58 59 60 |
# File 'lib/teapot/identity_set.rb', line 58 def delete(object) @table.delete(identity(object)) end |
#each(&block) ⇒ Object
66 67 68 |
# File 'lib/teapot/identity_set.rb', line 66 def each(&block) @table.each_value(&block) end |
#freeze ⇒ Object
36 37 38 39 40 |
# File 'lib/teapot/identity_set.rb', line 36 def freeze @table.freeze super end |
#identity(object) ⇒ Object
46 47 48 |
# File 'lib/teapot/identity_set.rb', line 46 def identity(object) object.name end |
#include?(object) ⇒ Boolean
62 63 64 |
# File 'lib/teapot/identity_set.rb', line 62 def include?(object) @table.include?(identity(object)) end |
#initialize_dup(other) ⇒ Object
42 43 44 |
# File 'lib/teapot/identity_set.rb', line 42 def initialize_dup(other) @table = other.table.dup end |