Class: BCDD::Contract::Proxy

Inherits:
Core::Proxy
  • Object
show all
Defined in:
lib/bcdd/contract/proxy.rb

Overview

A class to inherit to create proxy objects. Which can be used to check the arguments and returned values of the proxy object’s methods.

class Calculation < ::BCDD::Contract::Proxy

ValidNumber = ::BCDD::Contract[->(value, err) do
  err << '%p must be numeric' and return unless value.is_a?(::Numeric)
  err << '%p cannot be nan' and return if value.respond_to?(:nan?) && value.nan?
  err << '%p cannot be infinite' if value.respond_to?(:infinite?) && value.infinite?
end]

CannotBeZero = ::BCDD::Contract[->(arg, err) do
  err << '%p cannot be zero' if arg.zero?
end]

def divide(a, b)
  +ValidNumber[a]
  +ValidNumber[b] && +CannotBeZero[b]

  +ValidNumber[object.divide(a, b)]
end

# ... other methods ...

end

Constant Summary collapse

AlwaysEnabled =

A class to inherit to create a proxy object that is always enabled.

::Class.new(Core::Proxy)

Class Method Summary collapse

Class Method Details

.new(object) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/bcdd/contract/proxy.rb', line 32

def self.new(object)
  return object unless Config.instance.proxy_enabled

  instance = allocate
  instance.send(:initialize, object)
  instance
end