Class: Stannum::Associations::Many::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/stannum/associations/many.rb

Overview

Wrapper object for an entity’s plural association.

Instance Method Summary collapse

Constructor Details

#initialize(association:, entity:) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:



19
20
21
22
# File 'lib/stannum/associations/many.rb', line 19

def initialize(association:, entity:)
  @association = association
  @entity      = entity
end

Instance Method Details

#==(other) ⇒ true, false

Returns true if the object has matching data; otherwise false.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (true, false)

    true if the object has matching data; otherwise false.



56
57
58
59
60
61
62
# File 'lib/stannum/associations/many.rb', line 56

def ==(other)
  return false if other.nil?

  return false unless other.respond_to?(:to_a)

  to_a == other.to_a
end

#[](index) ⇒ Stannum::Entity

Retrieves the value of the association at the specified index.

Parameters:

  • index (Integer)

    the index of the value to retrieve.

Returns:



30
# File 'lib/stannum/associations/many.rb', line 30

def_delegator :data, :[]

#add(value) ⇒ self Also known as: <<, push

Appends the entity to the association.

If the entity is already in the association data, this method does nothing.

Parameters:

Returns:

  • (self)

    the association proxy.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stannum/associations/many.rb', line 72

def add(value)
  unless value.is_a?(association.resolved_type)
    message =
      'invalid association item - must be an instance of ' \
      "#{association.resolved_type.name}"

    raise ArgumentError, message
  end

  association.add_value(entity, value)

  self
end

#eachEnumerator #each {|item| ... } ⇒ Object

Overloads:

  • #eachEnumerator

    Returns an enumerator over each item in the entity’s association data.

    Returns:

    • (Enumerator)

      an enumerator over each item in the entity’s association data.

  • #each {|item| ... } ⇒ Object

    Yields:

    • Yields each item in the entity’s association data.

    Yield Parameters:



40
# File 'lib/stannum/associations/many.rb', line 40

def_delegator :data, :each

#empty?true, false

Returns true if the association has no items; otherwise false.

Returns:

  • (true, false)

    true if the association has no items; otherwise false.



45
# File 'lib/stannum/associations/many.rb', line 45

def_delegator :data, :empty?

#inspectString

Returns a human-readable string representation of the object.

Returns:

  • (String)

    a human-readable string representation of the object.



89
90
91
# File 'lib/stannum/associations/many.rb', line 89

def inspect
  "#{super[...55]} data=[#{each.map(&:inspect).join(', ')}]>"
end

#remove(value) ⇒ self Also known as: delete

Removes the entity from the association.

If the entity is not in the association data, this method does nothing.

Parameters:

Returns:

  • (self)

    the association proxy.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/stannum/associations/many.rb', line 100

def remove(value)
  unless value.is_a?(association.resolved_type)
    message =
      'invalid association item - must be an instance of ' \
      "#{association.resolved_type.name}"

    raise ArgumentError, message
  end

  association.remove_value(entity, value)

  self
end