Class: Geminabox::GemListMerge

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ GemListMerge


9
10
11
# File 'lib/geminabox/gem_list_merge.rb', line 9

def initialize(list)
  @list = list
end

Instance Attribute Details

#listObject

Returns the value of attribute list


3
4
5
# File 'lib/geminabox/gem_list_merge.rb', line 3

def list
  @list
end

Class Method Details

.from(*lists) ⇒ Object


5
6
7
# File 'lib/geminabox/gem_list_merge.rb', line 5

def self.from(*lists)
  lists.map{|list| new(list)}.inject(:merge)
end

Instance Method Details

#collectionObject


29
30
31
# File 'lib/geminabox/gem_list_merge.rb', line 29

def collection
  @collection ||= {}
end

#combine_hashes(other) ⇒ Object


33
34
35
36
37
# File 'lib/geminabox/gem_list_merge.rb', line 33

def combine_hashes(other)
  hash.merge(other.hash) do |key, value, other_value|
    (value + other_value).uniq{|v| v.values[ignore_dependencies]}
  end
end

#ensure_symbols_as_keys(item) ⇒ Object


43
44
45
46
47
48
# File 'lib/geminabox/gem_list_merge.rb', line 43

def ensure_symbols_as_keys(item)
  item.keys.each do |key|
    next if key.kind_of? Symbol
    item[key.to_sym] = item.delete(key)
  end
end

#hashObject


19
20
21
22
23
24
25
26
27
# File 'lib/geminabox/gem_list_merge.rb', line 19

def hash
  list.each do |item|
    ensure_symbols_as_keys(item)
    name = item[:name].to_sym
    collection[name] ||= []
    collection[name] << item unless collection[name].include?(item)
  end
  collection
end

#ignore_dependenciesObject


39
40
41
# File 'lib/geminabox/gem_list_merge.rb', line 39

def ignore_dependencies
  0..-2
end

#merge(other) ⇒ Object


13
14
15
16
17
# File 'lib/geminabox/gem_list_merge.rb', line 13

def merge(other)
  combine_hashes(other).values.flatten.sort do |x, y|
    x.values[ignore_dependencies] <=> y.values[ignore_dependencies]
  end
end