Method: Contract.adapt

Defined in:
lib/contract/integration.rb

.adapt(object, type) ⇒ Object

Tries to adapt the specified object to the specified type. Returns the old object if no suitable adaption route was found or if it already is of the specified type.

This will only use adaptions where the :to part is equal to the specified type. No multi-step conversion will be performed.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/contract/integration.rb', line 124

def self.adapt(object, type)
  return object if type === object

  @adaptions[type].each do |adaption|
    if adaption[:from] === object and
      (adaption[:if].nil? or adaption[:if] === object)
    then
      result = adaption[:via].call(object)
      return result if type === result
    end
  end

  return object
end