Skip to content

Instantly share code, notes, and snippets.

@ntl
Last active October 7, 2017 14:46
Show Gist options
  • Save ntl/dd530170aeec42d10a1474130f996cbb to your computer and use it in GitHub Desktop.
Save ntl/dd530170aeec42d10a1474130f996cbb to your computer and use it in GitHub Desktop.
class InitializerArgumentsPaymentProcessor
def initialize(number, expiration_date, amount)
@number, @expiration_date, @amount = number, expiration_date, amount
end
def call
if SomeGateway.(number, expiration_date, amount)
true
else
false
end
end
class Substitute
def call
@approved
end
def approve
@approved = true
end
def decline
@approved = false
end
end
end
class InstanceMethodPaymentProcessor
def call(number, expiration_date, amount)
if SomeGateway.(number, expiration_date, amount)
true
else
false
end
end
class Substitute
def initialize
@approved = []
end
def call(number, expiration_date, amount)
@approved.any? do |n, e, a|
n == number && e == expiration_date && a == amount
end
end
def approve(number, expiration_date, amount)
@approved << [number, expiration_date, amount)
end
def decline(number, expiration_date, amount)
@approved.delete_if { |n, e, a|
n == number && e == expiration_date && a == amount
end
end
end
end
@ntl
Copy link
Author

ntl commented Oct 7, 2017

Note that for the initializer arguments example, a class level call method can be added to give the class the same interface as instances of the instance methods arguments example. So you get both approaches:

class InitializerArgumentsPaymentProcessor
  def self.call(number, expiration_date, amount)
    instance = new(number, expiration_date, amount)
    instance.()
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment