Class: SimpleContracts::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_contracts/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, async: nil, logger: nil, sampler: nil, stats: nil, **kwargs) ⇒ Base

Returns a new instance of Base.



57
58
59
60
61
62
63
64
# File 'lib/simple_contracts/base.rb', line 57

def initialize(*args, async: nil, logger: nil, sampler: nil, stats: nil, **kwargs)
  @async = async.nil? ? default_async : !!async
  @sampler = sampler
  @stats = stats
  @logger = logger
  @input = {args: args, kwargs: kwargs}
  @meta = {checked: [], input: @input}
end

Class Method Details

.call(*args, **kwargs) ⇒ Object



33
34
35
# File 'lib/simple_contracts/base.rb', line 33

def call(*args, **kwargs)
  new(*args, **kwargs).call { yield }
end

.expectations_methodsObject



41
42
43
# File 'lib/simple_contracts/base.rb', line 41

def expectations_methods
  @expectations_methods ||= methods_with_prefix("expect_")
end

.guarantees_methodsObject



37
38
39
# File 'lib/simple_contracts/base.rb', line 37

def guarantees_methods
  @guarantees_methods ||= methods_with_prefix("guarantee_")
end

Instance Method Details

#callObject Also known as: match!



66
67
68
69
70
71
# File 'lib/simple_contracts/base.rb', line 66

def call
  return yield unless enabled?
  @output = yield
  @async ? verify_async : verify
  @output
end

#contract_nameObject



83
84
85
# File 'lib/simple_contracts/base.rb', line 83

def contract_name
  self.class.name
end

#deserialize(state_dump) ⇒ Object



79
80
81
# File 'lib/simple_contracts/base.rb', line 79

def deserialize(state_dump)
  Marshal.load(state_dump)
end

#serializeObject



75
76
77
# File 'lib/simple_contracts/base.rb', line 75

def serialize
  Marshal.dump(input: @input, output: @output, meta: @meta)
end