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

Version Path
locomotivecms_steam-1.8.0.alpha2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.8.0.alpha1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.7.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/adapters/memory/order.rb