- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13
 
Param Usage
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
endclass 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.merge(user: user))
  end
endGiven the ops above, let's look at how inputs can be accessed.
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 }The defaults accessor provides a hash of all the default values.
op = UserInfoUpdateOp.new
op.defaults # => { trigger: "manual", request_key: "a30z" }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: "1980-01-01" }
op.defaults # => { trigger: "manual", request_key: "a30z" }
op.params_with_defaults # => { dob: "1980-01-01", trigger: "manual", request_key: "a30z" }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 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" }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" }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" }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" }