Class: Cuprum::Collections::Collection

Inherits:
Cuprum::CommandFactory
  • Object
show all
Includes:
Relations::Options, Relations::Parameters, Relations::PrimaryKeys, Relations::Scope
Defined in:
lib/cuprum/collections/collection.rb

Overview

Provides a base implementation for collections.

Direct Known Subclasses

Basic::Collection

Defined Under Namespace

Classes: AbstractCollectionError

Instance Attribute Summary

Attributes included from Relations::Scope

#scope

Attributes included from Relations::Parameters

#name, #plural_name, #qualified_name, #singular_name

Attributes included from Relations::Options

#options

Instance Method Summary collapse

Methods included from Relations::Scope

#with_scope

Methods included from Relations::PrimaryKeys

#primary_key_name, #primary_key_type

Methods included from Relations::Parameters

#entity_class, resolve_parameters, #resolve_parameters

Constructor Details

#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Object

Parameters:

  • entity_class (Class, String) (defaults to: nil)

    the class of entity represented by the relation.

  • name (String) (defaults to: nil)

    the name of the relation.

  • qualified_name (String) (defaults to: nil)

    a scoped name for the relation.

  • singular_name (String) (defaults to: nil)

    the name of an entity in the relation.

  • options (Hash)

    additional options for the relation.

Options Hash (**options):

  • primary_key_name (String)

    the name of the primary key attribute. Defaults to 'id'.

  • primary_key_type (Class, Stannum::Constraint)

    the type of the primary key attribute. Defaults to Integer.

  • scope (Object)

    [Cuprum::Collections::Scopes::Base, Hash, Proc, nil] the configured scope for the relation.



# File 'lib/cuprum/collections/collection.rb', line 24

Instance Method Details

#==(other) ⇒ true, false

Returns true if the other object is a collection with the same options, otherwise false.

Parameters:

  • other (Object)

    The object to compare.

Returns:

  • (true, false)

    true if the other object is a collection with the same options, otherwise false.



44
45
46
47
48
# File 'lib/cuprum/collections/collection.rb', line 44

def ==(other)
  return false unless self.class == other.class

  comparable_options == other.comparable_options
end

#countInteger Also known as: size

Returns the count of items in the collection.

Returns:

  • (Integer)

    the count of items in the collection.



51
52
53
# File 'lib/cuprum/collections/collection.rb', line 51

def count
  query.count
end

#matches?(**expected) ⇒ Boolean

Checks if the collection matches the expected options.

Parameters:

  • expected (Hash)

    the options to compare.

Returns:

  • (Boolean)

    true if all of the expected options match, otherwise false.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cuprum/collections/collection.rb', line 62

def matches?(**expected)
  if expected[:entity_class].is_a?(String)
    expected = expected.merge(
      entity_class: Object.const_get(expected[:entity_class])
    )
  end

  expected[:name] = expected[:name].to_s if expected[:name]

  if expected[:qualified_name]
    expected[:qualified_name] = expected[:qualified_name].to_s
  end

  comparable_options >= expected
end

#queryObject

A new Query instance, used for querying against the collection data.

Returns:

  • (Object)

    the query.

Raises:



81
82
83
84
85
# File 'lib/cuprum/collections/collection.rb', line 81

def query
  raise AbstractCollectionError,
    "#{self.class.name} is an abstract class. Define a collection " \
    'subclass and implement the #query method.'
end