Class: AdjustCardinality

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

Overview

Given a data structure from traject, transform it into a valid IR by changing some values from arrays to scalars

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AdjustCardinality

Returns a new instance of AdjustCardinality.


12
13
14
15
# File 'lib/adjust_cardinality.rb', line 12

def initialize(attributes)
  # %w(id agg_data_provider agg_provider agg_is_shown_at agg_is_shown_by agg_preview)
  @source = attributes
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source


17
18
19
# File 'lib/adjust_cardinality.rb', line 17

def source
  @source
end

Class Method Details

.call(attributes) ⇒ Object


8
9
10
# File 'lib/adjust_cardinality.rb', line 8

def self.call(attributes)
  new(attributes).call
end

Instance Method Details

#callObject


19
20
21
# File 'lib/adjust_cardinality.rb', line 19

def call
  flatten_web_resources(flatten_top_level(source))
end

#flatten_services(services) ⇒ Object


53
54
55
# File 'lib/adjust_cardinality.rb', line 53

def flatten_services(services)
  services.map { |svc| process_service(svc) }
end

#flatten_top_level(attributes) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/adjust_cardinality.rb', line 23

def flatten_top_level(attributes)
  flatten = %w[id agg_data_provider agg_provider agg_is_shown_at agg_is_shown_by agg_preview]
  attributes.except(*flatten).tap do |output|
    flatten.each do |field|
      next unless attributes.key?(field)

      value = attributes.fetch(field).first
      output[field] = value
    end
  end
end

#flatten_web_resources(attributes) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# File 'lib/adjust_cardinality.rb', line 35

def flatten_web_resources(attributes)
  flatten = %w[agg_is_shown_at agg_is_shown_by agg_preview]
  attributes.except(*flatten).tap do |output|
    flatten.each do |field|
      next unless attributes.key?(field)

      output[field] = process_web_resource(attributes.fetch(field))
    end
  end
end

#process_node(original, fields) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/adjust_cardinality.rb', line 61

def process_node(original, fields)
  original.except(*fields).tap do |corrected|
    fields.each do |field|
      corrected[field] = original.fetch(field).first
    end
  end
end

#process_service(service) ⇒ Object


57
58
59
# File 'lib/adjust_cardinality.rb', line 57

def process_service(service)
  process_node(service, %w[service_id service_implements])
end

#process_web_resource(web_resource) ⇒ Object


46
47
48
49
50
51
# File 'lib/adjust_cardinality.rb', line 46

def process_web_resource(web_resource)
  res = process_node(web_resource, %w[wr_id])
  res.except('wr_has_service').tap do |resource|
    resource['wr_has_service'] = flatten_services(res.fetch('wr_has_service')) if res.key?('wr_has_service')
  end
end