strong_csv 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/strong_csv/let.rb +2 -0
- data/lib/strong_csv/row.rb +3 -0
- data/lib/strong_csv/value_result.rb +4 -2
- data/lib/strong_csv/version.rb +1 -1
- data/lib/strong_csv.rb +1 -1
- data/sig/strong_csv.rbs +163 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 109d3d3c31812aedff005dc2003a8066d20a551513f0d287c0ba761a9469ac04
|
4
|
+
data.tar.gz: 11a3cd4890a60b27231276d09bcdc05fb29487c100fdbc6afff8cc7febf58694
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dd9abc5eb9a9b30f655ed13eae0e699c0471b09196ba5bc8ef265e06056ecf9bce526308f6a1edb28ff7d57ae9e5023e52b9e6bc827021cf186a8a62108026a
|
7
|
+
data.tar.gz: 3e9d411ab432f498639c8532b46be1d52e734188c48089b565a32398b690e8ceed1255b40adcc80ebc3d21256782f186576d7dd5cf8346f88b43fa6c2489ebfd
|
data/lib/strong_csv/let.rb
CHANGED
@@ -89,6 +89,7 @@ class StrongCSV
|
|
89
89
|
Types::String.new(**options)
|
90
90
|
end
|
91
91
|
|
92
|
+
# @param options [Hash] See `Types::String#initialize` for more details.
|
92
93
|
def string?(**options)
|
93
94
|
optional(string(**options))
|
94
95
|
end
|
@@ -98,6 +99,7 @@ class StrongCSV
|
|
98
99
|
Types::Time.new(**options)
|
99
100
|
end
|
100
101
|
|
102
|
+
# @param options [Hash] See `Types::Time#initialize` for more details.
|
101
103
|
def time?(**options)
|
102
104
|
optional(time(**options))
|
103
105
|
end
|
data/lib/strong_csv/row.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class StrongCSV
|
4
|
-
# ValueResult represents a CSV field is valid or not and contains its casted value if it's valid.
|
4
|
+
# ValueResult represents whether a CSV field is valid or not, and contains its casted value if it's valid.
|
5
5
|
class ValueResult
|
6
6
|
DEFAULT_VALUE = Object.new
|
7
7
|
private_constant :DEFAULT_VALUE
|
@@ -15,11 +15,13 @@ class StrongCSV
|
|
15
15
|
@error_messages = error_messages
|
16
16
|
end
|
17
17
|
|
18
|
-
# @return [Object] The casted value if it's valid. Otherwise, returns the original value.
|
18
|
+
# @return [::Float, ::Integer, ::Object, ::String, ::Range, Boolean, ::Time, nil] The casted value if it's valid. Otherwise, returns the original value.
|
19
19
|
def value
|
20
20
|
success? ? @value : @original_value
|
21
21
|
end
|
22
22
|
|
23
|
+
# It returns true if the value is successfully casted.
|
24
|
+
#
|
23
25
|
# @return [Boolean]
|
24
26
|
def success?
|
25
27
|
@error_messages.nil?
|
data/lib/strong_csv/version.rb
CHANGED
data/lib/strong_csv.rb
CHANGED
@@ -21,7 +21,7 @@ require_relative "strong_csv/types/union"
|
|
21
21
|
require_relative "strong_csv/let"
|
22
22
|
require_relative "strong_csv/row"
|
23
23
|
|
24
|
-
#
|
24
|
+
# StrongCSV is a library for parsing CSV contents with type checking.
|
25
25
|
class StrongCSV
|
26
26
|
class Error < StandardError; end
|
27
27
|
|
data/sig/strong_csv.rbs
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
# TypeProf 0.21.2
|
2
|
+
|
3
|
+
type literals = ::Integer | ::Float | ::Range[untyped] | ::String | ::Regexp
|
4
|
+
type declarable = StrongCSV::Types::Base | literals
|
5
|
+
type casted = bool | ::Float | ::Integer | ::Range[untyped] | ::String | ::Regexp | ::Time
|
6
|
+
type column = ::Integer | ::Symbol
|
7
|
+
|
8
|
+
# Classes
|
9
|
+
class StrongCSV
|
10
|
+
VERSION: String
|
11
|
+
|
12
|
+
@let: Let
|
13
|
+
|
14
|
+
def initialize: -> void
|
15
|
+
|
16
|
+
def parse: (String | IO, **untyped) -> Array[Row]
|
17
|
+
| (String | IO, **untyped) { (Row) -> void } -> untyped
|
18
|
+
|
19
|
+
class ValueResult
|
20
|
+
DEFAULT_VALUE: Object
|
21
|
+
|
22
|
+
@value: (casted)?
|
23
|
+
@original_value: untyped
|
24
|
+
|
25
|
+
attr_reader error_messages: [::String]?
|
26
|
+
|
27
|
+
def initialize: (original_value: ::String | nil, ?value: (casted | ::Object)?, ?error_messages: [::String]?) -> void
|
28
|
+
|
29
|
+
def value: -> ((casted | ::Object | nil)?)
|
30
|
+
|
31
|
+
def success?: -> bool
|
32
|
+
end
|
33
|
+
|
34
|
+
module Types
|
35
|
+
class Base
|
36
|
+
def cast: (untyped _value) -> bot
|
37
|
+
end
|
38
|
+
|
39
|
+
class Boolean < Base
|
40
|
+
TRUE_VALUES: Set[::String]
|
41
|
+
FALSE_VALUES: Set[::String]
|
42
|
+
|
43
|
+
def cast: (::String | nil) -> ValueResult
|
44
|
+
end
|
45
|
+
|
46
|
+
class Float < Base
|
47
|
+
DEFAULT_CONSTRAINT: ^(::Float) -> true
|
48
|
+
|
49
|
+
@constraint: ^(::Float) -> boolish
|
50
|
+
|
51
|
+
def initialize: (?constraint: ^(::Float) -> boolish) -> void
|
52
|
+
|
53
|
+
def cast: (::String | nil) -> ValueResult
|
54
|
+
end
|
55
|
+
|
56
|
+
class Integer < Base
|
57
|
+
DEFAULT_CONSTRAINT: ^(::Integer) -> true
|
58
|
+
|
59
|
+
@constraint: ^(::Integer) -> boolish
|
60
|
+
|
61
|
+
def initialize: (?constraint: ^(::Integer) -> boolish) -> void
|
62
|
+
|
63
|
+
def cast: (::String | nil) -> ValueResult
|
64
|
+
end
|
65
|
+
|
66
|
+
module Literal
|
67
|
+
def cast: (untyped value) -> ValueResult
|
68
|
+
| (untyped value) -> ValueResult
|
69
|
+
| (untyped value) -> ValueResult
|
70
|
+
| (untyped value) -> ValueResult
|
71
|
+
| (untyped value) -> ValueResult
|
72
|
+
end
|
73
|
+
|
74
|
+
class Optional < Base
|
75
|
+
@type: Boolean | Float | Integer | String | Time | Literal
|
76
|
+
|
77
|
+
def initialize: (declarable) -> void
|
78
|
+
|
79
|
+
def cast: (::String | nil) -> ValueResult
|
80
|
+
end
|
81
|
+
|
82
|
+
class String < Base
|
83
|
+
@within: Range[untyped] | nil
|
84
|
+
|
85
|
+
def initialize: (?within: Range[untyped] | nil) -> void
|
86
|
+
|
87
|
+
def cast: (::String | nil) -> ValueResult
|
88
|
+
end
|
89
|
+
|
90
|
+
class Time < Base
|
91
|
+
@format: ::String
|
92
|
+
|
93
|
+
def initialize: (?format: ::String) -> void
|
94
|
+
|
95
|
+
def cast: (::String | nil) -> ValueResult
|
96
|
+
end
|
97
|
+
|
98
|
+
class Union < Base
|
99
|
+
@types: Array[untyped]
|
100
|
+
|
101
|
+
def initialize: (untyped `type`, *untyped types) -> void
|
102
|
+
|
103
|
+
def cast: (::String | nil) -> untyped
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class Let
|
108
|
+
@picked: Hash[untyped, untyped]
|
109
|
+
|
110
|
+
attr_reader types: Hash[column, [declarable, ^(casted) -> untyped | nil]]
|
111
|
+
attr_reader headers: bool
|
112
|
+
attr_reader pickers: Hash[untyped, Proc]
|
113
|
+
|
114
|
+
def initialize: -> void
|
115
|
+
|
116
|
+
def let: (::String | column, declarable, *declarable) -> void
|
117
|
+
| (::String | column, declarable, *declarable) { (casted) -> untyped } -> void
|
118
|
+
|
119
|
+
def pick: (column, as: ::Symbol) { (Array[::String]) -> untyped } -> void
|
120
|
+
|
121
|
+
def integer: (**untyped) -> Types::Integer
|
122
|
+
|
123
|
+
def integer?: (**untyped) -> Types::Optional
|
124
|
+
|
125
|
+
def boolean: -> Types::Boolean
|
126
|
+
|
127
|
+
def boolean?: -> Types::Optional
|
128
|
+
|
129
|
+
def float: (**untyped) -> Types::Float
|
130
|
+
|
131
|
+
def float?: (**untyped) -> Types::Optional
|
132
|
+
|
133
|
+
def string: (**untyped) -> Types::String
|
134
|
+
|
135
|
+
def string?: (**untyped) -> Types::Optional
|
136
|
+
|
137
|
+
def time: (**untyped) -> Types::Time
|
138
|
+
|
139
|
+
def time?: (**untyped) -> Types::Optional
|
140
|
+
|
141
|
+
def optional: (*declarable) -> Types::Optional
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def validate_columns: -> bool
|
146
|
+
end
|
147
|
+
|
148
|
+
class Row
|
149
|
+
extend Forwardable
|
150
|
+
|
151
|
+
@values: Hash[column, casted | ::Object | nil]
|
152
|
+
|
153
|
+
attr_reader errors: Hash[column, Array[::String]]
|
154
|
+
attr_reader lineno: ::Integer
|
155
|
+
|
156
|
+
def initialize: (row: Array[::String] | CSV::Row, types: Hash[column, [declarable, (^(casted | ::Object | nil) -> untyped | nil)]], lineno: ::Integer) -> void
|
157
|
+
|
158
|
+
def valid?: -> bool
|
159
|
+
end
|
160
|
+
|
161
|
+
class Error < StandardError
|
162
|
+
end
|
163
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yutaka Kamei
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/strong_csv/types/union.rb
|
56
56
|
- lib/strong_csv/value_result.rb
|
57
57
|
- lib/strong_csv/version.rb
|
58
|
+
- sig/strong_csv.rbs
|
58
59
|
homepage: https://github.com/yykamei/strong_csv
|
59
60
|
licenses:
|
60
61
|
- MIT
|