Sha256: 18d7041731810160cba775de8518f088e3c0c1299f9ba72bc125817028e9ef83

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

require 'simple_command/version'
require 'simple_command/errors'

module SimpleCommand
  attr_reader :result

  module ClassMethods
    def perform(*args)
      new(*args).perform
    end
  end

  def self.prepended(base)
    base.extend ClassMethods
  end

  def perform
    fail NotImplementedError unless defined?(super)

    @performed = true
    @result = super

    self
  end

  def success?
    performed? && !failure?
  end

  def failure?
    performed? && errors.any?
  end

  def errors
    @errors ||= {}
  end

  def add_error(key, value)
    errors[key] ||= []
    errors[key] << value
    errors[key].uniq!
  end

  def add_errors(errors_hash)
    errors_hash.each do |key, values|
      values.each { |value| add_error key, value }
    end
  end

  private

  def performed?
    @performed ||= false
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_command-0.0.5 lib/simple_command.rb