Class: ToFactory::Collation

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Collation

Returns a new instance of Collation.



13
14
15
16
# File 'lib/to_factory/collation.rb', line 13

def initialize(a, b)
  @a = a
  @b = b
end

Class Method Details

.organize(a, b) ⇒ Object



5
6
7
# File 'lib/to_factory/collation.rb', line 5

def self.organize(a,b)
  new(a, b).organize
end

.representations_from(a, b) ⇒ Object



9
10
11
# File 'lib/to_factory/collation.rb', line 9

def self.representations_from(a,b)
  new(a, b).representations
end

Instance Method Details

#detect_collisions!(a, b) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/to_factory/collation.rb', line 39

def detect_collisions!(a,b)
  collisions = []
  a.each do |x|
    b.each do |y|
      collisions << x.name if x.name == y.name
    end
  end

  raise_already_exists!(collisions) if collisions.any?
end

#organizeObject



18
19
20
21
22
23
# File 'lib/to_factory/collation.rb', line 18

def organize
  representations.group_by{|i| i.klass.name.underscore}.inject({}) do |o, (klass_name,r)|
    o[klass_name] = r.sort_by(&:hierarchy_order)
    o
  end
end

#representationsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/to_factory/collation.rb', line 25

def representations
  detect_collisions!(@a,@b)

  inference = KlassInference.new(merged)

  merged.each do |r|
    klass, order = inference.infer(r.name)
    r.klass = klass
    r.hierarchy_order = order
  end

  merged
end