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(target) ⇒ SortKey #initialize(target, order) ⇒ SortKey

Creates a new Arrow::SortKey.

Overloads:

  • #initialize(target) ⇒ SortKey

    Examples:

    String without the leading order mark

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

    String with the “+” leading order mark

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

    String with the “-” leading order mark

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

    Symbol that starts with “-”

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

    Parameters:

    • target (Symbol, String)

      The name or dot path of the sort column.

      If ‘target` 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 target and corresponding order is used. `“+”` uses ascending order and `“-”` uses ascending order.

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

  • #initialize(target, order) ⇒ SortKey

    Examples:

    No leading order mark processing

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

    Order by abbreviated target with Symbol

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

    Order by String

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

    Order by Arrow::SortOrder

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

    Parameters:

    • target (Symbol, String)

      The name or dot path of the sort column.

      No leading order mark processing. The given ‘target` 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



142
143
144
145
146
# File 'lib/arrow/sort-key.rb', line 142

def initialize(target, order=nil)
  target, order = normalize_target(target, order)
  order = normalize_order(order) || :ascending
  initialize_raw(target, order)
end

Class Method Details

.resolve(sort_key) ⇒ Arrow::SortKey .resolve(target) ⇒ Arrow::SortKey .resolve(target, 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(target) ⇒ Arrow::SortKey

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

    Returns:

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

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

    Returns:

Since:

  • 4.0.0



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

def resolve(target, order=nil)
  return target if target.is_a?(self)
  new(target, 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.



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

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



158
159
160
161
162
163
164
# File 'lib/arrow/sort-key.rb', line 158

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