Class: RailsLocaleSorter::OrderFact

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_locale_sorter.rb

Class Method Summary collapse

Class Method Details

.convert_and_sort(object, deep = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_locale_sorter.rb', line 14

def self.convert_and_sort(object, deep = false)
  # from http://seb.box.re/2010/1/15/deep-hash-ordering-with-ruby-1-8/
  if object.is_a?(Hash)
    # Hash is ordered in Ruby 1.9!
    res = self.returning(RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new) do |map|
      object.each {|k, v| map[k] = deep ? convert_and_sort(v, deep) : v }
    end
    return res.class[res.sort {|a, b| a[0].to_s <=> b[0].to_s } ]
  elsif deep && object.is_a?(Array)
    array = Array.new
    object.each_with_index {|v, i| array[i] = convert_and_sort(v, deep) }
    return array
  else
    return object
  end
end

.returning(value) {|value| ... } ⇒ Object

Yields:

  • (value)


9
10
11
12
# File 'lib/rails_locale_sorter.rb', line 9

def self.returning(value)
  yield(value)
  value
end