Sha256: 5b88b6e9ab0e84b398175d441a798f26350135c30d512f53a08dba2000206f01

Contents?: true

Size: 1020 Bytes

Versions: 8

Compression:

Stored size: 1020 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 ||= Hash.new { |h, k| h[k] = [] }
    end

    def success!
      self._failure = false
    end

    def failure!(messages = {})
      messages.each do |key, message|
        errors[key].push message
      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

8 entries across 8 versions & 1 rubygems

Version Path
cooperator-0.2.5 lib/cooperator/context.rb
cooperator-0.2.4 lib/cooperator/context.rb
cooperator-0.2.3 lib/cooperator/context.rb
cooperator-0.2.2 lib/cooperator/context.rb
cooperator-0.2.1 lib/cooperator/context.rb
cooperator-0.2.0 lib/cooperator/context.rb
cooperator-0.1.2 lib/cooperator/context.rb
cooperator-0.1.1 lib/cooperator/context.rb