Sha256: 3aaa2431c2d29069ef633cee1a666cf7ee761b0c54e2cbd9f3d8c7ddabc0ce9e

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 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)
            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; end
        end

        class Desc < Direction
          def <=>(other); other.obj <=> @obj; end
        end

      end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.beta.2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.alpha.3 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.alpha.2 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.alpha.1 lib/locomotive/steam/adapters/memory/order.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/adapters/memory/order.rb