class ContactManager::ContactRepository

Constants

NAMES_FIRST
NAMES_LAST

Public Class Methods

new(contacts = nil) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_repository.rb, line 142
def initialize(contacts = nil)
  @contacts = contacts || 100.times.map do |n|
    random_first_name_index = (rand*NAMES_FIRST.size).to_i
    random_last_name_index = (rand*NAMES_LAST.size).to_i
    first_name = NAMES_FIRST[random_first_name_index]
    last_name = NAMES_LAST[random_last_name_index]
    email = "#{first_name}@#{last_name}.com".downcase
    Contact.new(
      first_name: first_name,
      last_name: last_name,
      email: email
    )
  end
end

Public Instance Methods

find(attribute_filter_map) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_repository.rb, line 157
def find(attribute_filter_map)
  @contacts.find_all do |contact|
    match = true
    attribute_filter_map.keys.each do |attribute_name|
      contact_value = contact.send(attribute_name).downcase
      filter_value = attribute_filter_map[attribute_name].downcase
      match = false unless contact_value.match(filter_value)
    end
    match
  end
end