Sha256: 9e9ef1f68b1dd23125605d9c715fb94a3382a58727a0a7bc531a5cb6b541468f
Contents?: true
Size: 841 Bytes
Versions: 4
Compression:
Stored size: 841 Bytes
Contents
# frozen_string_literal: true require_relative 'sort/direction' module Realize class Collection # Transformer to take an array of oibjects and sort by the given key # and by the given sort direction. Defaulting to ascending. class Sort include Arrays include Direction acts_as_hashable DEFAULT_ORDER = ASCENDING attr_reader :key, :order def initialize(key:, order: DEFAULT_ORDER) raise ArgumentError, 'key is required' if key.to_s.empty? @key = key @order = Direction.const_get(order.to_s.upcase.to_sym) freeze end def transform(_resolver, value, _time, _record) records = array(value) sorted = records.sort_by { |hsh| hsh[key.to_sym] } order == DESCENDING ? sorted.reverse : sorted end end end end
Version data entries
4 entries across 4 versions & 1 rubygems