Skip to content
Mike Nelson edited this page Jan 30, 2020 · 9 revisions

Inputs provided to an Op are accessible in a handful of ways. Let's take a look at an example Op:

class ContactInfoUpdateOp < Subroutine::Op
  include ::Subroutine::AssociationFields
  
  association :user
  string :email
  string :phone

  def perform
    # this is a very unnecessary explicit assignment for the sake of this example
    user.email = email if field_provided?(:email)
    user.phone = phone if field_provided?(:phone)
    user.save! if user.changed?
  end
end
class UserInfoUpdateOp < ::Subroutine::Op
  include ::Subroutine::AssociationFields
  
  fields_from ContactInfoUpdateOp, group: :contact
  
  string :request_key, default: -> { SecureRandom.hex }
  string :trigger, default: "manual", group: "system"

  with_options group: :user do
    string :first_name
    string :last_name
    date :dob
  end
  
  def perform
    user.update(user_params)
    ContactInfoUpdateOp.submit!(contact_params)
  end
end

Given the ops above, let's look at how inputs can be accessed.

params

Using params is the most common way to access Op inputs. It retrieves the inputs passed to the op. It does not include the default values or input not defined by field declarations.

op = UserInfoUpdateOp.new
op.params # => { }

op = UserInfoUpdateOp.new(first_name: "Douglas")
op.params # => { first_name: "Douglas" }

op = UserInfoUpdateOp.new(email: "[email protected]")
op.params # => { email: "[email protected]" }

op = UserInfoUpdateOp.new(dob: nil)
op.params # => { dob: nil }

defaults

The defaults accessor provides a hash of all the default values.

op = UserInfoUpdateOp.new
op.defaults # => { trigger: "manual", request_key: "a30z" }

params_with_defaults

params_with_defaults provides a way to retrieve any provided params merged against the defaults:

op = UserInfoUpdateOp.new(dob: "1980-01-01")
op.params # => { dob: Tue, 01 Jan 1980 }
op.defaults # => { trigger: "manual", request_key: "a30z" }
op.params_with_defaults # => { dob: Tue, 01 Jan 1980, trigger: "manual", request_key: "a30z" }

original_params

The inputs originally provided to the Op. The values are not type cast.

op = UserInfoUpdateOp.new(foo: "bar", first_name: "Douglas")
op.params # => { first_name: "Douglas" }
op.original_params # => { foo: "bar", first_name: "Douglas" }

[group]_params

[group]_params allows you to access all inputs provided within a specific group.

op = UserInfoUpdateOp.new(trigger: "system", first_name: "Douglas", last_name: "Wilson")
op.params # => { trigger: "system", first_name: "Douglas", last_name: "Wilson" }
op.user_params # => { first_name: "Douglas", last_name: "Wilson" }

[group]_defaults

Retrieve the defaults relevant to a specific group of inputs.

op = UserInfoUpdateOp.new
op.defaults # => { trigger: "manual", request_key: "a30z" }
op.system_defaults # => { trigger: "manual" }

[group]_params_with_defaults

Retrieve the inputs within [group] merged with the defaults relevant to [group].

op = UserInfoUpdateOp.new(first_name: "Douglas")
op.params # => { first_name: "Douglas" }
op.defaults # => { trigger: "manual", request_key: "a30z" }
op.params_with_defaults # => { first_name: "Douglas", trigger: "manual", request_key: "a30z" }
op.system_params # => { }
op.system_params_with_defaults # => { trigger: "manual" }

without_[group]_params

Access all params except those within [group].

op = UserInfoUpdateOp.new(trigger: "system", first_name: "Douglas")
op.params # => { trigger: "system", first_name: "Douglas" }
op.without_system_params # => { first_name: "Douglas" }