Class: Arrow::SortKey

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/sort-key.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SortKey #initialize(name, order) ⇒ SortKey

Creates a new Arrow::SortKey.

Overloads:

  • #initialize(name) ⇒ SortKey

    Examples:

    String without the leading order mark

    key = Arrow::SortKey.new("count")
    key.name  # => "count"
    key.order # => Arrow::SortOrder::ASCENDING

    String with the “+” leading order mark

    key = Arrow::SortKey.new("+count")
    key.name  # => "count"
    key.order # => Arrow::SortOrder::ASCENDING

    String with the “-” leading order mark

    key = Arrow::SortKey.new("-count")
    key.name  # => "count"
    key.order # => Arrow::SortOrder::DESCENDING

    Symbol that starts with “-”

    key = Arrow::SortKey.new(:"-count")
    key.name  # => "-count"
    key.order # => Arrow::SortOrder::ASCENDING

    Parameters:

    • name (Symbol, String)

      The name of the sort column.

      If ‘name` is a String, the first character may be processed as the “leading order mark”. If the first character is `“+”` or `“-”`, they are processed as a leading order mark. If the first character is processed as a leading order mark, the first character is removed from sort column name and corresponding order is used. `“+”` uses ascending order and `“-”` uses ascending order.

      If ‘name` is not a String nor `name` doesn’t start with the leading order mark, sort column name is ‘name` as-is and ascending order is used.

  • #initialize(name, order) ⇒ SortKey

    Examples:

    No leading order mark processing

    key = Arrow::SortKey.new("-count", :ascending)
    key.name  # => "-count"
    key.order # => Arrow::SortOrder::ASCENDING

    Order by abbreviated name with Symbol

    key = Arrow::SortKey.new("count", :desc)
    key.name  # => "count"
    key.order # => Arrow::SortOrder::DESCENDING

    Order by String

    key = Arrow::SortKey.new("count", "descending")
    key.name  # => "count"
    key.order # => Arrow::SortOrder::DESCENDING

    Order by Arrow::SortOrder

    key = Arrow::SortKey.new("count", Arrow::SortOrder::DESCENDING)
    key.name  # => "count"
    key.order # => Arrow::SortOrder::DESCENDING

    Parameters:

    • name (Symbol, String)

      The name of the sort column.

      No leading order mark processing. The given ‘name` is used as-is.

    • order (Symbol, String, Arrow::SortOrder)

      How to order by this sort key.

      If this is a Symbol or String, this must be ‘:ascending`, `“ascending”`, `:asc`, `“asc”`, `:descending`, `“descending”`, `:desc` or `“desc”`.

Since:

  • 4.0.0



139
140
141
142
143
# File 'lib/arrow/sort-key.rb', line 139

def initialize(name, order=nil)
  name, order = normalize_name(name, order)
  order = normalize_order(order) || :ascending
  initialize_raw(name, order)
end

Class Method Details

.resolve(sort_key) ⇒ Arrow::SortKey .resolve(name) ⇒ Arrow::SortKey .resolve(name, order) ⇒ Arrow::SortKey

Ensure returning suitable Arrow::SortKey.

Overloads:

  • .resolve(sort_key) ⇒ Arrow::SortKey

    Returns the given sort key itself. This is convenient to use this method as Arrow::SortKey converter.

    Parameters:

    Returns:

  • .resolve(name) ⇒ Arrow::SortKey

    Creates a new suitable sort key from column name with leading order mark. See #initialize for details about order mark.

    Returns:

  • .resolve(name, order) ⇒ Arrow::SortKey

    Creates a new suitable sort key from column name without leading order mark and order. See #initialize for details.

    Returns:

Since:

  • 4.0.0



48
49
50
51
# File 'lib/arrow/sort-key.rb', line 48

def resolve(name, order=nil)
  return name if name.is_a?(self)
  new(name, order)
end

.try_convert(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
# File 'lib/arrow/sort-key.rb', line 54

def try_convert(value)
  case value
  when Symbol, String
    new(value.to_s, :ascending)
  else
    nil
  end
end

Instance Method Details

#to_sString

Returns The string representation of this sort key. You can use recreate Arrow::SortKey by ‘Arrow::SortKey.new(key.to_s)`.

Examples:

Recreate Arrow::SortKey

key = Arrow::SortKey.new("-count")
key.to_s # => "-count"
key == Arrow::SortKey.new(key.to_s) # => true

Returns:

  • (String)

    The string representation of this sort key. You can use recreate Arrow::SortKey by ‘Arrow::SortKey.new(key.to_s)`.

Since:

  • 4.0.0



155
156
157
158
159
160
161
# File 'lib/arrow/sort-key.rb', line 155

def to_s
  if order == SortOrder::ASCENDING
    "+#{name}"
  else
    "-#{name}"
  end
end