Module: ActiveObject::Array

Defined in:
lib/active_object/array.rb

Instance Method Summary collapse

Instance Method Details

#after(value) ⇒ Object



6
7
8
9
10
# File 'lib/active_object/array.rb', line 6

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

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

#before(value) ⇒ Object



12
13
14
15
16
# File 'lib/active_object/array.rb', line 12

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

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

#bury(*args) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, rubocop:disable Style/GuardClause, Style/IfInsideElse



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_object/array.rb', line 20

def bury(*args)
  if args.count < 2
    raise ArgumentError, '2 or more arguments required'
  elsif args.count == 2
    if args[0].is_a?(Integer)
      self[args[0]] = args[1]
    else
      self << { args[0] => args[1] }
    end
  else
    if args[0].is_a?(Integer)
      arg = args.shift

      self[arg] = [] unless self[arg]
      self[arg].bury(*args)
    else
      self << {}.bury(*args)
    end
  end

  self
end

#delete_firstObject

rubocop:enable Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity



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

def delete_first
  self[1..-1]
end

#delete_first!Object



49
50
51
# File 'lib/active_object/array.rb', line 49

def delete_first!
  replace(delete_first)
end

#delete_lastObject



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

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

#delete_last!Object



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

def delete_last!
  replace(delete_last)
end

#delete_values(*args) ⇒ Object



61
62
63
# File 'lib/active_object/array.rb', line 61

def delete_values(*args)
  args.each_with_object([]) { |val, results| results << delete(val) }
end

#demote(value) ⇒ Object



65
66
67
# File 'lib/active_object/array.rb', line 65

def demote(value)
  sort_by { |val| val == value ? 0 : -1 }
end

#demote!(value) ⇒ Object



69
70
71
# File 'lib/active_object/array.rb', line 69

def demote!(value)
  replace(demote(value))
end

#denillify(value = 0) ⇒ Object



73
74
75
# File 'lib/active_object/array.rb', line 73

def denillify(value = 0)
  map { |val| val.nil? ? value : val }
end

#denillify!(value = 0) ⇒ Object



77
78
79
# File 'lib/active_object/array.rb', line 77

def denillify!(value = 0)
  replace(denillify(value))
end

#dig(key, *rest) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/active_object/array.rb', line 81

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



89
90
91
92
93
# File 'lib/active_object/array.rb', line 89

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

#from(position) ⇒ Object



95
96
97
# File 'lib/active_object/array.rb', line 95

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

#fulfill(value, amount) ⇒ Object



99
100
101
102
103
# File 'lib/active_object/array.rb', line 99

def fulfill(value, amount)
  return self if amount <= length

  fill(value, length..(amount - 1))
end

#groups(number) ⇒ Object



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

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



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_object/array.rb', line 134

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

#indexes(value) ⇒ Object

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



152
153
154
155
156
# File 'lib/active_object/array.rb', line 152

def indexes(value)
  results = []
  each_with_index { |val, i| results << i if value == val }
  results
end

#merge(*values) ⇒ Object



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

def merge(*values)
  values.each { |val| concat(val) }
  self
end

#nillifyObject



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

def nillify
  map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
end

#nillify!Object



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

def nillify!
  replace(nillify)
end

#position(value) ⇒ Object



171
172
173
174
175
176
# File 'lib/active_object/array.rb', line 171

def position(value)
  idx = index(value)
  return if idx.nil?

  idx + 1
end

#positions(value) ⇒ Object



178
179
180
# File 'lib/active_object/array.rb', line 178

def positions(value)
  indexes(value).map { |val| val + 1 }
end

#probabilityObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/active_object/array.rb', line 182

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

#promote(value) ⇒ Object



195
196
197
# File 'lib/active_object/array.rb', line 195

def promote(value)
  sort_by { |val| val == value ? -1 : 0 }
end

#promote!(value) ⇒ Object



199
200
201
# File 'lib/active_object/array.rb', line 199

def promote!(value)
  replace(promote(value))
end

#reject_values(*args) ⇒ Object



203
204
205
# File 'lib/active_object/array.rb', line 203

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

#rposition(value) ⇒ Object



207
208
209
210
211
212
# File 'lib/active_object/array.rb', line 207

def rposition(value)
  idx = rindex(value)
  return if idx.nil?

  idx + 1
end

#sample!Object



214
215
216
# File 'lib/active_object/array.rb', line 214

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

#split(number = nil) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/active_object/array.rb', line 219

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



243
244
245
# File 'lib/active_object/array.rb', line 243

def strip
  reject(&:blank?)
end

#strip!Object



247
248
249
# File 'lib/active_object/array.rb', line 247

def strip!
  replace(strip)
end

#swap(from, to) ⇒ Object



251
252
253
254
# File 'lib/active_object/array.rb', line 251

def swap(from, to)
  self[from], self[to] = self[to], self[from]
  self
end

#to(position) ⇒ Object



256
257
258
# File 'lib/active_object/array.rb', line 256

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

#to_sentence(options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/active_object/array.rb', line 261

def to_sentence(options = {})
  options = {
    words_connector: ', ',
    two_words_connector: ' and ',
    last_word_connector: ', and '
  }.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