Class: AbstractNotifier::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_notifier/base.rb

Overview

Base class for notifiers

Defined Under Namespace

Classes: ParamsProxy

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification_name, **params) ⇒ Base

Returns a new instance of Base.



150
151
152
153
# File 'lib/abstract_notifier/base.rb', line 150

def initialize(notification_name, **params)
  @notification_name = notification_name
  @params = params.freeze
end

Class Attribute Details

.driverObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/abstract_notifier/base.rb', line 54

def driver
  return @driver if instance_variable_defined?(:@driver)

  @driver =
    if superclass.respond_to?(:driver)
      superclass.driver
    else
      raise "Driver not found for #{name}. " \
            "Please, specify driver via `self.driver = MyDriver`"
    end
end

Instance Attribute Details

#notification_nameObject (readonly)

Returns the value of attribute notification_name.



148
149
150
# File 'lib/abstract_notifier/base.rb', line 148

def notification_name
  @notification_name
end

#paramsObject (readonly)

Returns the value of attribute params.



148
149
150
# File 'lib/abstract_notifier/base.rb', line 148

def params
  @params
end

Class Method Details

.action_methodsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/abstract_notifier/base.rb', line 132

def action_methods
  @action_methods ||= begin
    # All public instance methods of this class, including ancestors
    methods = (public_instance_methods(true) -
      # Except for public instance methods of Base and its ancestors
      Base.public_instance_methods(true) +
      # Be sure to include shadowed public instance methods of this class
      public_instance_methods(false))

    methods.map!(&:to_s)

    methods.to_set
  end
end

.async_adapterObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/abstract_notifier/base.rb', line 71

def async_adapter
  return @async_adapter if instance_variable_defined?(:@async_adapter)

  @async_adapter =
    if superclass.respond_to?(:async_adapter)
      superclass.async_adapter
    else
      AbstractNotifier.async_adapter
    end
end

.async_adapter=(args) ⇒ Object



66
67
68
69
# File 'lib/abstract_notifier/base.rb', line 66

def async_adapter=(args)
  adapter, options = Array(args)
  @async_adapter = AsyncAdapters.lookup(adapter, options)
end

.default(method_name = nil, **hargs, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/abstract_notifier/base.rb', line 82

def default(method_name = nil, **hargs, &block)
  return @defaults_generator = block if block_given?

  return @defaults_generator = proc { send(method_name) } unless method_name.nil?

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.merge(hargs).freeze
    else
      hargs.freeze
    end
end

.default_paramsObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/abstract_notifier/base.rb', line 104

def default_params
  return @default_params if instance_variable_defined?(:@default_params)

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.dup
    else
      {}
    end
end

.defaults_generatorObject



95
96
97
98
99
100
101
102
# File 'lib/abstract_notifier/base.rb', line 95

def defaults_generator
  return @defaults_generator if instance_variable_defined?(:@defaults_generator)

  @defaults_generator =
    if superclass.respond_to?(:defaults_generator)
      superclass.defaults_generator
    end
end

.method_missing(method_name, *args) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/abstract_notifier/base.rb', line 115

def method_missing(method_name, *args)
  if action_methods.include?(method_name.to_s)
    new(method_name).public_send(method_name, *args)
  else
    super
  end
end

.respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/abstract_notifier/base.rb', line 127

def respond_to_missing?(method_name, _include_private = false)
  action_methods.include?(method_name.to_s) || super
end

.with(params) ⇒ Object



123
124
125
# File 'lib/abstract_notifier/base.rb', line 123

def with(params)
  ParamsProxy.new(self, params)
end

Instance Method Details

#notification(**payload) ⇒ Object

Raises:

  • (ArgumentError)


155
156
157
158
159
160
161
# File 'lib/abstract_notifier/base.rb', line 155

def notification(**payload)
  merge_defaults!(payload)

  raise ArgumentError, "Notification body must be present" if
    payload[:body].nil? || payload[:body].empty?
  Notification.new(self.class, payload)
end