Skip to content

Conversation

@fnordfish
Copy link

This allows to have more control on how the context works. For example which attributes are required, it's default values or validation.

Here's a short sample for illustration:

class MyInteractor
  include Interactor

  self.context_class = Class.new do
    include Context::Mixin

    attr_reader :foo
    attr_accessor :my_export

    def initialize(foo: nil)
      @foo = foo
    end
  end

  def call
    if context.foo
      context.my_export = "Got: " + context.foo.to_s
    else
      context.fail!(my_export: "Error")
    end
  end
end

puts MyInteractor.call(foo: "Hi").my_export # "Got: Hi"
puts MyInteractor.call.my_export # "Error"
MyInteractor.call(unexpected: "information") # ArgumentError (unknown keyword: unexpected)

This PR probably lacks quite some documentation etc. I just wanted to get your input if this is something you'd consider. (Obviously I'm happy to put more work into this if you do)

adding some minimalist docs so that rubocop is satisfied
@fnordfish
Copy link
Author

Specs are failing due to the development dependency on bundler ~> 1.9 and travis has the current 2.0.2 installed on more modern rubies.
That version requirement got dropped on master in 2c3d62e

@fnordfish fnordfish changed the title Make the Context class swapable Make the Context class swappable Sep 16, 2019
@gaffneyc
Copy link
Member

Thank you for the contribution and I'm sorry that it's taken so long to get eyes on it.

Contexts being a big ball of randomly keyed values has been one of my biggest complaints with Interactor since 1.0. I like the idea of making the Context class configurable as part of a larger effort to make working with Contexts easier. I'm not sure yet what that looks like yet but it will be a focus of a 4.0 release at some point in the future.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants