class Array

Public Instance Methods

convert_boundaries(options = {}) click to toggle source
# File lib/vidibus/core_extensions/array.rb, line 88
def convert_boundaries(options = {})
  prefix = options[:prefix] || "__a"
  i = 0
  boundaries = select {|a| a.match(/#{prefix}\d+/) if a.is_a?(String)}
  return self unless boundaries.any?
  array = self.dup
  converted = []
  for boundary in boundaries.uniq
    converted.concat(array.slice!(0, array.index(boundary)))
    array.shift
    converted << array.slice!(0, array.index(boundary))
    array.shift
  end
  converted.concat(array)
end
delete_rec(obj, &block) click to toggle source

Deletes items from self and nested arrays that are equal to obj. If any items are found, returns obj. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found.

Examples:

list = ["one", "two", ["one"]]
list.delete_rec("one") #=> "one"
list #=> ["two", []]
# File lib/vidibus/core_extensions/array.rb, line 145
def delete_rec(obj, &block)
  out = nil
  _self = self
  _self.each do |item|
    _out = nil
    if item.class == Array
      _out = item.delete_rec(obj, &block)
    else
      _out = _self.delete(obj, &block)
    end
    out ||= _out
  end
  self.replace(_self)
  out
end
flatten_once() click to toggle source

Flattens first level.

Example:

[1, [2, [3]]].flatten_once  # => [1, 2, [3]]

Inspired by: snippets.dzone.com/posts/show/302#comment-1417

# File lib/vidibus/core_extensions/array.rb, line 11
def flatten_once
  inject([]) do |v,e|
    if e.is_a?(Array)
      v.concat(e)
    else
      v << e
    end
  end
end
flatten_with_boundaries(options = {}) click to toggle source
# File lib/vidibus/core_extensions/array.rb, line 72
def flatten_with_boundaries(options = {})
  prefix = options[:prefix] || "__a"
  i = -1
  inject([]) do |v,e|
    if e.is_a?(Array)
      i += 1
      k = "#{prefix}#{i}"
      v << k
      v.concat(e)
      v << k
    else
      v << e
    end
  end
end
merge(array, options = {}) click to toggle source

Merges given array with current one.

Will perform insertion of new items by three rules:

  1. If the item's predecessor is present, insert item after it.

  2. If the item's follower is present, insert item before it.

  3. Insert item at end of list.

Examples:

[].merge([1, 2])            # => [1, 2]
['a'].merge([1, 2])         # => ['a', 1, 2]
[1, 'a'].merge([1, 2])      # => [1, 2, 'a']
[1, 'a'].merge([3, 1, 2])   # => [3, 1, 2, 'a']
# File lib/vidibus/core_extensions/array.rb, line 35
def merge(array, options = {})
  strict = options[:strict]
  exclude = options[:exclude] || []
  list = array.dup
  for value in list
    array.delete(value) and next if include?(value) or exclude.include?(value)
    position = nil
    if found = merging_predecessor(value, list)
      position = index(found) + 1
    elsif found = merging_follower(value, list)
      position = index(found)
    end
    if position or !strict
      insert(position || length, value)
      array.delete(value)
    end
  end
  self
end
merge_nested(array) click to toggle source
# File lib/vidibus/core_extensions/array.rb, line 55
def merge_nested(array)
  combined = self.dup
  append = []
  array.each_with_index do |a,i|
    source = a.dup
    combined.each_with_index do |c,j|
      break if source.empty?
      existing = combined.flatten_once - c
      combined[j] = c.merge(source, :strict => true, :exclude => existing)
    end
    if source.any?
      combined[i] ? combined[i].concat(source) : combined.last.concat(source)
    end
  end
  combined
end
sort_by_map(map, attribute = nil) click to toggle source

Sorts array of values, hashes or objects by given order. In order to sort hashes or objects, an attribute is required.

Examples:

list = ["two", "one", "three"]
map = ["one", "two", "three"]
list.sort_by_map(map)
# => ["one", "two", "three"]

list = [{:n => "two"}, {:n => "one"}, {:n => "three"}]
map = ["one", "two", "three"]
list.sort_by_map(map, :n)
# => [{:n => "one"}, {:n => "two"}, {:n => "three"}]
# File lib/vidibus/core_extensions/array.rb, line 119
def sort_by_map(map, attribute = nil)
  ordered = []
  for a in map
    ordered << self.detect do |m|
      if attribute
        n = m.is_a?(Hash) ? m[attribute] : m.send(attribute)
        a == n
      else
        a == m
      end
    end
  end
  ordered.compact
end

Private Instance Methods

merging_follower(needle, haystack) click to toggle source

Returns follower of given needle from haystack. Helper method for merge

# File lib/vidibus/core_extensions/array.rb, line 172
def merging_follower(needle, haystack)
  list = haystack[haystack.index(needle)+1..-1]
  (list & self).first
end
merging_predecessor(needle, haystack) click to toggle source

Returns predecessor of given needle from haystack. Helper method for merge

# File lib/vidibus/core_extensions/array.rb, line 165
def merging_predecessor(needle, haystack)
  list = haystack[0..haystack.index(needle)].reverse
  (list & self).first
end