Class: FlapjackConfigurator::EntityMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/flapjack_configurator/entity_mapper.rb

Overview

Walk though the entities and build the map of entities to contacts Built as a class for future proofing and because this is expected to be an expensive operation: so instantiate it once and pass it around.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_obj, diner) ⇒ EntityMapper

Returns a new instance of EntityMapper.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flapjack_configurator/entity_mapper.rb', line 11

def initialize(config_obj, diner)
  @entity_map = {}.tap { |em| config_obj.contact_ids.each { |cn| em[cn.to_sym] = [] } }
  default_contacts = config_obj.default_contacts

  # Walk the entities and compare each individually so that the whitelisting/blacklisting can be easily enforced
  # This probably will need some optimization
  diner.entities.each do |entity|
    contact_defined = false
    config_obj.contact_ids.each do |contact_id|
      match_id = _check_entity(entity, config_obj.contact_config(contact_id))
      if match_id
        @entity_map[contact_id.to_sym].push(entity[:id])
        contact_defined = true
      end
    end

    # ALL is a special case entity, don't associate the default to it.
    # Using next if per Rubocop :)
    next if contact_defined || entity[:id] == 'ALL'
    # No contacts match this entity, add it to the defaults
    default_contacts.each do |contact_id|
      @entity_map[contact_id.to_sym].push(entity[:id])
    end
  end

  return @entity_map
end

Instance Attribute Details

#entity_mapObject (readonly)

Returns the value of attribute entity_map.



9
10
11
# File 'lib/flapjack_configurator/entity_mapper.rb', line 9

def entity_map
  @entity_map
end

Instance Method Details

#_check_entity(entity, contact) ⇒ Object

Check if entity should be included in contact Helper for _build_entity_map Returns the entity ID on match or nil on no match



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flapjack_configurator/entity_mapper.rb', line 42

def _check_entity(entity, contact)
  # Priority 1: Exact Entries
  return true if contact['entities']['exact'].include? entity[:name]

  # Priority 2: Exact blacklist
  return false if contact['entities_blacklist']['exact'].include? entity[:name]

  # Priority 3: Regex blacklist
  contact['entities_blacklist']['regex'].each do |bl_regex|
    return false if /#{bl_regex}/.match(entity[:name])
  end

  # Priority 4: Match regex
  contact['entities']['regex'].each do |m_regex|
    return true if /#{m_regex}/.match(entity[:name])
  end

  # Fallthrough
  return false
end

#entities_for_contact(id) ⇒ Object

Return the entities for the given contact Returns a clone so the returned object is modifyable



65
66
67
68
# File 'lib/flapjack_configurator/entity_mapper.rb', line 65

def entities_for_contact(id)
  fail("ID #{id} not in entity map") unless @entity_map.key? id.to_sym
  return DeepClone.clone @entity_map[id.to_sym]
end