Sha256: c6011d5347dfa4eb09f69d23bdaa1037f54ae322dd07d081125de6e3752c2fd5
Contents?: true
Size: 1.47 KB
Versions: 37
Compression:
Stored size: 1.47 KB
Contents
module Locomotive::Steam module Adapters module Memory class Order attr_reader :list def initialize(*spec) @list = [] spec.compact.each do |criterion| @list += (case criterion when Array then criterion when Hash then criterion.to_a when String then criterion.split(',').collect { |s| build(s.strip) } else [] end) end end def empty? @list.empty? end def apply_to(entry, locale) @list.collect do |(name, direction)| value = entry.send(name) if value.respond_to?(:translations) # localized value = value[locale] end asc?(direction) ? Asc.new(value) : Desc.new(value) end end def asc?(direction) direction.nil? || direction.to_sym == :asc end private def build(string) pattern = string.include?('.') ? '.' : ' ' string.downcase.split(pattern).map(&:to_sym) end class Direction attr_reader :obj def initialize(obj); @obj = obj; end end class Asc < Direction def <=>(other) @obj && other.obj ? @obj <=> other.obj : @obj ? -1 : 1; end end class Desc < Direction def <=>(other); @obj && other.obj ? other.obj <=> @obj : @obj ? -1 : 1; end end end end end end
Version data entries
37 entries across 37 versions & 1 rubygems