Module: Facteur::AddresseeModel::InstanceMethods

Defined in:
lib/facteur/addressee_model.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



104
105
106
107
108
# File 'lib/facteur/addressee_model.rb', line 104

def method_missing(method, *args, &block)
  mailbox = Mailbox.find(:first, :conditions => {:name => method.to_s, :addressee_id => self.attributes["id"], :addressee_type => self.class.to_s})
  return mailbox if not mailbox.nil?
  super
end

Instance Method Details

#create_mailbox(name, options = {}) ⇒ Object

Creates a new mailbox. if a mailbox with the same name already exists, it fails and returns false. If succeeded, it creates an accessor for the new mail box and returns true. Example :

class User < ActiveRecord::base
   include Facteur::AddresseeModel

   mailbox :private_mailbox
end

The previous declaration will add : User#private_mailbox

# supposing that a name field exists
user = User.new(:name => 'John')
user.create_mailbox :public_mailbox #=> return true
user.create_mailbox :private_mailbox #=> return false


86
87
88
89
90
91
92
93
94
# File 'lib/facteur/addressee_model.rb', line 86

def create_mailbox(name, options={})
  mailbox = Mailbox.new(:name => name.to_s)
  mailbox.addressee = self
  mailbox.default = options[:default]
  return false if mailbox.save == false
  
  @default_mailbox = name if options[:default]
  true
end

#create_mailbox!(name, options = {}) ⇒ Object

Creates a new mailbox. if a mailbox with the same name already exists, it raises an exception. If succeeded, it creates an accessor for the new mail box and returns the created mailbox.



97
98
99
100
101
102
# File 'lib/facteur/addressee_model.rb', line 97

def create_mailbox!(name, options={})
  if create_mailbox(name, options) == false
    raise "Mailboxes names must be unique. Can't create '#{name}'"
  end
  self.send "#{name}"
end

#send_message(message, options) ⇒ Object

Sends a message to one or many addressees. The following options are available:

:to

the addressee or the list of addressees (mandatory)

:in

the name of the mailbox in which the message is posted (mandatory)

:body

the message’s body (mandatory)

Usage :

# send a message to one addressee
@john.send_message('message contents', :to => @peter, :in => :private_mailbox)

# send a message to many addressees
@john.send_message('message contents', :to => [@peter, @james], :in => :private_mailbox)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/facteur/addressee_model.rb', line 58

def send_message(message, options)
  msg = nil
  options[:body] = message
  if options[:to].is_a? Array
    options[:to].each do |addressee|
      msg = send_message_to(addressee, options[:in], options[:body], options[:subject])
    end
  else
    msg = send_message_to(options[:to], options[:in], options[:body], options[:subject])
  end
  msg
end