Class: Newman::MailingList

Inherits:
Object
  • Object
show all
Defined in:
lib/newman/mailing_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, store) ⇒ MailingList

To initialize a ‘Newman::MailingList` object, a list name and a store object must be provided, i.e:

store        = Newman::Store.new('simple.store')
mailing_list = Newman::MailingList.new("simple_list", store)


21
22
23
24
# File 'lib/newman/mailing_list.rb', line 21

def initialize(name, store)
  self.name  = name
  self.store = store
end

Instance Method Details

#subscribe(email) ⇒ Object

‘Newman::MailingList#subscribe` is used to add subscribers to the mailing list, i.e.

mailing_list.subscribe('[email protected]')

If the provided email address is for a new subscriber, a new record gets created for that subscriber, adding them to the list. Otherwise, this method does not modify the mailing list.

Returns true if list was modified, returns false otherwise.



39
40
41
42
43
44
45
# File 'lib/newman/mailing_list.rb', line 39

def subscribe(email)
  return false if subscriber?(email)

  store[name].create(email)

  true
end

#subscriber?(email) ⇒ Boolean

‘Newman::MailingList#subscriber?` is used to check if a given email address is on the list, i.e.

mailing_list.subscriber?('[email protected]')

Returns true if a record is found which matches the given email address, returns false otherwise.

Returns:

  • (Boolean)


80
81
82
# File 'lib/newman/mailing_list.rb', line 80

def subscriber?(email)
  store[name].any? { |r| r.contents == email }
end

#subscribersObject

‘Newman::MailingList#subscribers` is used to access all email addresses for the mailing list’s subscribers, i.e:

members = mailing_list.subscribers

Returns an array of email addresses.



93
94
95
# File 'lib/newman/mailing_list.rb', line 93

def subscribers
  store[name].map { |r| r.contents } 
end

#unsubscribe(email) ⇒ Object

‘Newman::MailingList#unsubscribe` is used to remove subscribers from the mailing list, i.e.

mailing_list.unsubscribe('[email protected]')

If the provided email address is for an existing subscriber, the record for that subscriber is destroyed, removing them from the list. Otherwise, this method does not modify the mailing list.

Returns true if list was modified, returns false otherwise.



60
61
62
63
64
65
66
67
# File 'lib/newman/mailing_list.rb', line 60

def unsubscribe(email)
  return false unless subscriber?(email)

  record = store[name].find { |e| e.contents == email } 
  store[name].destroy(record.id)

  true
end