Class: Beaneater::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/beaneater/stats.rb

Overview

Represents stats related to the beanstalkd pool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Stats

Creates new stats instance.

Examples:

Beaneater::Stats.new(@client)

Parameters:

  • client (Beaneater)

    The beaneater client instance.



18
19
20
# File 'lib/beaneater/stats.rb', line 18

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Defines a cached method for looking up data for specified key Protects against infinite loops by checking stacktrace



55
56
57
58
59
60
61
62
63
64
# File 'lib/beaneater/stats.rb', line 55

def method_missing(name, *args, &block)
  if caller.first !~ /`(method_missing|data')/ && data.keys.include?(name.to_s)
    self.class.class_eval <<-CODE, __FILE__, __LINE__
      def #{name}; data[#{name.inspect}]; end
    CODE
    data[name.to_s]
  else # no key matches or caught infinite loop
    super
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/beaneater/stats.rb', line 10

def client
  @client
end

Instance Method Details

#[](key) ⇒ String, Integer

Returns value for specified key.

Examples:

@bp.stats['total_connections'] # => 4

Parameters:

  • key (String, Symbol)

    Name of key to retrieve

Returns:

  • (String, Integer)

    Value of specified key



40
41
42
# File 'lib/beaneater/stats.rb', line 40

def [](key)
  data[key]
end

#dataBeaneater::StatStruct (protected)

Returns struct based on stats data from response.

Examples:

self.data # => { 'version' : 1.7, 'total_connections' : 23 }
self.data.total_connections # => 23

Returns:



75
76
77
# File 'lib/beaneater/stats.rb', line 75

def data
  StatStruct.from_hash(client.connection.transmit('stats')[:body])
end

#inspectString Also known as: to_s

Delegates inspection to the real data structure

Returns:

  • (String)

    returns a string containing a detailed stats summary



47
48
49
# File 'lib/beaneater/stats.rb', line 47

def inspect
  data.to_s
end

#keysArray<String>

Returns keys for stats data

Examples:

@bp.stats.keys # => ["version", "total_connections"]

Returns:

  • (Array<String>)

    Set of keys for stats.



29
30
31
# File 'lib/beaneater/stats.rb', line 29

def keys
  data.keys
end