Class: Array

Inherits:
Object show all
Defined in:
lib/sinarey_support/core_ext/array/wrap.rb,
lib/sinarey_support/core_ext/array/access.rb,
lib/sinarey_support/core_ext/object/blank.rb,
lib/sinarey_support/core_ext/array/grouping.rb,
lib/sinarey_support/core_ext/object/to_param.rb,
lib/sinarey_support/core_ext/object/to_query.rb,
lib/sinarey_support/core_ext/array/extract_options.rb,
lib/sinarey_support/core_ext/array/prepend_and_append.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap(object) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/sinarey_support/core_ext/array/wrap.rb', line 2

def self.wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end

Instance Method Details

#extract_options!Object



8
9
10
11
12
13
14
# File 'lib/sinarey_support/core_ext/array/extract_options.rb', line 8

def extract_options!
  if last.is_a?(Hash) && last.extractable_options?
    pop
  else
    {}
  end
end

#fifthObject



15
16
17
# File 'lib/sinarey_support/core_ext/array/access.rb', line 15

def fifth
  self[4]
end

#fourthObject



11
12
13
# File 'lib/sinarey_support/core_ext/array/access.rb', line 11

def fourth
  self[3]
end

#in_groups(number, fill_with = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinarey_support/core_ext/array/grouping.rb', line 19

def in_groups(number, fill_with = nil)
  division = size.div number
  modulo = size % number
  groups = []
  start = 0
  number.times do |index|
    length = division + (modulo > 0 && modulo > index ? 1 : 0)
    groups << last_group = slice(start, length)
    last_group << fill_with if fill_with != false &&
      modulo > 0 && length == division
    start += length
  end
  if block_given?
    groups.each { |g| yield(g) }
  else
    groups
  end
end

#in_groups_of(number, fill_with = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sinarey_support/core_ext/array/grouping.rb', line 3

def in_groups_of(number, fill_with = nil)
  if fill_with == false
    collection = self
  else
    padding = (number - size % number) % number
    collection = dup.concat([fill_with] * padding)
  end
  if block_given?
    collection.each_slice(number) { |slice| yield(slice) }
  else
    groups = []
    collection.each_slice(number) { |group| groups << group }
    groups
  end
end

#secondObject



3
4
5
# File 'lib/sinarey_support/core_ext/array/access.rb', line 3

def second
  self[1]
end

#split(value = nil, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/sinarey_support/core_ext/array/grouping.rb', line 38

def split(value = nil, &block)
  inject([[]]) do |results, element|
    if block && block.call(element) || value == element
      results << []
    else
      results.last << element
    end
    results
  end
end

#thirdObject



7
8
9
# File 'lib/sinarey_support/core_ext/array/access.rb', line 7

def third
  self[2]
end

#to_paramObject



26
27
28
# File 'lib/sinarey_support/core_ext/object/to_param.rb', line 26

def to_param
  collect { |e| e.to_param }.join '/'
end

#to_query(key) ⇒ Object



11
12
13
14
# File 'lib/sinarey_support/core_ext/object/to_query.rb', line 11

def to_query(key)
  prefix = "#{key}[]"
  collect { |value| value.to_query(prefix) }.join '&'
end