lib/yodeler.rb in yodeler-0.0.10 vs lib/yodeler.rb in yodeler-0.1.1

- old
+ new

@@ -1,122 +1,57 @@ -# encoding: utf-8 -require "yodeler/engine" -require "yodeler/listens_to_yodeler" +require "yaml" +require 'forwardable' -module Yodeler - autoload :Configuration, 'yodeler/configuration' - autoload :EventType, 'yodeler/models/event_type' - autoload :Event, 'yodeler/models/event' - autoload :Subscription, 'yodeler/models/subscription' - autoload :Notification, 'yodeler/models/notification' - autoload :ListensToYodeler, 'yodeler/listens_to_yodeler' - autoload :EventType, 'yodeler/models/event_type' +require "yodeler/version" +require "yodeler/endpoint" +require "yodeler/client" +require "yodeler/metric" - mattr_accessor :registrations - self.registrations = {} +require "yodeler/duplicate_endpoint_name_error" +require "yodeler/adapter_not_registered_error" - # Configuring the yodeler gem - def self.configuration - @configuration ||= Configuration.new - end +module Yodeler + class << self + extend Forwardable + def_delegators :@client, :gauge, :increment, :timing, :publish - def self.configure - yield configuration - end - - # Set the ActiveRecord table name prefix to yodeler_ - # - # This makes 'events' into 'yodeler_events' and also respects any - # 'global' table_name_prefix set on ActiveRecord::Base. - def self.table_name_prefix - "#{ActiveRecord::Base.table_name_prefix}yodeler_" - end - - # Creates an event_type model e.g.: Yodeler::#{event_type}EventType < Yodeler::EventType::Base - # - # @note - # you could also create your own class inheriting #{Yodeler::EventType::Base} - def self.register(event_type,&block) - event_type_klass = Yodeler.define_event_type(event_type) - - if block_given? - yield event_type_klass.configuration + # + # @private + def setup! + @client = nil + @registered_adapters = {} end - # keep track of the registrations - Yodeler.registrations[event_type] = event_type_klass - event_type_klass - end - - # Dispatches an event - # @param [Symbol] event_type the registered event type - # @param [Hash] payload a hash of values to serialize with the event - # - def self.dispatch(event_type, payload={}) - event_type_klass = Yodeler.registrations[event_type] - - # If the class wasn't registered dispatch the default class noop - if event_type_klass.nil? - # Register it the first time - Yodeler.register(:noop) if Yodeler.registrations[:noop].nil? - - event_type_klass = Yodeler::EventType::NoopEventType + def register_adapter(name,klass) + @registered_adapters[name] = klass end - started_at = nil - finished_at = nil - - if block_given? - started_at = Time.now - yield payload - finished_at = Time.now + # + # @private + def registered_adapters(name) + klass = @registered_adapters[name] + if !klass + raise AdapterNotRegisteredError.new(name: name) + end + @registered_adapters[name] end - event_type_klass.yodel!({ - started_at: started_at, - finished_at: finished_at, - payload: payload - }) - end - - # Clears all the registrations and removes the EventType classes - def self.flush_registrations! - Yodeler.registrations.keys.each do |event_type| - Yodeler.undefine_event_type(event_type) + # @private + def reset! + setup! end - Yodeler.registrations = {} - end - private - def self.event_type_class_name(event_type) - "#{event_type.to_s.classify}EventType" + def client + @client end - def self.undefine_event_type(event_type) - event_klass = Yodeler.registrations[event_type] - if event_klass && ( defined?( event_klass ) == 'constant' ) - Yodeler::EventType.instance_eval{ - remove_const( event_type.to_s.classify.to_sym ) - } - end + def configure + @client = Yodeler::Client.new + yield @client + @client end - - def self.define_event_type(event_type) - undefine_event_type(event_type) - - # Define an ancestor event type of Yodeler::EventType::Base and - # add AS Configurable to it - new_event_type_class = Class.new(Yodeler::EventType::Base) do - end - - # Define the event type class, e.g.: Yodeler::EventType::UserViewedEventType - Yodeler::EventType.const_set(Yodeler.event_type_class_name(event_type), new_event_type_class) - end - - # def self.cleanup!(hard=false) - # # defunct or delete any event_types, et al that aren't currently registered - # if hard - # else - # end - # end + end end +Yodeler.setup! +require "yodeler/adapters/memory_adapter" +require "yodeler/adapters/http_adapter"