Sha256: e4a5bb46e0b842fc38a6574d9d582a7210e72a7e5bb7a1326af93d56bde604a6

Contents?: true

Size: 678 Bytes

Versions: 1

Compression:

Stored size: 678 Bytes

Contents

require "casting/version"
require "casting/client"
require "casting/enum"
require "casting/super_delegate"
require "casting/null"
require "casting/context"

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

1 entries across 1 versions & 1 rubygems

Version Path
casting-1.0.2 lib/casting.rb