Class: ArrayCollection::CollectionArray

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

Overview

Sets of array utilities methods

Class Method Summary collapse

Class Method Details

.append(array, *elements) ⇒ Object



36
37
38
# File 'lib/array_collection/collection_array.rb', line 36

def append(array, *elements)
  array + elements
end

.diff(array1, array2) ⇒ Object



68
69
70
# File 'lib/array_collection/collection_array.rb', line 68

def diff(array1, array2)
  (array1 - array2) | (array2 - array1)
end

.except(array, *keys) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
# File 'lib/array_collection/collection_array.rb', line 56

def except(array, *keys)
  raise ArgumentError, "Empty key list" if keys.empty?

  array.map { |hash| hash.except(*keys) }
end

.filter(array, &block) ⇒ Object



9
10
11
# File 'lib/array_collection/collection_array.rb', line 9

def filter(array, &block)
  array.select(&block)
end

.full_join(array1, array2, key1, key2) ⇒ Object



108
109
110
111
112
113
# File 'lib/array_collection/collection_array.rb', line 108

def full_join(array1, array2, key1, key2)
  left_join_result = left_join(array1, array2, key1, key2)
  right_join_result = right_join(array1, array2, key1, key2)

  left_join_result.concat(right_join_result).uniq
end

.full_outter_join(array1, array2, key1, key2) ⇒ Object



115
116
117
# File 'lib/array_collection/collection_array.rb', line 115

def full_outter_join(array1, array2, key1, key2)
  full_join(array1, array2, key1, key2)
end

.inner_join(array1, array2, key1, key2) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/array_collection/collection_array.rb', line 76

def inner_join(array1, array2, key1, key2)
  array1.reject do |item1|
    matching_items = array2.select { |item2| item1[key1] == item2[key2] }
    matching_items.each { |matched_item| item1.merge!(matched_item) }
    matching_items.empty?
  end
end

.join(array1, array2, key1, key2) ⇒ Object



72
73
74
# File 'lib/array_collection/collection_array.rb', line 72

def join(array1, array2, key1, key2)
  inner_join(array1, array2, key1, key2)
end

.key_by(records, key) ⇒ Object



52
53
54
# File 'lib/array_collection/collection_array.rb', line 52

def key_by(records, key)
  records.to_h { |record| [record[key].to_s, yield(record)] }
end

.left_join(array1, array2, key1, key2) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/array_collection/collection_array.rb', line 84

def left_join(array1, array2, key1, key2)
  return array1 if array2.count.zero?

  right_index = build_index(array2, key2)

  result = []
  array1.each do |left_item|
    right_items = right_index[left_item[key1]] || [nil_attributes(array2)]
    merge_items(result, left_item, right_items)
  end
  result
end

.map(array, &block) ⇒ Object



44
45
46
# File 'lib/array_collection/collection_array.rb', line 44

def map(array, &block)
  array.map(&block)
end

.map_with_keys(hash, &block) ⇒ Object



48
49
50
# File 'lib/array_collection/collection_array.rb', line 48

def map_with_keys(hash, &block)
  hash.transform_values(&block)
end

.only(array, *keys) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/array_collection/collection_array.rb', line 62

def only(array, *keys)
  raise ArgumentError, "Empty key list" if keys.empty?

  array.map { |hash| hash.select { |k, _| keys.include?(k) } }
end

.prepend(array, *elements) ⇒ Object



40
41
42
# File 'lib/array_collection/collection_array.rb', line 40

def prepend(array, *elements)
  elements + array
end

.right_join(array1, array2, key1, key2) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/array_collection/collection_array.rb', line 97

def right_join(array1, array2, key1, key2)
  left_index = build_index(array1, key1)

  result = []
  array2.each do |right_item|
    left_items = left_index[right_item[key2]] || [nil_attributes(array1)]
    merge_items(result, right_item, left_items)
  end
  result
end

.where(array, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/array_collection/collection_array.rb', line 13

def where(array, *args)
  if args.size == 2
    key, value = args
    array.select { |item| item[key] == value }
  elsif args.size == 3
    key, operator, value = args
    array.select { |item| ArrayCollection::CollectionFilter.apply_operator(operator, item[key], value) }
  else
    raise ArgumentError, "Invalid number of arguments"
  end
end

.where_not_nil(array) ⇒ Object



25
26
27
# File 'lib/array_collection/collection_array.rb', line 25

def where_not_nil(array)
  array.compact
end

.wrap(value) ⇒ Object



29
30
31
32
33
34
# File 'lib/array_collection/collection_array.rb', line 29

def wrap(value)
  return [] if value.nil?
  return value if value.is_a?(Array)

  [value]
end