Module: Gemmy::Patches::ArrayPatch::InstanceMethods::Arrange

Defined in:
lib/gemmy/patches/array_patch.rb

Instance Method Summary collapse

Instance Method Details

#arrangeObject Also known as: rangify

facets creates ranges from array



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/gemmy/patches/array_patch.rb', line 215

def arrange
  array = uniq.sort_by { |e| Range === e ? e.first : e }
  array.inject([]) do |c, value|
    unless c.empty?
      last = c.last
      last_value    = (Range === last  ? last.last   : last)
      current_value = (Range === value ? value.first : value)
      if (last_value.succ <=> current_value) == -1
        c << value
      else
        first  = (Range === last ? last.first : last)
        second = [Range === last ? last.last : last, Range === value ? value.last : value].max
        c[-1] = [first..second]
        c.flatten!
      end
    else
      c << value
    end
  end
end