module Formally

Constants

ClassMismatch
Error
Invalid
SchemaUndefined
Unfilled
VERSION

Attributes

config[RW]
formally[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/formally.rb, line 46
def initialize *args
  super
  @formally = self.class.formally.new self
end
predicates(with: []) click to toggle source
# File lib/formally.rb, line 23
def predicates with: []
  _predicates = config.predicates
  Module.new do
    include Dry::Logic::Predicates
    (_predicates + with).each do |predicate|
      instance_exec(&predicate)
    end
  end
end
prepended(base) click to toggle source
# File lib/formally.rb, line 18
def prepended base
  base.extend Formally::ClassMethods
  base.formally = Formally.config.with(klass: base)
end

Public Instance Methods

fill(data={}) click to toggle source
Calls superclass method
# File lib/formally.rb, line 51
def fill data={}
  if data.respond_to?(:permit!)
    # Assume ActionController::Parameters or similar
    # The schema will handle whitelisting allowed attributes
    data = data.permit!.to_h.deep_symbolize_keys
  end

  formally.call data

  if formally.valid?
    super formally.data
  end

  self
end
save() click to toggle source
Calls superclass method
# File lib/formally.rb, line 67
def save
  return false unless formally.valid?
  formally.transaction do
    super
  end
  formally.callbacks(:after_commit).each(&:call)
  true
end
save!() click to toggle source
# File lib/formally.rb, line 76
def save!
  unless save
    ex = Formally::Invalid.new
    ex.form   = self
    ex.errors = errors
    raise ex
  end
  true
end