module ActiveObject::Array
Public Instance Methods
after(value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, rubocop:disable Style/GuardClause, Style/IfInsideElse
# 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_first()
click to toggle source
rubocop:enable Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
# File lib/active_object/array.rb, line 45 def delete_first self[1..-1] end
delete_first!()
click to toggle source
# File lib/active_object/array.rb, line 49 def delete_first! replace(delete_first) end
delete_last()
click to toggle source
# File lib/active_object/array.rb, line 53 def delete_last self[0...-1] end
delete_last!()
click to toggle source
# File lib/active_object/array.rb, line 57 def delete_last! replace(delete_last) end
delete_values(*args)
click to toggle source
# File lib/active_object/array.rb, line 61 def delete_values(*args) args.each_with_object([]) { |val, results| results << delete(val) } end
demote(value)
click to toggle source
# File lib/active_object/array.rb, line 65 def demote(value) sort_by { |val| val == value ? 0 : -1 } end
demote!(value)
click to toggle source
# File lib/active_object/array.rb, line 69 def demote!(value) replace(demote(value)) end
denillify(value = 0)
click to toggle source
# File lib/active_object/array.rb, line 73 def denillify(value = 0) map { |val| val.nil? ? value : val } end
denillify!(value = 0)
click to toggle source
# File lib/active_object/array.rb, line 77 def denillify!(value = 0) replace(denillify(value)) end
dig(key, *rest)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/active_object/array.rb, line 95 def from(position) self[position, length] || [] end
fulfill(value, amount)
click to toggle source
# 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)
click to toggle source
# 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) { |val| ... }
click to toggle source
rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
# 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) { |val| ... }
click to toggle source
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# 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)
click to toggle source
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
# 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)
click to toggle source
# File lib/active_object/array.rb, line 158 def merge(*values) values.each { |val| concat(val) } self end
nillify()
click to toggle source
# 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!()
click to toggle source
# File lib/active_object/array.rb, line 167 def nillify! replace(nillify) end
position(value)
click to toggle source
# File lib/active_object/array.rb, line 171 def position(value) idx = index(value) return if idx.nil? idx + 1 end
positions(value)
click to toggle source
# File lib/active_object/array.rb, line 178 def positions(value) indexes(value).map { |val| val + 1 } end
probability()
click to toggle source
# 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)
click to toggle source
# File lib/active_object/array.rb, line 195 def promote(value) sort_by { |val| val == value ? -1 : 0 } end
promote!(value)
click to toggle source
# File lib/active_object/array.rb, line 199 def promote!(value) replace(promote(value)) end
reject_values(*args)
click to toggle source
# File lib/active_object/array.rb, line 203 def reject_values(*args) reject { |val| args.include?(val) } end
rposition(value)
click to toggle source
# File lib/active_object/array.rb, line 207 def rposition(value) idx = rindex(value) return if idx.nil? idx + 1 end
sample!()
click to toggle source
# File lib/active_object/array.rb, line 214 def sample! delete_at(::Random.rand(length - 1)) end
split(number = nil) { |element| ... }
click to toggle source
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# 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
strip()
click to toggle source
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
# File lib/active_object/array.rb, line 243 def strip reject(&:blank?) end
strip!()
click to toggle source
# File lib/active_object/array.rb, line 247 def strip! replace(strip) end
swap(from, to)
click to toggle source
# File lib/active_object/array.rb, line 251 def swap(from, to) self[from], self[to] = self[to], self[from] self end
to(position)
click to toggle source
# File lib/active_object/array.rb, line 256 def to(position) position >= 0 ? first(position + 1) : self[0..position] end
to_sentence(options = {})
click to toggle source
rubocop:disable Metrics/MethodLength
# 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