class Lutaml::UmlRepository::QueryDSL::Order
sorted = order.apply(classes)
order = Order.new(:name, :desc)
@example Descending order
sorted = order.apply(classes)
order = Order.new(:name, :asc)
@example Ascending order
Handles nil values by treating them as empty strings for comparison.
Sorts results based on a specified field and direction.
Order specification for sorting query results
def apply(results)
-
(Array)- The sorted collection
Parameters:
-
results(Array) -- The collection to sort
def apply(results) sorted = results.sort_by do |obj| extract_sort_value(obj) end @direction == :desc ? sorted.reverse : sorted end
def extract_sort_value(obj)
-
(Object)- The value to use for sorting
Parameters:
-
obj(Object) -- The object to extract value from
def extract_sort_value(obj) return "" unless obj.class.attributes.key?(@field.to_sym) value = obj.public_send(@field) normalize_value(value) end
def initialize(field, direction = :asc)
-
(ArgumentError)- if direction is invalid
Parameters:
-
direction(Symbol) -- The sort direction (:asc or :desc) -
field(Symbol, String) -- The field to sort by
def initialize(field, direction = :asc) @field = field.to_sym @direction = validate_direction(direction) end
def normalize_value(value)
-
(Object)- The normalized value
Parameters:
-
value(Object) -- The value to normalize
def normalize_value(value) case value when nil "" when String value.downcase else value end end
def validate_direction(direction)
-
(ArgumentError)- if direction is invalid
Returns:
-
(Symbol)- The validated direction
Parameters:
-
direction(Symbol) -- The direction to validate
def validate_direction(direction) dir = direction.to_sym unless VALID_DIRECTIONS.include?(dir) raise ArgumentError, "Invalid direction: #{direction}. " \ "Must be one of #{VALID_DIRECTIONS.join(', ')}" end dir end