Class: Heya::Campaigns::Base
- Inherits:
-
Object
- Object
- Heya::Campaigns::Base
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActiveSupport::Rescuable, GlobalID::Identification, Singleton
- Defined in:
- lib/heya/campaigns/base.rb
Overview
Base provides a Ruby DSL for building campaign sequences. Multiple actions are supported; the default is email.
Instance Attribute Summary collapse
-
#steps ⇒ Object
Returns the value of attribute steps.
Class Method Summary collapse
- .default(**params) ⇒ Object
- .find(_id) ⇒ Object
- .inherited(campaign) ⇒ Object
- .segment(arg = nil, &block) ⇒ Object
- .step(name, **opts, &block) ⇒ Object
- .user_type(value = nil) ⇒ Object
Instance Method Summary collapse
- #add(user, restart: false, concurrent: false, send_now: true) ⇒ Object
-
#gid ⇒ Object
Returns String GlobalID.
- #handle_exception(exception) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #remove(user) ⇒ Object
- #user_class ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
17 18 19 |
# File 'lib/heya/campaigns/base.rb', line 17 def initialize self.steps = [] end |
Instance Attribute Details
#steps ⇒ Object
Returns the value of attribute steps.
71 72 73 |
# File 'lib/heya/campaigns/base.rb', line 71 def steps @steps end |
Class Method Details
.default(**params) ⇒ Object
101 102 103 |
# File 'lib/heya/campaigns/base.rb', line 101 def default(**params) self.__defaults = __defaults.merge(params).freeze end |
.find(_id) ⇒ Object
95 96 97 |
# File 'lib/heya/campaigns/base.rb', line 95 def find(_id) instance end |
.inherited(campaign) ⇒ Object
89 90 91 92 93 |
# File 'lib/heya/campaigns/base.rb', line 89 def inherited(campaign) Heya.register_campaign(campaign) Heya.unregister_campaign(campaign.superclass) super end |
.segment(arg = nil, &block) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/heya/campaigns/base.rb', line 113 def segment(arg = nil, &block) if block_given? self.__segments = ([block] | __segments).freeze elsif arg self.__segments = ([arg] | __segments).freeze end end |
.step(name, **opts, &block) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/heya/campaigns/base.rb', line 121 def step(name, **opts, &block) if block_given? opts[:block] ||= block opts[:action] ||= Actions::Block end opts = STEP_ATTRS .merge(Heya.config.campaigns.) .merge(__defaults) .merge(opts) attrs = opts.select { |k, _| STEP_ATTRS.key?(k) } attrs[:id] = "#{self.name}/#{name}" attrs[:name] = name.to_s attrs[:campaign] = instance attrs[:position] = steps.size attrs[:params] = opts.reject { |k, _| STEP_ATTRS.key?(k) }.stringify_keys step = Step.new(**attrs) method_name = :"#{step.name.underscore}" raise "Invalid step name: #{step.name}\n Step names must not conflict with method names on Heya::Campaigns::Base" if respond_to?(method_name) define_singleton_method method_name do |user| step.action.new(user: user, step: step).build end steps << step step end |
Instance Method Details
#add(user, restart: false, concurrent: false, send_now: true) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/heya/campaigns/base.rb', line 29 def add(user, restart: false, concurrent: false, send_now: true) return false unless Heya.in_segments?(user, *__segments) membership = CampaignMembership.where(user: user, campaign_gid: gid) if membership.exists? return false unless restart membership.delete_all end if restart CampaignReceipt .where(user: user, step_gid: steps.map(&:gid)) .delete_all end if (step = steps.first) membership.create! do |m| m.concurrent = concurrent m.step_gid = step.gid end if send_now && step.wait == 0 Scheduler.new.run(user: user) end end true end |
#gid ⇒ Object
Returns String GlobalID.
25 26 27 |
# File 'lib/heya/campaigns/base.rb', line 25 def gid to_gid(app: "heya").to_s end |
#handle_exception(exception) ⇒ Object
67 68 69 |
# File 'lib/heya/campaigns/base.rb', line 67 def handle_exception(exception) rescue_with_handler(exception) || raise(exception) end |
#remove(user) ⇒ Object
58 59 60 61 |
# File 'lib/heya/campaigns/base.rb', line 58 def remove(user) CampaignMembership.where(user: user, campaign_gid: gid).delete_all true end |
#user_class ⇒ Object
63 64 65 |
# File 'lib/heya/campaigns/base.rb', line 63 def user_class @user_class ||= self.class.user_type.constantize end |