Sha256: 136090ff031fe62c65f0dee7445d2fae1c1cfb3fafc849b87c13bb54e42c0593

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module Controll::Notify
  module Macros
    extend ActiveSupport::Concern

    module ClassMethods
      def handler type, options = {}, &block
        unless valid_type? type
          raise ArgumentError, "Notification type must be any of: #{types}, was: #{type}" 
        end

        clazz_name = "#{parent}::#{type.to_s.camelize}Handler"
        parent = options[:parent] || Controll::Notify::Base

        clazz = parent ? Class.new(parent) : Class.new
        Object.const_set clazz_name, clazz
        context = self.kind_of?(Class) ? self : self.class
        clazz = context.const_get(clazz_name)

        if block_given?
          clazz.instance_eval &block      
        end
        clazz
      end

      def messages hash = nil, &block
        unless block_given? || !hash.blank?
          raise ArgumentError, "Must be called with non-empty Hash or block" 
        end

        define_method :messages do
          block_given? ? instance_eval(&block) : hash
        end
      end

      def message event_name, text = nil, &block
        unless event_name.kind_of?(Symbol) || event_name.kind_of?(String)
          raise ArgumentError, "First argument must be an event name, was: #{event_name}"
        end
        
        unless block_given? || !text.blank?
          raise ArgumentError, "Must be called with non-empty String or block" 
        end
       
        define_method event_name do
          block_given? ? instance_eval(&block) : hash
        end
      end
      alias_method :msg, :message

      protected

      def valid_type? type
        types.include? type.to_sym
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
controll-0.3.2 lib/controll/notify/macros.rb
controll-0.3.1 lib/controll/notify/macros.rb
controll-0.3.0 lib/controll/notify/macros.rb