class Realm::ActionHandler

Attributes

contracts[R]

Public Class Methods

call(action: :handle, params: {}, runtime: nil) click to toggle source
# File lib/realm/action_handler.rb, line 14
def call(action: :handle, params: {}, runtime: nil)
  new(runtime: runtime).(action: action, params: params)
end
new(runtime: nil) click to toggle source
# File lib/realm/action_handler.rb, line 54
def initialize(runtime: nil)
  @runtime = runtime
end

Protected Class Methods

contract(&block) click to toggle source
# File lib/realm/action_handler.rb, line 24
def contract(&block)
  @method_contract = Class.new(Realm::Contract, &block).new
end
contract_json(...) click to toggle source
# File lib/realm/action_handler.rb, line 36
def contract_json(...)
  contract { json(...) }
end
Also aliased as: json_contract
contract_params(...) click to toggle source
# File lib/realm/action_handler.rb, line 32
def contract_params(...)
  contract { params(...) }
end
Also aliased as: params_contract
contract_schema(...) click to toggle source
# File lib/realm/action_handler.rb, line 28
def contract_schema(...)
  contract { schema(...) }
end
Also aliased as: schema_contract
json_contract(...)
Alias for: contract_json
method_added(method_name) click to toggle source
Calls superclass method
# File lib/realm/action_handler.rb, line 44
def method_added(method_name)
  super
  return unless defined?(@method_contract)

  @contracts ||= {}
  @contracts[method_name] = @method_contract
  remove_instance_variable(:@method_contract)
end
params_contract(...)
Alias for: contract_params
require_permission(*names) click to toggle source
# File lib/realm/action_handler.rb, line 20
def require_permission(*names)
  # TODO: implement
end
schema_contract(...)
Alias for: contract_schema

Public Instance Methods

call(action: :handle, params: {}) click to toggle source
# File lib/realm/action_handler.rb, line 58
def call(action: :handle, params: {})
  # TODO: check permissions
  raise CannotHandleAction.new(self, action) unless respond_to?(action)

  safe_params = validate(action, params.to_h)
  send(action, **safe_params)
end

Private Instance Methods

validate(action, params) click to toggle source
# File lib/realm/action_handler.rb, line 72
def validate(action, params)
  contract = self.class.contracts && self.class.contracts[action]
  return params unless contract

  result = contract.(params)
  raise Realm::InvalidParams, result if result.failure?

  result.to_h
end