Class: JayAPI::Elasticsearch::Count
- Inherits:
-
Object
- Object
- JayAPI::Elasticsearch::Count
- 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
-
#*(other) ⇒ Numeric
Multiplies the receiver's count by another value.
-
#+(other) ⇒ Numeric
Adds another value to the receiver's count.
-
#-(other) ⇒ Numeric
Subtracts another value from the receiver's count.
-
#/(other) ⇒ Numeric
Divides the receiver's count by another value.
-
#<=>(other) ⇒ -1, ...
Compares this count with another numeric value.
-
#coerce(other) ⇒ Array(Numeric, Integer)
Coerces another value so arithmetic operations can be performed with the receiver.
-
#count ⇒ Integer
The count returned by Elasticsearch.
-
#initialize(data) ⇒ Count
constructor
A new instance of Count.
-
#to_s ⇒ String
The count converted to a string.
Constructor Details
#initialize(data) ⇒ Count
Returns a new instance of Count.
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.
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.
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.
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.
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.
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.
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 |
#count ⇒ Integer
The count returned by Elasticsearch.
26 27 28 |
# File 'lib/jay_api/elasticsearch/count.rb', line 26 def count @count ||= data['count'] end |
#to_s ⇒ String
Returns 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 |