Class: Nuntius::BaseMessenger
- Inherits:
-
Object
- Object
- Nuntius::BaseMessenger
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- app/messengers/nuntius/base_messenger.rb
Overview
Messengers select templates, can manipulate them and
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#templates ⇒ Object
Returns the value of attribute templates.
Class Method Summary collapse
- .class_name_for(obj) ⇒ Object
- .class_names_for(obj) ⇒ Object
- .event_name_for(obj, event) ⇒ Object
-
.liquid_variable_name_for(obj) ⇒ String
Returns the variable name used in the liquid context.
- .locale(locale = nil) ⇒ Object
- .messenger_for_class(name) ⇒ Object
- .messenger_for_obj(obj) ⇒ Object
- .messenger_name_for_class(name) ⇒ Object
- .messenger_name_for_obj(obj) ⇒ Object
- .template_scope(template_scope = nil) ⇒ Object
- .timebased_scope(name, &scope_proc) ⇒ Object
- .timebased_scope_for(template) ⇒ Object
- .timebased_scopes ⇒ Object
Instance Method Summary collapse
- #attach(attachment) ⇒ Object
-
#call ⇒ Object
Calls the event method on the messenger.
-
#dispatch(filtered_templates) ⇒ Object
Turns the templates in messages, and dispatches the messages to transports.
-
#initialize(object, event, params = {}) ⇒ BaseMessenger
constructor
A new instance of BaseMessenger.
-
#override_layout(selected_layout) ⇒ Object
Allow messengers to override the selected layout.
Constructor Details
#initialize(object, event, params = {}) ⇒ BaseMessenger
Returns a new instance of BaseMessenger.
15 16 17 18 19 20 |
# File 'app/messengers/nuntius/base_messenger.rb', line 15 def initialize(object, event, params = {}) @object = object @event = event @params = params @attachments = params.fetch(:attachments, []) end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
12 13 14 |
# File 'app/messengers/nuntius/base_messenger.rb', line 12 def @attachments end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
12 13 14 |
# File 'app/messengers/nuntius/base_messenger.rb', line 12 def event @event end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
12 13 14 |
# File 'app/messengers/nuntius/base_messenger.rb', line 12 def object @object end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'app/messengers/nuntius/base_messenger.rb', line 12 def params @params end |
#templates ⇒ Object
Returns the value of attribute templates.
13 14 15 |
# File 'app/messengers/nuntius/base_messenger.rb', line 13 def templates @templates end |
Class Method Details
.class_name_for(obj) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/messengers/nuntius/base_messenger.rb', line 73 def class_name_for(obj) case obj when Array, ActiveRecord::Relation obj.first.class.name.demodulize when Hash "Custom" when Class obj.name.demodulize else obj.class.name end end |
.class_names_for(obj) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'app/messengers/nuntius/base_messenger.rb', line 86 def class_names_for(obj) main_class_name = class_name_for(obj) return [main_class_name] if !obj.class.respond_to?(:base_class?) || obj.class.base_class? list = [main_class_name] list << obj.class.base_class.name list end |
.event_name_for(obj, event) ⇒ Object
96 97 98 99 100 101 102 |
# File 'app/messengers/nuntius/base_messenger.rb', line 96 def event_name_for(obj, event) if obj.is_a?(Hash) "#{obj.keys.first}##{event}" else event end end |
.liquid_variable_name_for(obj) ⇒ String
Returns the variable name used in the liquid context
61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/messengers/nuntius/base_messenger.rb', line 61 def liquid_variable_name_for(obj) return obj.keys.first.to_s if obj.is_a?(Hash) plural = obj.is_a?(Array) || obj.is_a?(ActiveRecord::Relation) list = plural ? obj : [obj] klass = list.first.class klass = klass.base_class if klass.respond_to?(:base_class) name = klass.name.demodulize name = name.pluralize if plural name.underscore end |
.locale(locale = nil) ⇒ Object
133 134 135 136 |
# File 'app/messengers/nuntius/base_messenger.rb', line 133 def locale(locale = nil) @locale = locale if locale @locale end |
.messenger_for_class(name) ⇒ Object
104 105 106 107 108 109 |
# File 'app/messengers/nuntius/base_messenger.rb', line 104 def messenger_for_class(name) klass = messenger_name_for_class(name).safe_constantize klass ||= messenger_name_for_class(name.safe_constantize.superclass).safe_constantize klass ||= messenger_name_for_class(name.safe_constantize.superclass.superclass).safe_constantize klass end |
.messenger_for_obj(obj) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/messengers/nuntius/base_messenger.rb', line 115 def messenger_for_obj(obj) return Nuntius::CustomMessenger if obj.is_a? Hash klass = messenger_name_for_obj(obj).safe_constantize # Lets check 2 levels above to see if a messager exists for a possible super class (think STI) klass ||= messenger_name_for_obj(obj.class.superclass).safe_constantize klass ||= messenger_name_for_obj(obj.class.superclass.superclass).safe_constantize raise Nuntius::MissingMessengerException.new(self), "messenger missing for #{obj.class.name}" unless klass klass end |
.messenger_name_for_class(name) ⇒ Object
111 112 113 |
# File 'app/messengers/nuntius/base_messenger.rb', line 111 def messenger_name_for_class(name) "#{name}Messenger" end |
.messenger_name_for_obj(obj) ⇒ Object
129 130 131 |
# File 'app/messengers/nuntius/base_messenger.rb', line 129 def messenger_name_for_obj(obj) "#{class_name_for(obj)}Messenger" end |
.template_scope(template_scope = nil) ⇒ Object
138 139 140 141 |
# File 'app/messengers/nuntius/base_messenger.rb', line 138 def template_scope(template_scope = nil) @template_scope = template_scope if template_scope @template_scope end |
.timebased_scope(name, &scope_proc) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'app/messengers/nuntius/base_messenger.rb', line 147 def timebased_scope(name, &scope_proc) raise ArgumentError, "timebased_scope must start with before or after" unless %w[before after].detect { |prefix| name.to_s.start_with?(prefix) } name = name.to_sym timebased_scopes[name] = scope_proc if scope_proc.present? define_method(name) { |object, params = {}| } unless respond_to?(name) timebased_scopes[name] || nil end |
.timebased_scope_for(template) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/messengers/nuntius/base_messenger.rb', line 156 def timebased_scope_for(template) return [] unless timebased_scopes.include?(template.event.to_sym) timebased_scope(template.event) .call(template.interval_time_range, template.) .where("#{template.klass.constantize.table_name}.created_at > ?", template.created_at) .where.not( id: Nuntius::Message.select(:nuntiable_id) .where(template_id: template.id) ) end |
.timebased_scopes ⇒ Object
143 144 145 |
# File 'app/messengers/nuntius/base_messenger.rb', line 143 def timebased_scopes @timebased_scopes ||= {} end |
Instance Method Details
#attach(attachment) ⇒ Object
49 50 51 |
# File 'app/messengers/nuntius/base_messenger.rb', line 49 def attach() @attachments << end |
#call ⇒ Object
Calls the event method on the messenger
23 24 25 26 27 28 |
# File 'app/messengers/nuntius/base_messenger.rb', line 23 def call select_templates run_callbacks(:action) do send(@event.to_sym, @object, @params) if respond_to?(@event.to_sym) end end |
#dispatch(filtered_templates) ⇒ Object
Turns the templates in messages, and dispatches the messages to transports
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/messengers/nuntius/base_messenger.rb', line 31 def dispatch(filtered_templates) filtered_templates.each do |template| template.layout = override_layout(template.layout) msg = template.(@object, liquid_context, params) @attachments.each do || msg.() end transport = Nuntius::BaseTransport.class_from_name(template.transport).new transport.deliver(msg) if msg.to.present? end end |
#override_layout(selected_layout) ⇒ Object
Allow messengers to override the selected layout
45 46 47 |
# File 'app/messengers/nuntius/base_messenger.rb', line 45 def override_layout(selected_layout) selected_layout end |