Project

decouple

0.0
No commit activity in last 3 years
No release in over 3 years
Decouples long methods in a pretty weird unnatural way. Please use PRIVATE methods instead. That's a really bad idea, NEVER use it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

Decouple FTW!

Gem Version Build Status

Decouples long ruby methods (sounds strange, right?) in a pretty weird unnatural way. Please use PRIVATE methods instead, extract classes, objects, do whatever is possible not to use it. That's a really bad idea, NEVER use it. I DO ALWAYS USE NATURAL LANGUAGE CONSTRUCTIONS.

Exactly the same thing you can do using Observers, metaprogramming etc... Please also reffer to Module#prepend.

Anyways... :)

class MyClass
  include Decouple

  def send_email
    # 100 lines of code
    proceed_action
  end

  def send_letter
    # ...
    # Another thousand lines code
    # ...
    proceed_action :to_grandma
  end

  private

  def burn_paper
    # bazillion lines of code
  end
end

# app/callbacks/safe_actions.rb
MyClass.decouple do
  on :send_email do
    Mailer.notify
  end

  on :send_letter do |receiver|
    Mailer.notify(receipient: receiver)
  end
end

# app/callbacks/burn_paper.rb
MyClass.decouple do
  on :send_email do
    burn_paper
  end
end

It is preferred not to populate hard logic in decouple block, better create multiple decouple blocks if possible.

Testing

Never decouple tests (it is like testing private methods). All you do in your decoupled blocks - it is still the same method you decouple.

subject { MyClass.new }

# Put all your tests here, don't decouple tests!
describe '#send_email' do
  it 'delivers emails to grandma' do
    expect { subject.send_email(:to_grandma) }.to deliver_emails
  end
end

Anyways... :)

subject { MyClass.new }

describe 'callbacks' do
  # <paranoid_mode>
  specify do
    expect(:subject).to receive(:proceed_action).with(:something)
    subject.send_email
  end
  # </paranoid_mode>

  describe 'on #send_email' do
    it 'delivers emails to grandma' do
      expect { subject.proceed_with(:send_email, :to_grandma) }.to deliver_emails
    end
  end
end