Class: Segmentor::Target

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/segmentor/target.rb

Overview

Target is single piece of data, returned by a

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_with_user(user) ⇒ Object



19
20
21
# File 'lib/segmentor/target.rb', line 19

def self.new_with_user(user)
  ::Segmentor::Target.new(user_id: user.id, payload: {})
end

.new_with_user_and_payload(user, payload) ⇒ Object



23
24
25
# File 'lib/segmentor/target.rb', line 23

def self.new_with_user_and_payload(user, payload)
  ::Segmentor::Target.new(user_id: user.id, payload: payload)
end

Instance Method Details

#can_be_notified?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/segmentor/target.rb', line 45

def can_be_notified?
  most_recent_receipt = segment.receipts.where(user_id: user_id).order(:sent_at).last

  # notification can be sent if there is a receipt for this target that
  # is older than the repeat frequency of its segment
  return true if most_recent_receipt.nil?

  # we have a receipt. notify only if:
  # segment repeat frequency is not NO_REPEAT
  # receipt is older than segment repeat frequency
  segment.repeat_frequency != ::Segmentor::Segment::NO_REPEAT &&
    most_recent_receipt.sent_at < segment.repeat_frequency.days.ago
end

#get_bindingObject



35
36
37
# File 'lib/segmentor/target.rb', line 35

def get_binding
  binding
end

#issue_receipt!(rendered_value = nil) ⇒ Object



27
28
29
# File 'lib/segmentor/target.rb', line 27

def issue_receipt!(rendered_value = nil)
  ::Segmentor::Receipt.record_receipt(self, rendered_value)
end

#most_recent_receiptObject



31
32
33
# File 'lib/segmentor/target.rb', line 31

def most_recent_receipt
  segment.receipts.where(user_id: user_id).order(:sent_at).last
end

#preview(&block) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
# File 'lib/segmentor/target.rb', line 39

def preview(&block)
  raise ArgumentError, 'block required' unless block_given?

  block.call(self, segment.notifier_context)
end