typ-formatter 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/typ/formatter.rb +104 -25
  3. metadata +35 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e4e33337a51f5d9c3e197e29cfcf3e5e51bbcbb341c923e5c0efc6a711e240c
4
- data.tar.gz: 22be26a0cfdc410f9b828f26ce691376e6bdba52e3b5fa148013332435fd0fd2
3
+ metadata.gz: 247b0274ef455b8940e1c651f7559a995a53194da880873ebe7d403a27e754a0
4
+ data.tar.gz: e2aa7239e375f5f2f9a061347c55aba54c22d9adc27dfcde78ebe3aa660e6c4d
5
5
  SHA512:
6
- metadata.gz: 07062bb2dcd94f09916a69fa05eaa30bcc3e97114d8d956c10a8291b02d95ca9e2631be0fe1b1b0aff633933158c3316e489806bd9c45a18cae87408b1a29e18
7
- data.tar.gz: 42caa83d32d8a78692995ec255a2fd42ab55984e759e1d66f3aad29e0e548b69b920239c3e10e3b3f723a7f50afdcb041993881ae99a0a189ab18100aa9a3be9
6
+ metadata.gz: e549112ce1f63d3cc2bb8d0df3836fb7042cf91162f43b443fad2a3c84c160ba11b9ccf41b51eaabb3921fab884c752695925215238ca3b5fc30b74ee61494a2
7
+ data.tar.gz: bb1da432b0e3a2df36ce63717d8339bb36e09f00d8d8099010c698edce0c7cee75c1daa009e02015f2ed4d3fc406270e6695f133a34e4d4aa5d76c6a36d3e464
data/lib/typ/formatter.rb CHANGED
@@ -1,5 +1,108 @@
1
1
  module Typ
2
2
  class Formatter
3
+ module FormatGate
4
+ class Formatter
5
+ def initialize gate
6
+ @gate = gate
7
+ @string = format
8
+ end
9
+
10
+ attr_reader :gate
11
+
12
+ def to_s
13
+ @string
14
+ end
15
+
16
+ def head
17
+ @head ||= if gate.dsl_key
18
+ "#{gate.dsl_method} #{gate.dsl_key.inspect} #{gate.dsl_literal.inspect}"
19
+ else
20
+ "#{gate.dsl_method} #{gate.dsl_literal}"
21
+ end
22
+ end
23
+
24
+ def actual
25
+ end
26
+
27
+ def format
28
+ if gate.ok?
29
+ Rainbow(head).green
30
+ else
31
+ error = gate.error
32
+ string = case error
33
+ when Typ::Error::BadAssertion
34
+ actual_type = "got #{actual.class}".indent 2
35
+ actual_value = actual.ai.indent 4
36
+
37
+ actual_type + "\n" + actual_value
38
+ else
39
+ (error.class.to_s.indent 2) + ":\n" + (error.to_s.indent 4)
40
+ end
41
+
42
+ Rainbow(head).red + "\n" + string
43
+ end
44
+ end
45
+ end
46
+
47
+ module ToPascalCase
48
+ refine Symbol do
49
+ def to_pascal_case
50
+ to_s.split('_').map(&:capitalize).join
51
+ end
52
+ end
53
+ end
54
+
55
+ require 'ap'
56
+
57
+ using ToPascalCase
58
+
59
+ def format_gate gate
60
+ if gate.is_a? Typ
61
+ Typ::Formatter.new gate
62
+ else
63
+ FormatGate
64
+ .const_get(gate.dsl_method.to_pascal_case)
65
+ .new(gate)
66
+ end
67
+ end
68
+
69
+ class Is < Formatter
70
+ def format
71
+ literal = gate.dsl_literal
72
+
73
+ case literal
74
+ when Array
75
+ string = "is #{literal}"
76
+ gate.ok? ? Rainbow(string).green : Rainbow(string).red
77
+ end
78
+ end
79
+ end
80
+
81
+ class IsA < Formatter
82
+ def format
83
+ string = "is_a #{gate.dsl_literal}"
84
+
85
+ if gate.ok?
86
+ Rainbow(string).green
87
+ else
88
+ Rainbow(string).red + "\n" + "got #{gate.it.class}".indent(2) + "\n" + gate.it.ai.indent(4)
89
+ end
90
+ end
91
+ end
92
+
93
+ class Its < Formatter
94
+ def actual
95
+ gate.it.send gate.dsl_key
96
+ end
97
+ end
98
+
99
+ class Key < Formatter
100
+ def actual
101
+ gate.it[gate.dsl_key]
102
+ end
103
+ end
104
+ end
105
+
3
106
  require 'string/indent'
4
107
  require 'rainbow'
5
108
 
@@ -23,30 +126,6 @@ module Typ
23
126
  end.join "\n"
24
127
  end
25
128
 
26
- def format_gate gate
27
- case gate
28
- when Typ
29
- self.class.new gate
30
- when Is::Array
31
- format_is_array gate
32
- when IsA
33
- format_is_a gate
34
- end
35
- end
36
-
37
- def format_is_array gate
38
- string = "is #{gate.to_a}"
39
- gate.ok? ? Rainbow(string).green : Rainbow(string).red
40
- end
41
-
42
- def format_is_a gate
43
- is_a = "is_a #{gate.class.type}"
44
-
45
- if gate.ok?
46
- Rainbow(is_a).green
47
- else
48
- Rainbow(is_a).red + "\n" + "got #{gate.it.class}".indent(2)
49
- end
50
- end
129
+ include FormatGate
51
130
  end
52
131
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typ-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Anatoly Chernow
7
+ - Anatoly Chernov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
11
+ date: 2023-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typ
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rainbow
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,8 +52,23 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description:
42
- email:
70
+ email:
71
+ - chertoly@gmail.com
43
72
  executables: []
44
73
  extensions: []
45
74
  extra_rdoc_files: []
@@ -63,9 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
92
  - !ruby/object:Gem::Version
64
93
  version: '0'
65
94
  requirements: []
66
- rubyforge_project:
67
- rubygems_version: 2.7.6
95
+ rubygems_version: 3.4.15
68
96
  signing_key:
69
97
  specification_version: 4
70
- summary: A formatter to create nice-looking strings from Typs.
98
+ summary: To format validations made with typs.
71
99
  test_files: []