Sha256: 2978aa9e3024e2847b6a24537906960c24db18f1a4d8b544b402ff2b4fba979e

Contents?: true

Size: 1.21 KB

Versions: 50

Compression:

Stored size: 1.21 KB

Contents

module Appfuel
  module Repository
    class OrderExpr < Expr

      def self.build(data)
        return [data] if data.instance_of?(self)
        data = [data] if data.is_a?(String)
        unless data.respond_to?(:each)
          fail "order must be a string or implement :each"
        end

        results = []
        data.each do |item|
          item = parse_order_string(item) if item.is_a?(String)
          if item.instance_of?(self)
            results << item
            next
          end

          if !item.is_a?(Hash)
            fail "order array must be a list of strings or hashes"
          end
          domain_attr, dir = item.first
          results << self.new(domain_attr, dir)
        end
        results
      end

      def initialize(domain_attr, op = nil)
        op ||= 'asc'

        super(domain_attr, op, nil)
        @op = @op.downcase
        unless ['asc', 'desc'].include?(@op)
          fail "order direction must be either asc or desc"
        end
      end

      def to_s
        "#{attr_list.join('.')} #{op}"
      end

      private
      def self.parse_order_string(str)
        str, dir = str.split(' ')
         dir = 'asc' if dir.nil?
         {str => dir.downcase}
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
appfuel-0.7.0 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.16 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.15 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.14 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.13 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.12 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.11 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.10 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.9 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.8 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.7 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.6 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.5 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.4 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.3 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.6.1 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.5.16 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.5.15 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.5.14 lib/appfuel/storage/repository/order_expr.rb
appfuel-0.5.13 lib/appfuel/storage/repository/order_expr.rb