yard-contracts 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/yard-contracts/formatters.rb +4 -93
- data/lib/yard-contracts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3162a34884d61da98b5669091f169634cc93f6fb
|
4
|
+
data.tar.gz: 73567abca501f0f141d97c303b662e2f1e85a141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a661e034c825fae56aff68007aaf8ef5eee74b7c0fa0e762967c2ec3bf395a483fe6f0e36a627181103217704f0ab0c95aefa085e39002ece51e4d7dafd49e9
|
7
|
+
data.tar.gz: 66d42a3d74c1f71415bbea835909e4788322d30ad84c094c7ff4ccdacf72349f0d71346ade1a388b45af0f40d8e5c84d756c32af2252d60b82f6650dd26b865c
|
@@ -3,95 +3,6 @@ require 'contracts/builtin_contracts'
|
|
3
3
|
module Contracts
|
4
4
|
# A namespace for classes related to formatting.
|
5
5
|
module Formatters
|
6
|
-
# Used to format contracts for the `Expected:` field of error output.
|
7
|
-
class Expected
|
8
|
-
def initialize(contract, full = true)
|
9
|
-
@contract, @full = contract, full
|
10
|
-
end
|
11
|
-
|
12
|
-
# Formats any type of Contract.
|
13
|
-
def contract(contract = @contract)
|
14
|
-
if contract.is_a?(Hash)
|
15
|
-
hash_contract(contract)
|
16
|
-
elsif contract.is_a?(Array)
|
17
|
-
array_contract(contract)
|
18
|
-
else
|
19
|
-
InspectWrapper.new(contract, @full)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# Formats Hash contracts.
|
24
|
-
def hash_contract(hash)
|
25
|
-
@full = true
|
26
|
-
hash.inject({}) do |repr, (k, v)|
|
27
|
-
repr.merge(k => InspectWrapper.new(contract(v), @full))
|
28
|
-
end.inspect
|
29
|
-
end
|
30
|
-
|
31
|
-
# Formats Array contracts.
|
32
|
-
def array_contract(array)
|
33
|
-
@full = true
|
34
|
-
array.map { |v| InspectWrapper.new(contract(v), @full) }.inspect
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# A wrapper class to produce correct inspect behaviour for different
|
39
|
-
# contract values - constants, Class contracts, instance contracts etc.
|
40
|
-
class InspectWrapper
|
41
|
-
def initialize(value, full = true)
|
42
|
-
@value, @full = value, full
|
43
|
-
end
|
44
|
-
|
45
|
-
# Inspect different types of contract values.
|
46
|
-
# Contracts module prefix will be removed from classes.
|
47
|
-
# Custom to_s messages will be wrapped in round brackets to differentiate
|
48
|
-
# from standard Strings.
|
49
|
-
# Primitive values e.g. 42, true, nil will be left alone.
|
50
|
-
def inspect
|
51
|
-
return '' unless full?
|
52
|
-
return @value.inspect if empty_val?
|
53
|
-
return @value.to_s if plain?
|
54
|
-
return delim(@value.to_s) if useful_to_s?
|
55
|
-
@value.inspect.gsub(/^Contracts::/, '')
|
56
|
-
end
|
57
|
-
|
58
|
-
def delim(value)
|
59
|
-
@full ? "(#{value})" : "#{value}"
|
60
|
-
end
|
61
|
-
|
62
|
-
# Eliminates eronious quotes in output that plain inspect includes.
|
63
|
-
def to_s
|
64
|
-
inspect
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
def empty_val?
|
70
|
-
@value.nil? || @value == ''
|
71
|
-
end
|
72
|
-
|
73
|
-
def full?
|
74
|
-
@full ||
|
75
|
-
@value.is_a?(Hash) || @value.is_a?(Array) ||
|
76
|
-
(!plain? && useful_to_s?)
|
77
|
-
end
|
78
|
-
|
79
|
-
def plain?
|
80
|
-
# Not a type of contract that can have a custom to_s defined
|
81
|
-
!@value.is_a?(CallableClass) && @value.class != Class
|
82
|
-
end
|
83
|
-
|
84
|
-
def useful_to_s?
|
85
|
-
# Useless to_s value or no custom to_s behavious defined
|
86
|
-
@value.to_s != '' &&
|
87
|
-
if @value.class == Class # It's a class contract
|
88
|
-
@value.to_s != @value.name
|
89
|
-
else # It's an instance contract
|
90
|
-
!@value.to_s.match(/#\<\w+:.+\>/)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
6
|
class TypesAST
|
96
7
|
def initialize(types)
|
97
8
|
@types = types[0..-2]
|
@@ -170,7 +81,7 @@ module Contracts
|
|
170
81
|
result = {}
|
171
82
|
hash.each do |h|
|
172
83
|
result[h[0].jump(:label).last.to_sym] =
|
173
|
-
Contracts::Formatters::InspectWrapper.
|
84
|
+
Contracts::Formatters::InspectWrapper.create(type(h[1]))
|
174
85
|
end
|
175
86
|
result
|
176
87
|
end
|
@@ -179,7 +90,7 @@ module Contracts
|
|
179
90
|
def array_type(array)
|
180
91
|
# This works because Ast inherits from Array.
|
181
92
|
array.map do |v|
|
182
|
-
Contracts::Formatters::InspectWrapper.
|
93
|
+
Contracts::Formatters::InspectWrapper.create(type(v))
|
183
94
|
end.inspect
|
184
95
|
end
|
185
96
|
end
|
@@ -227,7 +138,7 @@ module Contracts
|
|
227
138
|
named_count = 1
|
228
139
|
end
|
229
140
|
|
230
|
-
type = Contracts::Formatters::InspectWrapper.
|
141
|
+
type = Contracts::Formatters::InspectWrapper.create(type)
|
231
142
|
desc = Contracts::Formatters::Expected.new(con, false).contract
|
232
143
|
# The pluses are to escape things like curly brackets
|
233
144
|
desc = "#{desc}".empty? ? '' : "+#{desc}+"
|
@@ -240,7 +151,7 @@ module Contracts
|
|
240
151
|
def return
|
241
152
|
type, type_ast = @result
|
242
153
|
con = get_contract_value(type)
|
243
|
-
type = Contracts::Formatters::InspectWrapper.
|
154
|
+
type = Contracts::Formatters::InspectWrapper.create(
|
244
155
|
TypeAST.new(type_ast).type
|
245
156
|
)
|
246
157
|
desc = Contracts::Formatters::Expected.new(con, false).contract
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yard-contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon George
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|