Class: DataMapper::SubjectSet Private

Inherits:
Object
  • Object
show all
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.

API:

  • private

Direct Known Subclasses

PropertySet, RelationshipSet

Defined Under Namespace

Classes: NameCache

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • (defaults to: [])

    the entries to initialize this set with

API:

  • private



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

#entriesOrderedSet (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

Returns:

API:

  • private



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.

Parameters:

  • the entry to be added

Returns:

  • self

API:

  • private



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

Parameters:

  • the name of the entry

Returns:

  • the entry having the given name, or nil if not found

API:

  • private



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

#clearSubjectSet

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

Returns:

  • self

API:

  • private



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

Parameters:

  • the entry to delete

Returns:

  • the deleted entry or nil

API:

  • private



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

Yields:

  • (entry)

    each entry in the set

Yield Parameters:

  • entry (#name)

    an entry in the set

Returns:

  • self

API:

  • private



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

Returns:

  • true if the set contains at least one entry

API:

  • private



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

Parameters:

  • the entry to test for

Returns:

  • true if the entry is included in this SubjectSet

API:

  • private



159
160
161
# File 'lib/dm-core/support/subject_set.rb', line 159

def include?(entry)
  entries.include?(entry)
end

#initialize_copyObject

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

API:

  • private



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

Parameters:

  • the entry name to test for

Returns:

  • true if the SubjectSet contains a entry named name

API:

  • private



172
173
174
# File 'lib/dm-core/support/subject_set.rb', line 172

def named?(name)
  !self[name].nil?
end

#sizeInteger

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

Returns:

  • the number of elements

API:

  • private



237
238
239
# File 'lib/dm-core/support/subject_set.rb', line 237

def size
  entries.size
end

#to_aryArray

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

Returns:

  • an array containing all the SubjectSet’s entries

API:

  • private



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

Parameters:

  • the names of the desired entries

Returns:

  • 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



227
228
229
# File 'lib/dm-core/support/subject_set.rb', line 227

def values_at(*names)
  names.map { |name| self[name] }
end