Sha256: eba79ef556608c92754fdc0175e65094769016ba02b3dba0e30f83a8e6590967

Contents?: true

Size: 582 Bytes

Versions: 2

Compression:

Stored size: 582 Bytes

Contents

require 'casting/client'
require 'casting/super_delegate'

module Casting

  class InvalidClientError < StandardError; end

  def self.delegating(assignments)
    assignments.each do |object, mod|
      cast_object(object, mod)
    end
    yield
  ensure
    assignments.each do |object, mod|
      uncast_object(object)
    end
  end

  def self.cast_object(object, mod)
    raise InvalidClientError.new unless object.respond_to?(:cast_as)

    object.cast_as(mod)
  end

  def self.uncast_object(object)
    return unless object.respond_to?(:uncast)

    object.uncast
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
casting-0.6.8 lib/casting.rb
casting-0.6.7 lib/casting.rb