class DataContract::Contract

Attributes

mod[R]
obj[R]

Public Class Methods

new(mod, obj) click to toggle source
# File lib/data_contract/contract.rb, line 6
def initialize(mod, obj)
  @mod = mod
  @obj = obj
end

Public Instance Methods

assign(val, receiver, attribute) click to toggle source
# File lib/data_contract/contract.rb, line 47
def assign(val, receiver, attribute)
  setter = "#{attribute}="

  unless receiver.respond_to?(setter, true)
    raise ContractError, "The #{attribute} attribute cannot be assigned to #{receiver.class.name}"
  end

  receiver.send setter, val
end
assignable?(other_obj) click to toggle source
# File lib/data_contract/contract.rb, line 30
def assignable?(other_obj)
  unless shared? other_obj
    unless compatible? other_obj
      return false
    end
  end
  true
end
compatible?(other_obj) click to toggle source
# File lib/data_contract/contract.rb, line 19
def compatible?(other_obj)
  mod.instance_methods(false).each do |m|
    return false unless other_obj.respond_to?(m, true)
  end
  return true
end
getters() click to toggle source
# File lib/data_contract/contract.rb, line 15
def getters
  mod.instance_methods(false).select { |m| m.to_s[-1, 1] != '=' }
end
scatter(receiver) click to toggle source
# File lib/data_contract/contract.rb, line 39
def scatter(receiver)
  getters.each do |attribute|
    val = obj.send attribute
    assign val, receiver, attribute
  end
  receiver
end
setters() click to toggle source
# File lib/data_contract/contract.rb, line 11
def setters
  mod.instance_methods(false).select { |m| m.to_s[-1, 1] == '=' }
end
shared?(other_obj) click to toggle source
# File lib/data_contract/contract.rb, line 26
def shared?(other_obj)
  other_obj.is_a? mod
end