Module: Notably::Notification

Defined in:
lib/notably/notification.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/notably/notification.rb', line 94

def method_missing(method, *args, &block)
  if method.to_s =~ /=/
    method = method.to_s.gsub!('=', '')
    if @data.key? method
      @data[method.to_sym] = *args
    end
  else
    if @data.key? method
      @data[method]
    else
      super
    end
  end
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/notably/notification.rb', line 3

def created_at
  @created_at
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/notably/notification.rb', line 3

def data
  @data
end

#groupsObject

Returns the value of attribute groups.



3
4
5
# File 'lib/notably/notification.rb', line 3

def groups
  @groups
end

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/notably/notification.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
  if defined?(Rails) && defined?(ActionView)
    base.send(:include, ActionView::Helpers)
    base.send(:include, Rails.application.routes.url_helpers)
  end
end

Instance Method Details

#initialize(*attributes_hashes) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/notably/notification.rb', line 25

def initialize(*attributes_hashes)
  @data = {}
  @groups = []
  attributes_hashes.each do |attributes|
    case attributes
    when Hash
      raise ArgumentError, "Hash does not have all required attributes" unless self.class.required_attributes.all? { |k| attributes.key? k }
      if @data.any?
        raise ArgumentError, "Group by fields do not have shared values" unless @data == attributes.slice(*self.class.group_by)
      else
        @data = attributes
      end
      @groups << OpenStruct.new(attributes.except(*self.class.group_by))
    else
      raise ArgumentError, "Object #{attributes} does not respond to all required attributes" unless self.class.required_attributes.all? { |k| attributes.respond_to? k }
      if @data.any?
        raise ArgumentError, "Group by fields do not have shared values" unless @data == Hash[self.class.group_by.collect { |k| [k, attributes.send(k)] }]
      else
        @data = Hash[self.class.required_attributes.collect { |k| [k, attributes.send(k)] }]
      end
      @groups << OpenStruct.new(Hash[(self.class.required_attributes - self.class.group_by).collect { |k| [k, attributes.send(k)] }])
    end
  end
end

#marshalObject



86
87
88
# File 'lib/notably/notification.rb', line 86

def marshal
  Marshal.dump(to_h)
end

#receiversObject



21
22
23
# File 'lib/notably/notification.rb', line 21

def receivers
  []
end

#saveObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/notably/notification.rb', line 50

def save
  receivers.each do |receiver|
    # look for groupable messages within group_within
    if self.class.group?
      group_within = self.class.group_within.call(receiver)
      groupable_notifications = receiver.notifications_since(group_within)
      groupable_notifications.select! { |notification| notification[:data].slice(*self.class.group_by) == data.slice(*self.class.group_by) }
      groupable_notifications.each do |notification|
        @groups += notification[:groups]
      end
    end
    run_callbacks(:before_notify, receiver)
    Notably.config.redis.pipelined do
      Notably.config.redis.zadd(receiver.send(:notification_key), created_at.to_i, marshal)
      receiver.touch if Notably.config.touch_receivers
      if self.class.group?
        groupable_notifications.each do |notification|
          Notably.config.redis.zrem(receiver.send(:notification_key), Marshal.dump(notification))
          @groups -= notification[:groups]
        end
      end
    end
    run_callbacks(:after_notify, receiver)
  end
end

#to_hObject



76
77
78
79
80
81
82
83
84
# File 'lib/notably/notification.rb', line 76

def to_h
  {
    created_at: created_at,
    data: data,
    groups: groups,
    message: to_s,
    html: to_html
  }
end

#to_htmlObject



17
18
19
# File 'lib/notably/notification.rb', line 17

def to_html
  ""
end

#to_sObject



13
14
15
# File 'lib/notably/notification.rb', line 13

def to_s
  ActionView::Base.full_sanitizer.sanitize(to_html) if defined?(ActionView)
end