class Formally::State

Public Class Methods

new(schema:, transaction: @schema, @transaction = schema, transaction) click to toggle source
# File lib/formally/state.rb, line 3
def initialize schema:, transaction:
  @schema, @transaction = schema, transaction
  @callbacks = { after_commit: [] }
end

Public Instance Methods

after_commit(&block) click to toggle source
# File lib/formally/state.rb, line 31
def after_commit &block
  @callbacks[:after_commit].push block
end
call(data) click to toggle source
# File lib/formally/state.rb, line 8
def call data
  result  = @schema.call(data || {})
  @data   = result.output
  @errors = result.errors
  @filled = true
end
callbacks(key) click to toggle source
# File lib/formally/state.rb, line 35
def callbacks key
  @callbacks.fetch key, []
end
data() click to toggle source
# File lib/formally/state.rb, line 15
def data
  @filled ? @data : raise(Formally::Unfilled)
end
errors() click to toggle source
# File lib/formally/state.rb, line 19
def errors
  @filled ? @errors : raise(Formally::Unfilled)
end
transaction(&block) click to toggle source
# File lib/formally/state.rb, line 27
def transaction &block
  @transaction.call(&block)
end
valid?() click to toggle source
# File lib/formally/state.rb, line 23
def valid?
  errors.empty?
end