Module: Controll::Notify::Macros::ClassMethods

Defined in:
lib/controll/notify/macros.rb

Instance Method Summary collapse

Instance Method Details

#handler(type, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/controll/notify/macros.rb', line 6

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

#message(event_name, text = nil, &block) ⇒ Object Also known as: msg



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/controll/notify/macros.rb', line 35

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

#messages(hash = nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/controll/notify/macros.rb', line 25

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