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(nil) 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(nil) 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 { |v| result << delete(v) }
  result
end

#dig(key, *rest) ⇒ Object



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

def dig(key, *rest)
  if value = (self[key] rescue nil)
    if rest.empty?
      value
    elsif value.respond_to?(:dig)
      value.dig(*rest)
    end
  end
end

#duplicates(minimum = 2) ⇒ Object



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

def duplicates(minimum=2)
  hash = Hash.new(0)

  each { |i| hash[i] += 1 }
  hash.delete_if { |k, v| v < minimum }.keys
end

#from(position) ⇒ Object



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

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

#groups(number) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/active_object/array.rb', line 58

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

  n, r = length.divmod(number)
  collection = (0..(n - 1)).collect { |i| self[(i * number), number] }
  r > 0 ? collection << self[-r, r] : collection
end

#in_groups(number, fill_with = nil) ⇒ Object



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

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 |i|
    grouping = division + (modulo > 0 && modulo > i ? 1 : 0)
    collection << last_group = slice(start, grouping)
    last_group << fill_with if fill_with != false && modulo > 0 && grouping == division
    start += grouping
  end

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

#in_groups_of(number, fill_with = nil) ⇒ Object



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

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

  block_given? ? collection.each_slice(number) { |slice| yield(slice) } : collection.each_slice(number).to_a
end

#percentile(percentage) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/active_object/array.rb', line 99

def percentile(percentage)
  size = self.size

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

#probabilityObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_object/array.rb', line 110

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

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

  hash.keys.each { |e| hash[e] /= differ }
  hash
end

#reject_values(*args) ⇒ Object



123
124
125
# File 'lib/active_object/array.rb', line 123

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

#sample!Object



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

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

#split(number = nil) ⇒ Object



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

def split(number=nil)
  if block_given?
    inject([[]]) do |results, element|
      yield(element) ? (results << []) : (results.last << element)
      results
    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



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

def strip
  reject { |v| v.blank? }
end

#strip!Object



156
157
158
# File 'lib/active_object/array.rb', line 156

def strip!
  replace(strip)
end

#to(position) ⇒ Object



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

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

#to_sentence(options = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/active_object/array.rb', line 164

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