Class: Dux::Comparable::Attribute Private

Inherits:
Struct
  • Object
show all
Defined in:
lib/dux/comparable.rb

Overview

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

Attribute definition with sort ordering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (private)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



189
190
191
# File 'lib/dux/comparable.rb', line 189

def name
  @name
end

#orderObject (private)

Returns the value of attribute order

Returns:

  • (Object)

    the current value of order



189
190
191
# File 'lib/dux/comparable.rb', line 189

def order
  @order
end

Instance Method Details

#ascending?Boolean

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.

Check if the #order is ascending

Returns:

  • (Boolean)


191
192
193
# File 'lib/dux/comparable.rb', line 191

def ascending?
  order == :asc
end

#descending?Boolean

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.

Check if the #order is descending

Returns:

  • (Boolean)


196
197
198
# File 'lib/dux/comparable.rb', line 196

def descending?
  order == :desc
end

#to_comparison(wrap: false) ⇒ String

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.

Generate the comparison expression used to compare this attribute against another.

Parameters:

  • wrap (Boolean) (defaults to: false)

    if the expression should be wrapped in parentheses.

Returns:

  • (String)


216
217
218
219
220
221
222
223
224
# File 'lib/dux/comparable.rb', line 216

def to_comparison(wrap: false)
  if ascending?
    "self.#{name} <=> other.#{name}"
  elsif descending?
    "other.#{name} <=> self.#{name}"
  end.tap do |expression|
    return "(#{expression})" if wrap
  end
end

#to_inspect(many: false, default: nil) ⇒ String

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.

Parameters:

  • many (Boolean) (defaults to: false)
  • default (:asc, :desc, nil) (defaults to: nil)

Returns:

  • (String)


204
205
206
207
208
# File 'lib/dux/comparable.rb', line 204

def to_inspect(many: false, default: nil)
  ":#{name}".tap do |s|
    s << " #{order.to_s.upcase}" unless many && order == default
  end
end