Sha256: d02c6ef67152b9388a5a0d072a5365ffa4570542685e17c3ee3844ec6ac5f15b

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true
module GetYourRep
  # The Delegation class holds an array of Representative objects. It can enumerate on its reps.
  class Delegation
    include Enumerable
    include GetYourRep::Errors

    # Set the reps attribute, which will hold an array of Representative objects, to an empty array by default.
    def initialize
      @reps = []
    end

    # Defines the enumerator methods on the :reps attribute.
    def each(&block)
      @reps.each(&block)
    end

    # Merge reps with another Delegation. This will add the reps of other to the reps of self,
    # and leave other unchanged. Only accepts a Delegation as an argument.
    def merge(other)
      if other.is_a?(self.class)
        other.reps.each { |rep| add_rep(rep) }
        self
      else
        not_a_del_error
      end
    end

    # Returns a frozen duplicate of the reps array.
    def reps
      @reps.dup.freeze
    end

    # Empties the reps array.
    def clear_reps
      @reps.clear
    end

    # Add a Representative to the reps array. Only accepts a Representative as an argument.
    def add_rep(other)
      if other.is_a?(Representative)
        @reps << other
        other.delegation = self unless other.delegation == self
      else
        not_a_rep_error
      end
    end

    # Collects the OfficeLocations of all associated reps.
    def office_locations
      map(&:office_locations).flatten
    end

    # Collects the full names of all associated reps.
    def names
      map(&:name)
    end

    # Collects the first names of all associated reps.
    def first_names
      map(&:first_name)
    end

    # Collects the last names of all associated reps.
    def last_names
      map(&:last_name)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
get_your_rep-1.1.0 lib/get_your_rep/delegation.rb
get_your_rep-1.0.8 lib/get_your_rep/delegation.rb
get_your_rep-1.0.7 lib/get_your_rep/delegation.rb
get_your_rep-1.0.6 lib/get_your_rep/delegation.rb
get_your_rep-1.0.5 lib/get_your_rep/delegation.rb
get_your_rep-1.0.4 lib/get_your_rep/delegation.rb
get_your_rep-1.0.3 lib/get_your_rep/delegation.rb
get_your_rep-1.0.2 lib/get_your_rep/delegation.rb
get_your_rep-1.0.1 lib/get_your_rep/delegation.rb
get_your_rep-1.0.0 lib/get_your_rep/delegation.rb