summaryrefslogtreecommitdiffstats
path: root/src/arrow/ruby/red-arrow/lib/arrow/sort-options.rb
blob: a7c2d6431f4219ec28f71c67e633f7a7f3aab38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

module Arrow
  class SortOptions
    class << self
      # @api private
      def try_convert(value)
        case value
        when Symbol, String
          new(value)
        when ::Array
          new(*value)
        else
          nil
        end
      end
    end

    alias_method :initialize_raw, :initialize
    private :initialize_raw
    # @param sort_keys [::Array<String, Symbol, Arrow::SortKey>] The
    #   sort keys to be used. See {Arrow::SortKey.resolve} how to
    #   resolve each sort key in `sort_keys`.
    #
    #   You can add more sort keys by {#add_sort_key} later.
    #
    # @example No initial sort keys
    #   options = Arrow::SortOptions.new
    #   options.sort_keys # => []
    #
    # @example String sort keys
    #   options = Arrow::SortOptions.new("count", "-age")
    #   options.sort_keys.collect(&:to_s) # => ["+count", "-age"]
    #
    # @example Symbol sort keys
    #   options = Arrow::SortOptions.new(:count, :age)
    #   options.sort_keys.collect(&:to_s) # => ["+count", "+age"]
    #
    # @example Mixed sort keys
    #   options = Arrow::SortOptions.new(:count, "-age")
    #   options.sort_keys.collect(&:to_s) # => ["+count", "-age"]
    #
    # @since 4.0.0
    def initialize(*sort_keys)
      initialize_raw
      sort_keys.each do |sort_key|
        add_sort_key(sort_key)
      end
    end

    # @api private
    alias_method :add_sort_key_raw, :add_sort_key
    # Add a sort key.
    #
    # @return [void]
    #
    # @overload add_sort_key(key)
    #
    #   @param key [Arrow::SortKey] The sort key to be added.
    #
    #   @example Add a key to sort by "price" column in descending order
    #     options = Arrow::SortOptions.new
    #     options.add_sort_key(Arrow::SortKey.new(:price, :descending))
    #     options.sort_keys.collect(&:to_s) # => ["-price"]
    #
    # @overload add_sort_key(name)
    #
    #   @param name [Symbol, String] The sort key name to be
    #     added. See also {Arrow::SortKey#initialize} for the leading
    #     order mark for String name.
    #
    #   @example Add a key to sort by "price" column in descending order
    #     options = Arrow::SortOptions.new
    #     options.add_sort_key("-price")
    #     options.sort_keys.collect(&:to_s) # => ["-price"]
    #
    # @overload add_sort_key(name, order)
    #
    #   @param name [Symbol, String] The sort key name.
    #
    #   @param order [Symbol, String, Arrow::SortOrder] The sort
    #     order. See {Arrow::SortKey#initialize} for details.
    #
    #   @example Add a key to sort by "price" column in descending order
    #     options = Arrow::SortOptions.new
    #     options.add_sort_key("price", :desc)
    #     options.sort_keys.collect(&:to_s) # => ["-price"]
    #
    # @since 4.0.0
    def add_sort_key(name, order=nil)
      add_sort_key_raw(SortKey.resolve(name, order))
    end
  end
end