Class: BloodContracts::BaseContract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/blood_contracts/base_contract.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.contract_rule(name, &block) ⇒ Object



11
12
13
14
# File 'lib/blood_contracts/base_contract.rb', line 11

def contract_rule(name, &block)
  define_method("_#{name}", block)
  rules << name
end

.rulesObject



7
8
9
# File 'lib/blood_contracts/base_contract.rb', line 7

def rules
  @rules ||= Set.new
end

Instance Method Details

#build_storage(name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/blood_contracts/base_contract.rb', line 28

def build_storage(name)
  s = Storage.new(contract_name: name)
  s.input_writer  = method(:input_writer)  if defined? input_writer
  s.output_writer = method(:output_writer) if defined? output_writer
  s.input_serializer  = input_serializer   if defined? input_serializer
  s.output_serializer = output_serializer  if defined? output_serializer
  s.meta_serializer   = meta_serializer    if defined? meta_serializer
  s
end

#call(data) ⇒ Object



17
18
19
20
# File 'lib/blood_contracts/base_contract.rb', line 17

def call(data)
  return yield(data) if block_given?
  action.call(data)
end

#contractObject



22
23
24
25
26
# File 'lib/blood_contracts/base_contract.rb', line 22

def contract
  @contract ||= Hash[
    self.class.rules.map { |name| [name, {check: method("_#{name}")}] }
  ]
end

#to_contract_suite(name: self.class.to_s.pathize) ⇒ Object



38
39
40
# File 'lib/blood_contracts/base_contract.rb', line 38

def to_contract_suite(name: self.class.to_s.pathize)
  Suite.new(storage: build_storage(name), contract: contract)
end