An insertion ordered set of named objects
{SubjectSet} uses {DataMapper::OrderedSet} under the hood to keep track of a set of entries. In DataMapper code, a subject can be either a {DataMapper::Property}, or a {DataMapper::Associations::Relationship}.
All entries added to instances of this class must respond to the {name} method
The motivation behind this is that we use this class as a base to keep track properties and relationships. The following constraints apply for these types of objects: {Property} names must be unique within any model. {Associations::Relationship} names must be unique within any model
When adding an entry with a name that already exists, the already existing entry will be replaced with the new entry with the same name. This is because we want to be able to update properties, and relationship during the course of initializing our application.
This also happens to be consistent with the way ruby handles redefining methods, where the last definitions “wins”.
Furthermore, the builtin ruby {Set#<<} method also updates the old object if a new object gets added.
@api private
Initialize a SubjectSet
@param [Enumerable<name>] entries
the entries to initialize this set with
@api private
# File lib/dm-core/support/subject_set.rb, line 97 def initialize(entries = []) @entries = OrderedSet.new(entries, NameCache) end
Make sure that entry is part of this SubjectSet
If an entry with the same name already exists, it will be updated. If no such named entry exists, it will be added.
@param [name] entry
the entry to be added
@return [SubjectSet] self
@api private
# File lib/dm-core/support/subject_set.rb, line 120 def <<(entry) entries << entry self end
Lookup an entry in the SubjectSet based on a given name
@param [to_s] name
the name of the entry
@return [Object, nil]
the entry having the given name, or nil if not found
@api private
# File lib/dm-core/support/subject_set.rb, line 193 def [](name) name = name.to_s entries.detect { |entry| entry.name.to_s == name } end
Removes all entries and returns self
@return [SubjectSet] self
@api private
# File lib/dm-core/support/subject_set.rb, line 143 def clear entries.clear self end
Delete an entry from this SubjectSet
@param [name] entry
the entry to delete
@return [name, nil]
the deleted entry or nil
@api private
# File lib/dm-core/support/subject_set.rb, line 134 def delete(entry) entries.delete(entry) end
Iterate over each entry in the set
@yield [entry]
each entry in the set
@yieldparam [name] entry
an entry in the set
@return [SubjectSet] self
@api private
# File lib/dm-core/support/subject_set.rb, line 209 def each return to_enum unless block_given? entries.each { |entry| yield(entry) } self end
Check if there are any entries
@return [Boolean]
true if the set contains at least one entry
@api private
# File lib/dm-core/support/subject_set.rb, line 180 def empty? entries.empty? end
Test if the given entry is included in this SubjectSet
@param [name] entry
the entry to test for
@return [Boolean]
true if the entry is included in this SubjectSet
@api private
# File lib/dm-core/support/subject_set.rb, line 157 def include?(entry) entries.include?(entry) end
Initialize a copy of a SubjectSet
@api private
# File lib/dm-core/support/subject_set.rb, line 104 def initialize_copy(*) @entries = @entries.dup end
Tests wether the SubjectSet contains a entry named name
@param [to_s] name
the entry name to test for
@return [Boolean]
true if the SubjectSet contains a entry named name
@api private
# File lib/dm-core/support/subject_set.rb, line 170 def named?(name) !self[name].nil? end
Get the number of elements inside this SubjectSet
@return [Integer]
the number of elements
@api private
# File lib/dm-core/support/subject_set.rb, line 236 def size entries.size end
Convert the SubjectSet into an Array
@return [Array]
an array containing all the SubjectSet's entries
@api private
# File lib/dm-core/support/subject_set.rb, line 246 def to_ary to_a end
All entries (or nil values) that have any of the given names
@param [Enumerable<to_s>] names
the names of the desired entries
@return [Array<name, nil>]
an array containing entries whose names match any of the given names, or nil values for those names with no matching entries in the set
@api private
# File lib/dm-core/support/subject_set.rb, line 226 def values_at(*names) names.map { |name| self[name] } end
Generated with the Darkfish Rdoc Generator 2.