Sha256: 7208418e60ae4ca7ded9216a8db2b51de2ee0fa6be7b76c454220df837d24eb2

Contents?: true

Size: 985 Bytes

Versions: 4

Compression:

Stored size: 985 Bytes

Contents

module Linner
  module HashRecursiveMerge
    def rmerge!(other_hash)
      merge!(other_hash) do |key, oldval, newval|
        oldval.class == self.class ? oldval.rmerge!(newval) : newval
      end
    end
  end

  module Order
    def order_by(ary)
      ary << "..." if not ary.include? "..."
      order_ary = ary.inject([[]]) do |a, x|
        x != "..." ? a.last << x : a<< []; a
      end
      order_by_before(self, order_ary.first)
      order_by_after(self, order_ary.last)
      self
    end

  private
    def order_by_before(list, before)
      before.reverse.each do |f|
        if i = list.index {|x| x =~ /#{f}/i}
          list.unshift list.delete_at i
        end
      end
    end

    def order_by_after(list, after)
      after.reverse.each do |f|
        if i = list.index {|x| x =~ /#{f}/i}
          list.push list.delete_at i
        end
      end
    end
  end
end

class Hash
  include Linner::HashRecursiveMerge
end

class Array
  include Linner::Order
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
linner-0.4.0 lib/linner/helper.rb
linner-0.3.2 lib/linner/helper.rb
linner-0.3.1 lib/linner/helper.rb
linner-0.3.0 lib/linner/helper.rb