Module: Typelizer::PropertySorter

Defined in:
lib/typelizer/property_sorter.rb

Class Method Summary collapse

Class Method Details

.sort(props, sort_order) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/typelizer/property_sorter.rb', line 5

def self.sort(props, sort_order)
  case sort_order
  when :none, nil
    props
  when :alphabetical
    props.sort_by { |p| p.name.to_s.downcase }
  when :id_first_alphabetical
    props.sort_by { |p| [(p.name.to_s.downcase == "id") ? 0 : 1, p.name.to_s.downcase] }
  when Proc
    result = sort_order.call(props)
    result.is_a?(Array) ? result : props
  else
    props
  end
rescue => e
  Typelizer.logger.warn("PropertySorter error: #{e.message}, preserving original order")
  props
end