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
97 98 99 |
# File 'lib/dm-core/support/subject_set.rb', line 97 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
89 90 91 |
# File 'lib/dm-core/support/subject_set.rb', line 89 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.
120 121 122 123 |
# File 'lib/dm-core/support/subject_set.rb', line 120 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
193 194 195 196 |
# 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 |
#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
143 144 145 146 |
# File 'lib/dm-core/support/subject_set.rb', line 143 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
134 135 136 |
# File 'lib/dm-core/support/subject_set.rb', line 134 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
209 210 211 212 |
# File 'lib/dm-core/support/subject_set.rb', line 209 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
180 181 182 |
# File 'lib/dm-core/support/subject_set.rb', line 180 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
157 158 159 |
# File 'lib/dm-core/support/subject_set.rb', line 157 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
104 105 106 |
# File 'lib/dm-core/support/subject_set.rb', line 104 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
170 171 172 |
# File 'lib/dm-core/support/subject_set.rb', line 170 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
235 236 237 |
# File 'lib/dm-core/support/subject_set.rb', line 235 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
245 246 247 |
# File 'lib/dm-core/support/subject_set.rb', line 245 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
225 226 227 |
# File 'lib/dm-core/support/subject_set.rb', line 225 def values_at(*names) names.map { |name| self[name] } end |