Class: JayAPI::Elasticsearch::Count

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/jay_api/elasticsearch/count.rb

Overview

Lightweight wrapper around an Elasticsearch count response.

It behaves like a numeric value for comparisons and arithmetic while preserving access to the original response payload.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Count

Returns a new instance of Count.

Parameters:

  • data (Hash)

    The raw Elasticsearch response that includes the count value.



20
21
22
# File 'lib/jay_api/elasticsearch/count.rb', line 20

def initialize(data)
  @data = data
end

Instance Method Details

#*(other) ⇒ Numeric

Multiplies the receiver's count by another value.

Parameters:

  • other (Numeric, #to_int)

    The value to multiply by.

Returns:

  • (Numeric)

    The arithmetic result.



81
82
83
84
# File 'lib/jay_api/elasticsearch/count.rb', line 81

def *(other)
  other, this = coerce(other)
  this * other
end

#+(other) ⇒ Numeric

Adds another value to the receiver's count.

Parameters:

  • other (Numeric, #to_int)

    The value to add.

Returns:

  • (Numeric)

    The arithmetic result.



65
66
67
68
# File 'lib/jay_api/elasticsearch/count.rb', line 65

def +(other)
  other, this = coerce(other)
  this + other
end

#-(other) ⇒ Numeric

Subtracts another value from the receiver's count.

Parameters:

  • other (Numeric, #to_int)

    The value to subtract.

Returns:

  • (Numeric)

    The arithmetic result.



73
74
75
76
# File 'lib/jay_api/elasticsearch/count.rb', line 73

def -(other)
  other, this = coerce(other)
  this - other
end

#/(other) ⇒ Numeric

Divides the receiver's count by another value.

Parameters:

  • other (Numeric, #to_int)

    The divisor.

Returns:

  • (Numeric)

    The arithmetic result.



89
90
91
92
# File 'lib/jay_api/elasticsearch/count.rb', line 89

def /(other)
  other, this = coerce(other)
  this / other
end

#<=>(other) ⇒ -1, ...

Compares this count with another numeric value. :reek:ManualDispatch -- Relies on the other object responding to to_int.

Parameters:

  • other (Numeric, #to_int)

    The value to compare with.

Returns:

  • (-1, 0, 1, nil)

    Comparison result, or nil when the values are not comparable.



52
53
54
55
56
57
58
59
60
# File 'lib/jay_api/elasticsearch/count.rb', line 52

def <=>(other)
  if other.is_a?(Numeric)
    to_int <=> other
  elsif other.respond_to?(:to_int)
    to_int <=> other.to_int
  else
    super
  end
end

#coerce(other) ⇒ Array(Numeric, Integer)

Coerces another value so arithmetic operations can be performed with the receiver. :reek:FeatureEnvy :reek:ManualDispatch -- Not much that can be done here, since this is a coercion method.

Parameters:

  • other (Numeric, #to_int)

    The other operand.

Returns:

  • (Array(Numeric, Integer))

    A two-element array in the form expected by Ruby coercion.

Raises:

  • (TypeError)

    If other cannot be coerced into an Integer.



37
38
39
40
41
42
43
44
45
# File 'lib/jay_api/elasticsearch/count.rb', line 37

def coerce(other)
  if other.is_a?(Numeric)
    [other, to_int]
  elsif other.respond_to?(:to_int)
    [other.to_int, to_int]
  else
    raise TypeError, "#{other.class} cannot be coerced into Integer"
  end
end

#countInteger

The count returned by Elasticsearch.

Returns:

  • (Integer)

    The raw count value from the response payload.



26
27
28
# File 'lib/jay_api/elasticsearch/count.rb', line 26

def count
  @count ||= data['count']
end

#to_sString

Returns The count converted to a string.

Returns:

  • (String)

    The count converted to a string.



95
96
97
# File 'lib/jay_api/elasticsearch/count.rb', line 95

def to_s
  count.to_s
end