Class: Nunes::Adapter
- Inherits:
-
Object
- Object
- Nunes::Adapter
- Defined in:
- lib/nunes/adapter.rb
Direct Known Subclasses
Constant Summary collapse
- ReplaceRegex =
Private: What Ruby uses to separate namespaces.
/[^a-z0-9\-_]+/i
- Separator =
Private: The default metric namespace separator.
"."
- RegexSeparator =
Regexp.escape(Separator)
- StartsOrEndsWithSeparator =
Private: Regex to match metric ending with separator.
/\A#{RegexSeparator}|#{RegexSeparator}\Z/
- Nothing =
Private
""
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Private.
Class Method Summary collapse
-
.adapters ⇒ Object
Private.
-
.wrap(client) ⇒ Object
Private: Wraps a given object with the correct adapter/decorator.
-
.wraps?(client) ⇒ Boolean
Private.
Instance Method Summary collapse
-
#increment(metric, value = 1) ⇒ Object
Internal: Increment a metric by a value.
-
#initialize(client) ⇒ Adapter
constructor
Internal: Sets the client for the adapter.
-
#prepare(metric) ⇒ Object
Private: Prepare a metric name before it is sent to the adapter’s client.
-
#timing(metric, duration) ⇒ Object
Internal: Record a metric’s duration.
Constructor Details
#initialize(client) ⇒ Adapter
Internal: Sets the client for the adapter.
client - The thing being adapted to a simple interface.
38 39 40 |
# File 'lib/nunes/adapter.rb', line 38 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Private
33 34 35 |
# File 'lib/nunes/adapter.rb', line 33 def client @client end |
Class Method Details
.adapters ⇒ Object
Private
28 29 30 |
# File 'lib/nunes/adapter.rb', line 28 def self.adapters [Nunes::Adapter, *subclasses] end |
.wrap(client) ⇒ Object
Private: Wraps a given object with the correct adapter/decorator.
client - The thing to be wrapped.
Returns Nunes::Adapter instance.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nunes/adapter.rb', line 8 def self.wrap(client) raise ArgumentError, "client cannot be nil" if client.nil? return client if client.is_a?(self) adapter = adapters.detect { |adapter| adapter.wraps?(client) } if adapter.nil? raise ArgumentError, "I have no clue how to wrap what you've given me (#{client.inspect})" end adapter.new(client) end |
.wraps?(client) ⇒ Boolean
Private
23 24 25 |
# File 'lib/nunes/adapter.rb', line 23 def self.wraps?(client) client.respond_to?(:increment) && client.respond_to?(:timing) end |
Instance Method Details
#increment(metric, value = 1) ⇒ Object
Internal: Increment a metric by a value. Override in subclass if client interface does not match.
44 45 46 |
# File 'lib/nunes/adapter.rb', line 44 def increment(metric, value = 1) @client.increment prepare(metric), value end |
#prepare(metric) ⇒ Object
Private: Prepare a metric name before it is sent to the adapter’s client.
69 70 71 72 73 74 |
# File 'lib/nunes/adapter.rb', line 69 def prepare(metric) metric = metric.to_s.gsub(ReplaceRegex, Separator) metric.squeeze!(Separator) metric.gsub!(StartsOrEndsWithSeparator, Nothing) metric end |
#timing(metric, duration) ⇒ Object
Internal: Record a metric’s duration. Override in subclass if client interface does not match.
50 51 52 |
# File 'lib/nunes/adapter.rb', line 50 def timing(metric, duration) @client.timing prepare(metric), duration end |