Class: Ardm::SubjectSet Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ardm/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 Ardm 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

PropertySet

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:

  • entries (Enumerable<#name>) (defaults to: [])

    the entries to initialize this set with



97
98
99
# File 'lib/ardm/support/subject_set.rb', line 97

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:



89
90
91
# File 'lib/ardm/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.

Parameters:

  • entry (#name)

    the entry to be added

Returns:



120
121
122
123
# File 'lib/ardm/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

Parameters:

  • name (#to_s)

    the name of the entry

Returns:

  • (Object, nil)

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



193
194
195
196
# File 'lib/ardm/support/subject_set.rb', line 193

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:



143
144
145
146
# File 'lib/ardm/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

Parameters:

  • entry (#name)

    the entry to delete

Returns:

  • (#name, nil)

    the deleted entry or nil



134
135
136
# File 'lib/ardm/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

Yields:

  • (entry)

    each entry in the set

Yield Parameters:

  • entry (#name)

    an entry in the set

Returns:



209
210
211
212
# File 'lib/ardm/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

Returns:

  • (Boolean)

    true if the set contains at least one entry



180
181
182
# File 'lib/ardm/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

Parameters:

  • entry (#name)

    the entry to test for

Returns:

  • (Boolean)

    true if the entry is included in this SubjectSet



157
158
159
# File 'lib/ardm/support/subject_set.rb', line 157

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



104
105
106
# File 'lib/ardm/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

Parameters:

  • name (#to_s)

    the entry name to test for

Returns:

  • (Boolean)

    true if the SubjectSet contains a entry named name



170
171
172
# File 'lib/ardm/support/subject_set.rb', line 170

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:

  • (Integer)

    the number of elements



235
236
237
# File 'lib/ardm/support/subject_set.rb', line 235

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:

  • (Array)

    an array containing all the SubjectSet’s entries



245
246
247
# File 'lib/ardm/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

Parameters:

  • names (Enumerable<#to_s>)

    the names of the desired entries

Returns:

  • (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



225
226
227
# File 'lib/ardm/support/subject_set.rb', line 225

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