Class: DataMapper::SubjectSet Private
- Inherits:
-
Object
- Object
- DataMapper::SubjectSet
- Includes:
- Enumerable
- Defined in:
- lib/dm-core/support/subject_set.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An insertion ordered set of named objects
SubjectSet uses OrderedSet under the hood to keep track of a set of entries. In DataMapper code, a subject can be either a Property, or a 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.
Direct Known Subclasses
Defined Under Namespace
Classes: NameCache
Instance Attribute Summary collapse
-
#entries ⇒ OrderedSet
readonly
private
The elements in the SubjectSet.
Instance Method Summary collapse
-
#<<(entry) ⇒ SubjectSet
private
Make sure that entry is part of this SubjectSet.
-
#[](name) ⇒ Object?
private
Lookup an entry in the SubjectSet based on a given name.
-
#clear ⇒ SubjectSet
private
Removes all entries and returns self.
-
#delete(entry) ⇒ #name?
private
Delete an entry from this SubjectSet.
-
#each {|entry| ... } ⇒ SubjectSet
private
Iterate over each entry in the set.
-
#empty? ⇒ Boolean
private
Check if there are any entries.
-
#include?(entry) ⇒ Boolean
private
Test if the given entry is included in this SubjectSet.
-
#initialize(entries = []) ⇒ SubjectSet
constructor
private
Initialize a SubjectSet.
-
#initialize_copy ⇒ Object
private
Initialize a copy of a SubjectSet.
-
#named?(name) ⇒ Boolean
private
Tests wether the SubjectSet contains a entry named name.
-
#size ⇒ Integer
private
Get the number of elements inside this SubjectSet.
-
#to_ary ⇒ Array
private
Convert the SubjectSet into an Array.
-
#values_at(*names) ⇒ Array<#name, nil>
private
All entries (or nil values) that have any of the given names.
Constructor Details
#initialize(entries = []) ⇒ SubjectSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a SubjectSet
99 100 101 |
# File 'lib/dm-core/support/subject_set.rb', line 99 def initialize(entries = []) @entries = OrderedSet.new(entries, NameCache) end |
Instance Attribute Details
#entries ⇒ OrderedSet (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The elements in the SubjectSet
91 92 93 |
# File 'lib/dm-core/support/subject_set.rb', line 91 def entries @entries end |
Instance Method Details
#<<(entry) ⇒ SubjectSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
122 123 124 125 |
# File 'lib/dm-core/support/subject_set.rb', line 122 def <<(entry) entries << entry self end |
#[](name) ⇒ Object?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Lookup an entry in the SubjectSet based on a given name
195 196 197 198 |
# File 'lib/dm-core/support/subject_set.rb', line 195 def [](name) name = name.to_s entries.detect { |entry| entry.name.to_s == name } end |
#clear ⇒ SubjectSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes all entries and returns self
145 146 147 148 |
# File 'lib/dm-core/support/subject_set.rb', line 145 def clear entries.clear self end |
#delete(entry) ⇒ #name?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete an entry from this SubjectSet
136 137 138 |
# File 'lib/dm-core/support/subject_set.rb', line 136 def delete(entry) entries.delete(entry) end |
#each {|entry| ... } ⇒ SubjectSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iterate over each entry in the set
211 212 213 214 |
# File 'lib/dm-core/support/subject_set.rb', line 211 def each entries.each { |entry| yield(entry) } self end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if there are any entries
182 183 184 |
# File 'lib/dm-core/support/subject_set.rb', line 182 def empty? entries.empty? end |
#include?(entry) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Test if the given entry is included in this SubjectSet
159 160 161 |
# File 'lib/dm-core/support/subject_set.rb', line 159 def include?(entry) entries.include?(entry) end |
#initialize_copy ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a copy of a SubjectSet
106 107 108 |
# File 'lib/dm-core/support/subject_set.rb', line 106 def initialize_copy(*) @entries = @entries.dup end |
#named?(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tests wether the SubjectSet contains a entry named name
172 173 174 |
# File 'lib/dm-core/support/subject_set.rb', line 172 def named?(name) !self[name].nil? end |
#size ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the number of elements inside this SubjectSet
237 238 239 |
# File 'lib/dm-core/support/subject_set.rb', line 237 def size entries.size end |
#to_ary ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert the SubjectSet into an Array
247 248 249 |
# File 'lib/dm-core/support/subject_set.rb', line 247 def to_ary to_a end |
#values_at(*names) ⇒ Array<#name, nil>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All entries (or nil values) that have any of the given names
227 228 229 |
# File 'lib/dm-core/support/subject_set.rb', line 227 def values_at(*names) names.map { |name| self[name] } end |