Module: Maestrano::Connector::Rails::Concerns::ComplexEntity::ClassMethods

Defined in:
app/models/maestrano/connector/rails/concerns/complex_entity.rb,
app/models/maestrano/connector/rails/concerns/complex_entity.rb

Overview


Helper methods

Instance Method Summary collapse

Instance Method Details

#build_empty_hash(entities_names) ⇒ Object

output : => []



179
180
181
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 179

def build_empty_hash(entities_names)
  Hash[*entities_names.collect { |name| [name, []] }.flatten(1)]
end

#build_hash_with_entities(entities_name, entity_name, proc, entities) ⇒ Object

output: => entities with proc.call(entities_name == entity_name)



185
186
187
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 185

def build_hash_with_entities(entities_name, entity_name, proc, entities)
  Hash[*entities_name.collect { |name| proc.call(name) == entity_name ? [name, entities] : [name, []] }.flatten(1)]
end

#connec_entities_namesObject



9
10
11
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 9

def connec_entities_names
  raise 'Not implemented'
end

#count_and_first(entities) ⇒ Object

For complex entities, we take the size of the biggest array And the first record we can find (even if it’s not in the first array)



33
34
35
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 33

def count_and_first(entities)
  {count: entities.values.map(&:size).max, first: entities.values.flatten.first}
end

#external_entities_namesObject



13
14
15
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 13

def external_entities_names
  raise 'Not implemented'
end

#find_complex_entity_and_instantiate_external_sub_entity_instance(entity_name, organization, connec_client, external_client, opts) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 193

def find_complex_entity_and_instantiate_external_sub_entity_instance(entity_name, organization, connec_client, external_client, opts)
  Maestrano::Connector::Rails::External.entities_list.each do |entity_name_from_list|
    clazz = "Entities::#{entity_name_from_list.singularize.titleize.split.join}".constantize
    if clazz.methods.include?('external_entities_names'.to_sym)
      formatted_names = clazz.formatted_external_entities_names
      return instantiate_sub_entity_instance(formatted_names[entity_name], organization, connec_client, external_client, opts) if formatted_names[entity_name]
    end
  end
  nil
end

#formatted_connec_entities_namesObject



21
22
23
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 21

def formatted_connec_entities_names
  formatted_entities_names(connec_entities_names)
end

#formatted_entities_names(names) ⇒ Object



25
26
27
28
29
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 25

def formatted_entities_names(names)
  return names.with_indifferent_access if names.is_a?(Hash)

  names.index_by { |name| name }.with_indifferent_access
end

#formatted_external_entities_namesObject



17
18
19
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 17

def formatted_external_entities_names
  formatted_entities_names(external_entities_names)
end

#instantiate_sub_entity_instance(entity_name, organization, connec_client, external_client, opts) ⇒ Object



189
190
191
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 189

def instantiate_sub_entity_instance(entity_name, organization, connec_client, external_client, opts)
  "Entities::SubEntities::#{entity_name.titleize.split.join}".constantize.new(organization, connec_client, external_client, opts)
end

#public_connec_entity_nameObject



37
38
39
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 37

def public_connec_entity_name
  public_name(formatted_connec_entities_names)
end

#public_external_entity_nameObject



41
42
43
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 41

def public_external_entity_name
  public_name(formatted_external_entities_names)
end

#public_name(formatted_names) ⇒ Object



45
46
47
48
49
50
# File 'app/models/maestrano/connector/rails/concerns/complex_entity.rb', line 45

def public_name(formatted_names)
  names = formatted_names.keys.map(&:pluralize)
  return names.first.humanize if names.size == 1

  (names[0..-2].join(', ') + " and #{names.last}").humanize
end