Module: ActiveObject::Array

Defined in:
lib/active_object/array.rb

Instance Method Summary collapse

Instance Method Details

#after(value) ⇒ Object



3
4
5
6
7
# File 'lib/active_object/array.rb', line 3

def after(value)
  return unless include?(value)

  self[(index(value).to_i + 1) % length]
end

#before(value) ⇒ Object



9
10
11
12
13
# File 'lib/active_object/array.rb', line 9

def before(value)
  return unless include?(value)

  self[(index(value).to_i - 1) % length]
end

#delete_firstObject



15
16
17
# File 'lib/active_object/array.rb', line 15

def delete_first
  self[1..-1]
end

#delete_first!Object



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

def delete_first!
  replace(delete_first)
end

#delete_lastObject



23
24
25
# File 'lib/active_object/array.rb', line 23

def delete_last
  self[0...-1]
end

#delete_last!Object



27
28
29
# File 'lib/active_object/array.rb', line 27

def delete_last!
  replace(delete_last)
end

#delete_values(*args) ⇒ Object



31
32
33
34
35
# File 'lib/active_object/array.rb', line 31

def delete_values(*args)
  result = []
  args.each { |val| result << delete(val) }
  result
end

#dig(key, *rest) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/active_object/array.rb', line 37

def dig(key, *rest)
  value = (self[key] rescue nil)

  return if value.nil?
  return value if rest.empty?
  return value.dig(*rest) if value.respond_to?(:dig)
end

#duplicates(minimum = 2) ⇒ Object



45
46
47
48
49
# File 'lib/active_object/array.rb', line 45

def duplicates(minimum = 2)
  hash = Hash.new(0)
  each { |val| hash[val] += 1 }
  hash.delete_if { |_, val| val < minimum }.keys
end

#from(position) ⇒ Object



51
52
53
# File 'lib/active_object/array.rb', line 51

def from(position)
  self[position, length] || []
end

#groups(number) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/active_object/array.rb', line 55

def groups(number)
  return [] if number <= 0

  num, rem = length.divmod(number)
  collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
  rem.positive? ? collection << self[-rem, rem] : collection
end

#in_groups(number, fill_with = nil) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_object/array.rb', line 64

def in_groups(number, fill_with = nil)
  collection_length = length
  division = collection_length.div(number)
  modulo = collection_length % number

  collection = []
  start = 0
  number.times do |int|
    mod_gt_zero = modulo.positive?
    grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
    collection << last_group = slice(start, grouping)
    last_group << fill_with if fill_with != false && mod_gt_zero && grouping == division
    start += grouping
  end

  block_given? ? collection.each { |val| yield(val) } : collection
end

#in_groups_of(number, fill_with = nil) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_object/array.rb', line 84

def in_groups_of(number, fill_with = nil)
  if number.to_i <= 0
    raise ArgumentError,
          "Group length must be a positive integer, was #{number.inspect}"
  end

  if fill_with == false
    collection = self
  else
    padding = (number - length % number) % number
    collection = dup.concat(Array.new(padding, fill_with))
  end

  sliced_collection = collection.each_slice(number)

  block_given? ? sliced_collection { |val| yield(val) } : sliced_collection.to_a
end

#percentile(percentage) ⇒ Object

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



103
104
105
106
107
108
109
110
111
112
# File 'lib/active_object/array.rb', line 103

def percentile(percentage)
  total_size = size

  if total_size > 1
    index = (total_size * percentage) / 100.0
    sort[index]
  else
    first
  end
end

#probabilityObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_object/array.rb', line 114

def probability
  hash = Hash.new(0.0)
  differ = 0.0

  each do |val|
    hash[val] += 1.0
    differ += 1.0
  end

  hash.each_key { |val| hash[val] /= differ }
  hash
end

#reject_values(*args) ⇒ Object



127
128
129
# File 'lib/active_object/array.rb', line 127

def reject_values(*args)
  reject { |val| args.include?(val) }
end

#sample!Object



131
132
133
# File 'lib/active_object/array.rb', line 131

def sample!
  delete_at(Random.rand(length - 1))
end

#split(number = nil) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/active_object/array.rb', line 136

def split(number = nil)
  if block_given?
    each_with_object([[]]) do |element, results|
      yield(element) ? (results << []) : (results.last << element)
    end
  else
    results = [[]]
    arr = dup

    until arr.empty?
      if (idx = arr.index(number))
        results.last.concat(arr.shift(idx))
        arr.shift
        results << []
      else
        results.last.concat(arr.shift(arr.length))
      end
    end

    results
  end
end

#stripObject

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



160
161
162
# File 'lib/active_object/array.rb', line 160

def strip
  reject(&:blank?)
end

#strip!Object



164
165
166
# File 'lib/active_object/array.rb', line 164

def strip!
  replace(strip)
end

#to(position) ⇒ Object



168
169
170
# File 'lib/active_object/array.rb', line 168

def to(position)
  position >= 0 ? first(position + 1) : self[0..position]
end

#to_sentence(options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/active_object/array.rb', line 173

def to_sentence(options = {})
  default_connectors = {
    words_connector: ', ',
    two_words_connector: ' and ',
    last_word_connector: ', and '
  }
  options = default_connectors.merge!(options)

  case length
  when 0
    ''
  when 1
    self[0].to_s.dup
  when 2
    "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
  else
    "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
  end
end