Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created February 12, 2016 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelevie/7809bb86b8e1e4f761b5 to your computer and use it in GitHub Desktop.
Save adelevie/7809bb86b8e1e4f761b5 to your computer and use it in GitHub Desktop.
def complete_and_successful
relation = delivery_deadline_expired(@relation)
relation = delivered(relation)
relation = accepted(relation)
relation = cap_submitted(relation)
relation = paid(relation)
relation
end
@krusynth
Copy link

It's been a while since I've done this sort of thing, but would this work?

def complete_and_successful
  [delivery_deadline_expired, delivered, accepted, cap_submitted, paid].inject(@relation) do |obj, method|
    method(obj)
  end
end

@dogweather
Copy link

If these could properly be considered attributes or features of Relation (and they do read that way), then refactor them to make them instance methods which return self. Then you can do:

def complete_and_successful
  @relation
    .delivery_deadline_expired
    .delivered
    .accepted
    .cap_submitted
    .paid
end

@dogweather
Copy link

You could also do this without refactoring the functions into methods by using .tap:

def complete_and_successful
  @relation
    .tap { |r| delivery_deadline_expired(r) }
    .tap { |r| delivered(r) }
    .tap { |r| accepted(r) }
    .tap { |r| cap_submitted(r) }
    .tap { |r| paid(r) }
end

@dogweather
Copy link

@krues8dr, you've been doing a lot of javascript or python programming. 😄 In Ruby it'd look like this:

def complete_and_successful
  [:delivery_deadline_expired, :delivered, :accepted, :cap_submitted, :paid].inject(@relation) do |obj, method|
    obj.send(method)
  end
end

@krusynth
Copy link

@dogweather Ugh, you're right. Language switching on a Friday afternoon. :) I definitely agree that the chained version is much nicer!

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