class Legatus::Directive

Attributes

callbacks[R]
models[R]
properties[R]
transactions[R]
validations[R]
errors[R]
params[R]
props[R]

Public Class Methods

callback() { || ... } click to toggle source
# File lib/legatus/directive.rb, line 63
def callback
  @callbacks ||= {}
  @callbacks.merge!(yield)
end
chain(obj, invocations) click to toggle source
# File lib/legatus/directive.rb, line 24
def chain(obj, invocations)
  return Chain.new(invocations).apply(obj)
end
model(mname, &block) click to toggle source
# File lib/legatus/directive.rb, line 48
def model(mname, &block)
  @models ||= {}
  @models[mname] = block
end
new(params) click to toggle source
# File lib/legatus/directive.rb, line 71
def initialize(params)
  @params = params
  @errors = {}
  @props = {}

  self.class.properties.each do |pname, chain|
    @props[pname] = chain.apply(params)
  end
end
permit(attributes, subschema=nil) click to toggle source
# File lib/legatus/directive.rb, line 28
def permit(attributes, subschema=nil)
  lambda do |parent|
    result = parent.permit(attributes)
    return result if subschema.nil?

    result.tap do |whitelisted|
      subschema.each do |key, allowed|
        child = parent[key]
        next if child.nil?
        
        if child.is_a?(Array)
          whitelisted[:"#{key}_attributes"] = child.map { |c| c.permit(allowed) }
        else
          whitelisted[:"#{key}_attributes"] = child.permit(allowed)
        end
      end
    end
  end
end
props() { || ... } click to toggle source
# File lib/legatus/directive.rb, line 15
def props
  schema = yield

  @properties = {}
  schema.each do |key, invocations|
    @properties[key] = Chain.new(invocations)
  end
end
transaction(&block) click to toggle source
# File lib/legatus/directive.rb, line 58
def transaction(&block)
  @transactions ||= []
  @transactions << block
end
validate(*models) click to toggle source
# File lib/legatus/directive.rb, line 53
def validate(*models)
  @validations ||= []
  @validations.concat(models)
end

Public Instance Methods

clean() click to toggle source
# File lib/legatus/directive.rb, line 93
def clean
  self.reqs(self.props, self.props.keys)
end
execute() click to toggle source
# File lib/legatus/directive.rb, line 127
def execute
  return (
    self.valid? and self.executed?(:clean) and
    self.valid? and self.executed?(:load) and 
    self.valid? and self.executed?(:validate) and
    self.valid? and self.executed?(:persist)
  )
end
extract(*props) click to toggle source
# File lib/legatus/directive.rb, line 89
def extract(*props)
  props.map { |prop| self.send(prop) }
end
invalid?() click to toggle source
# File lib/legatus/directive.rb, line 85
def invalid?
  return !@errors.empty?
end
load() click to toggle source
# File lib/legatus/directive.rb, line 97
def load
  self.valid? and self.class.models.each do |mname, loader|
    self.send(:"#{mname}=", Flexcon.dispatch(self, loader))
  end
end
persist() click to toggle source
# File lib/legatus/directive.rb, line 117
def persist
  return nil if self.invalid?

  self.class.transactions.each do |handler|
    uow = UnitOfWork.new
    handler.call(uow, self)
    uow.commit
  end if self.class.transactions.present?
end
valid?() click to toggle source
# File lib/legatus/directive.rb, line 81
def valid?
  return @errors.empty?
end
validate() click to toggle source
# File lib/legatus/directive.rb, line 103
def validate
  return (
    self.valid? and self.class.validations.each do |mname|
      self.check(mname => self.send(mname))
    end
  ) if self.class.validations.present?

  return (
    self.valid? and self.class.models.each do |mname, loader|
      self.check(mname => self.send(mname))
    end
  ) if self.class.models.present?
end

Protected Instance Methods

callback?(mname, stage) click to toggle source
# File lib/legatus/directive.rb, line 154
def callback?(mname, stage)
  return true if self.class.callbacks.blank?
  return true if self.class.callbacks[mname].blank?

  Flexcon.dispatch(self, self.class.callbacks[mname][stage])
end
check(schema) click to toggle source
# File lib/legatus/directive.rb, line 161
def check(schema)
  schema.each do |key, model|
    if model.respond_to?(:each_with_index)
      self.check_many(key, model)
    else
      self.check_one(key, model)
    end
  end
end
check_many(key, models) click to toggle source
# File lib/legatus/directive.rb, line 178
def check_many(key, models)
  models.each_with_index do |m, i|
    if m.invalid?
      @errors[key] ||= {}
      @errors[key][i] ||= {}
      @errors[key][i].merge!(m.errors)
    end
  end
end
check_one(key, model) click to toggle source
# File lib/legatus/directive.rb, line 171
def check_one(key, model)
  if model.invalid?
    @errors[key] ||= {}
    @errors[key].merge!(model.errors)
  end
end
executed?(stepname) click to toggle source
# File lib/legatus/directive.rb, line 146
def executed?(stepname)
  return (
    self.callback?(stepname, :before) and
    self.send(stepname) and
    self.callback?(stepname, :after)
  )
end
reqs(source, attributes) click to toggle source
# File lib/legatus/directive.rb, line 137
def reqs(source, attributes)
  attributes.each do |attribute|
    next if not source[attribute].blank?
    @errors[attribute] ||= {}
    @errors[attribute][:base] = []
    @errors[attribute][:base] << 'is required'
  end
end