Module: Facteur::BaseAddresseeModel::InstanceMethods
- Defined in:
- lib/facteur/base_addressee_model.rb
Instance Method Summary collapse
-
#==(comparison_object) ⇒ Object
redefine the comparison method because for some weirds reasons, the original fails.
-
#create_mailbox(name, options = {}) ⇒ Object
Creates a new mailbox.
-
#create_mailbox!(name, options = {}) ⇒ Object
Creates a new mailbox.
-
#method_missing(method, *args, &block) ⇒ Object
generates the mailboxes accessors.
-
#send_message(message, options) ⇒ Object
Sends a message to one or many addressees.
-
#sent_messages ⇒ Object
returns the messages sent by this addressee.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
generates the mailboxes accessors
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/facteur/base_addressee_model.rb', line 93 def method_missing(method, *args, &block) begin super rescue NoMethodError, NameError mailbox = self.mailboxes.where(:name => method.to_s).first if mailbox.nil? super unless self.class.has_mailbox?(method) # if the unknown method matches one of the Model mailboxes, then the mailbox is created create_mailbox(method, self.class.mailboxes.select{ |m| m[:name] == method }.first) else return mailbox end end end |
Instance Method Details
#==(comparison_object) ⇒ Object
redefine the comparison method because for some weirds reasons, the original fails
110 111 112 113 114 |
# File 'lib/facteur/base_addressee_model.rb', line 110 def ==(comparison_object) comparison_object.equal?(self) || (self.class.to_s == comparison_object.class.to_s && comparison_object.id == id && !comparison_object.new_record?) end |
#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
78 79 80 81 82 |
# File 'lib/facteur/base_addressee_model.rb', line 78 def create_mailbox(name, ={}) mailbox = mailboxes.build(:name => name.to_s, :default => [:default]) return false if mailbox.save == false mailbox 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.
85 86 87 88 89 90 |
# File 'lib/facteur/base_addressee_model.rb', line 85 def create_mailbox!(name, ={}) if create_mailbox(name, ) == 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.('message contents', :to => @peter, :in => :private_mailbox)
# send a message to many addressees
@john.('message contents', :to => [@peter, @james], :in => :private_mailbox)
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/facteur/base_addressee_model.rb', line 52 def (, ) [:body] = if [:to].respond_to?(:each) [:to].each { |addressee| (addressee, [:in], [:body], [:subject]) } else ([:to], [:in], [:body], [:subject]) end (, ) end |
#sent_messages ⇒ Object
returns the messages sent by this addressee
35 36 37 |
# File 'lib/facteur/base_addressee_model.rb', line 35 def . end |