Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Use instances rather than class methods
Browse files Browse the repository at this point in the history
Rather than sending Griddler.configuration.processor_method to
Griddler.configuration.processor_class with a Griddler::Email as an
argument (i.e., EmailProcessor.process(email)), instantiate
Griddler.configuration.processor_class with a Griddler::Email and call
Griddler.configuration.processor_method on that object (i.e.,
EmailProcessor.new(email).process).

This encourages the behavior of separating logic into methods within an
EmailProcessor, which is better than having a single class method with
many responsibilities or passing state around between class methods.
  • Loading branch information
calebhearth committed Jun 19, 2014
1 parent 33187f5 commit 7e8f34e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.0.0

* Use instances of email processor classes rather than calling class methods.
https://github.com/thoughtbot/griddler/pull/153

*Caleb Thompson*
* Remove configuration of email addresses for to, from, and cc.
https://github.com/thoughtbot/griddler/pull/150

Expand Down
12 changes: 8 additions & 4 deletions app/controllers/griddler/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ def create

private

delegate :processor_class, :processor_method, :email_service, to: :griddler_configuration

def normalized_params
Array.wrap(Griddler.configuration.email_service.normalize_params(params))
Array.wrap(email_service.normalize_params(params))
end

def process_email(email)
processor_class = Griddler.configuration.processor_class
processor_method = Griddler.configuration.processor_method
processor_class.public_send(processor_method, email)
processor_class.new(email).public_send(processor_method)
end

def griddler_configuration
Griddler.configuration
end
end
12 changes: 6 additions & 6 deletions spec/controllers/griddler/emails_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
end

it 'calls process on the custom processor class' do
my_handler = double
my_handler.should_receive(:process)
my_handler = double(process: nil)
my_handler.should_receive(:new).and_return(my_handler)
Griddler.configuration.stub(processor_class: my_handler)

post :create, email_params
end

it 'calls the custom processor method on the processor class' do
Griddler.configuration.stub(processor_method: :perform)
griddler_email = double
Griddler::Email.should_receive(:new).and_return(griddler_email)
fake_processor = double(perform: nil)

EmailProcessor.should_receive(:perform).with(griddler_email)
EmailProcessor.should_receive(:new).and_return(fake_processor)
fake_processor.should_receive(:perform)

post :create, to: '[email protected]'
post :create, email_params
end
end

Expand Down
7 changes: 4 additions & 3 deletions spec/dummy/app/models/email_processor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class EmailProcessor
cattr_accessor :email
def self.process(email)
@@email = email
def initialize(email)
@email = email
end

def process; end
end

0 comments on commit 7e8f34e

Please sign in to comment.