Sha256: 43d5934269766e33b32c15b469acf0f6f7dec75d541155ea19901c01c9f6008b

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

module Cooperator
  class Context
    def initialize(attributes = {})
      @_attributes = {_failure: false}

      attributes.each do |k, v|
        send :"#{k}=", v
      end
    end

    def errors
      @_errors ||= []
    end

    def success!
      self._failure = false
    end

    def failure!(*args)
      args.each do |error|
        errors.push error
      end

      self._failure = true
    end

    def success?
      not failure?
    end

    def failure?
      _failure
    end

    def include?(key)
      @_attributes.include? key
    end

    def method_missing(method, *args, &block)
      return @_attributes.fetch method if @_attributes.include? method

      name = String method

      if name.include? '='
        name.gsub!(/=/, '')

        @_attributes[:"#{name}"] = args.shift
      else
        super
      end
    end

    def respond_to_missing?(method, private = false)
      @_attributes.include?(method) || super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cooperator-0.1.0 lib/cooperator/context.rb