Module: BCDD::Contract

Defined in:
lib/bcdd/contract.rb,
lib/bcdd/contract/map.rb,
lib/bcdd/contract/core.rb,
lib/bcdd/contract/list.rb,
lib/bcdd/contract/unit.rb,
lib/bcdd/contract/proxy.rb,
lib/bcdd/contract/config.rb,
lib/bcdd/contract/version.rb,
lib/bcdd/contract/registry.rb,
lib/bcdd/contract/interface.rb,
lib/bcdd/contract/map/pairs.rb,
lib/bcdd/contract/assertions.rb,
lib/bcdd/contract/core/proxy.rb,
lib/bcdd/contract/map/schema.rb,
lib/bcdd/contract/core/checker.rb,
lib/bcdd/contract/core/factory.rb,
lib/bcdd/contract/unit/checker.rb,
lib/bcdd/contract/unit/factory.rb,
lib/bcdd/contract/core/checking.rb

Defined Under Namespace

Modules: Assertions, Interface Classes: Config, Error, Proxy

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.assert(value) ⇒ Object



53
54
55
# File 'lib/bcdd/contract.rb', line 53

def self.assert(value, ...)
  Config.instance.assertions_enabled ? assert!(value, ...) : value
end

.assert!(value, message, &block) ⇒ Object



41
42
43
44
45
# File 'lib/bcdd/contract.rb', line 41

def self.assert!(value, message, &block)
  return value if (value && !block) || (value && block.call(value))

  error!(format(message, value))
end

.configObject



21
22
23
# File 'lib/bcdd/contract.rb', line 21

def self.config
  Config.instance
end

.configuration {|config| ... } ⇒ Object

Yields:



25
26
27
28
29
# File 'lib/bcdd/contract.rb', line 25

def self.configuration
  yield(config)

  config.freeze
end

.error!(message) ⇒ Object

Raises:



37
38
39
# File 'lib/bcdd/contract.rb', line 37

def self.error!(message)
  raise Error, message
end

.list(arg) ⇒ Object



95
96
97
# File 'lib/bcdd/contract.rb', line 95

def self.list(arg)
  List.new(new(arg))
end

.new(arg) ⇒ Object

Raises:

  • (::ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bcdd/contract.rb', line 61

def self.new(arg)
  return arg if arg.is_a?(Core::Checker)

  return schema(arg) if arg.is_a?(::Hash)

  return Registry.fetch(arg) if arg.is_a?(::Symbol)

  return unit(arg) unless arg.is_a?(::Array) || arg.is_a?(::Set)

  list = arg.to_a.flatten

  return list(list[0]) if list.size == 1

  raise ::ArgumentError, 'must be one contract checker'
end

.pairs(arg) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/bcdd/contract.rb', line 105

def self.pairs(arg)
  arg.is_a?(::Hash) or raise ::ArgumentError, 'must be a Hash'
  arg.keys.size == 1 or raise ::ArgumentError, 'must have only one key and value'

  key, val = arg.to_a.flatten(1)

  Map::Pairs.new(new(key) => new(val))
end

.proxy(always_enabled: false, &block) ⇒ Object



31
32
33
34
35
# File 'lib/bcdd/contract.rb', line 31

def self.proxy(always_enabled: false, &block)
  proxy_class = always_enabled ? Proxy::AlwaysEnabled : Proxy

  ::Class.new(proxy_class, &block)
end

.refute(value) ⇒ Object



57
58
59
# File 'lib/bcdd/contract.rb', line 57

def self.refute(value, ...)
  Config.instance.assertions_enabled ? refute!(value, ...) : value
end

.refute!(value, message, &block) ⇒ Object



47
48
49
50
51
# File 'lib/bcdd/contract.rb', line 47

def self.refute!(value, message, &block)
  return value if (!value && !block) || (value && block && !block.call(value))

  error!(format(message, value))
end

.register(**kargs) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/bcdd/contract.rb', line 83

def self.register(**kargs)
  kargs.empty? and raise ::ArgumentError, 'must be passed as keyword arguments'

  kargs.each_with_object({}) do |(key, val), memo|
    memo[key] = Registry.write(key, new(val))
  end
end

.schema(arg) ⇒ Object



99
100
101
102
103
# File 'lib/bcdd/contract.rb', line 99

def self.schema(arg)
  arg.is_a?(::Hash) or raise ::ArgumentError, 'must be a Hash'

  Map::Schema.new(arg.transform_values { |svalue| new(svalue) })
end

.to_procObject



79
80
81
# File 'lib/bcdd/contract.rb', line 79

def self.to_proc
  ->(arg) { self[arg] }
end

.unit(arg) ⇒ Object



91
92
93
# File 'lib/bcdd/contract.rb', line 91

def self.unit(arg)
  Unit.new(arg)
end