strong_csv 0.3.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a367df39ccd195cbb7f62845c919f9718a4ef536de595294991fa7bc26693ca7
4
- data.tar.gz: 220e1d69c32123c78d5a31cb63d2c9c21686bbe76feb68e4f0aeb9762601e0e3
3
+ metadata.gz: 8078bf75d84307eb32f647a418d2cd2214d6891a78a25a73f7589b82fec0716c
4
+ data.tar.gz: 318af2a0c3a42ac7141819b06e729a5e98967c5106988bc4349b308f9c9d87f8
5
5
  SHA512:
6
- metadata.gz: bfe0ecbb668a07be731f9f50143812fc42d213993b4e3591e04678a4c6323347f69f18a6241d4dbfa524faa37baada63c7ffc0ab394073c1f435c914910f5d95
7
- data.tar.gz: d345d0c12929b60d801c16e8d994fa1570f0e5fc2034f6d74de3f96bf188a7a0e85f7b9c2b1bbe61a1a0f4e4cdb4a41deddf7915cbeb540ace4b0c790c5056b0
6
+ metadata.gz: 63aa30f83182e0cab08f816124945d9da91149faaa82017ba0a80ca8b39ef57fed5eab521db1efd8404045aadba189550bd779f195434cef2294975dc30216f3
7
+ data.tar.gz: e4e74ea2446efcf32f6e67fe8be5df207ae8dbd66b392df4c2fb8895cd71aebf70894d062e64eee2814295aa00f29876afcb95b4c3a88ffffd7be0c8e9a9ba21
data/README.md CHANGED
@@ -78,11 +78,11 @@ strong_csv = StrongCSV.new do
78
78
  end
79
79
  end
80
80
 
81
- # TODO: The followings are not implemented so far.
82
-
83
81
  # Regular expressions
84
82
  let :url, %r{\Ahttps://}
85
83
 
84
+ # TODO: The followings are not implemented so far.
85
+
86
86
  # Custom validation
87
87
  #
88
88
  # This example sees the database to fetch exactly stored `User` IDs,
@@ -158,6 +158,10 @@ end
158
158
  <td><a href="#literal"><code>"abc"</code> (String literal)</a></td>
159
159
  <td>The value must be casted to the specific <code>String</code> literal.</td>
160
160
  </tr>
161
+ <tr>
162
+ <td><a href="#literal"><code>%r{\Ahttps://}</code> (Regexp literal)</a></td>
163
+ <td>The value must be casted to a <code>String</code> that matches the specified Regexp.</td>
164
+ </tr>
161
165
  <tr>
162
166
  <td><a href="#union"><code>,</code> (Union type)</a></td>
163
167
  <td>The value must satisfy one of the subtypes.</td>
@@ -338,18 +342,19 @@ strong_csv = StrongCSV.new do
338
342
  let 1, "test"
339
343
  let 2, 2.5
340
344
  let 3, 1..10
345
+ let 4, /[a-z]+/
341
346
  end
342
347
 
343
348
  result = strong_csv.parse(<<~CSV)
344
- 123,test,2.5,9
345
- 123,test,2.5,0
346
- 123,Hey,2.5,10
349
+ 123,test,2.5,9,abc
350
+ 123,test,2.5,0,xyz
351
+ 123,Hey,2.5,10,!
347
352
  CSV
348
353
 
349
354
  result.map(&:valid?) # => [true, false, false]
350
- result[0].slice(0, 1, 2, 3) # => {0=>123, 1=>"test", 2=>2.5, 3=>9}
351
- result[1].slice(0, 1, 2, 3) # => {0=>123, 1=>"test", 2=>2.5, 3=>"0"} (0 is out of 1..10)
352
- result[2].slice(0, 1, 2, 3) # => {0=>123, 1=>"Hey", 2=>2.5, 3=>10} ("Hey" is not equal to "test")
355
+ result[0].slice(0, 1, 2, 3, 4) # => {0=>123, 1=>"test", 2=>2.5, 3=>9, 4=>"abc"}
356
+ result[1].slice(0, 1, 2, 3, 4) # => {0=>123, 1=>"test", 2=>2.5, 3=>"0", 4=>"xyz"} (0 is out of 1..10)
357
+ result[2].slice(0, 1, 2, 3, 4) # => {0=>123, 1=>"Hey", 2=>2.5, 3=>10, 4=>"!"} ("Hey" is not equal to "test", and "!" does not match /[a-z]+/)
353
358
  ```
354
359
 
355
360
  ### Union
@@ -405,6 +410,9 @@ ja:
405
410
  range:
406
411
  cant_be_casted: "`%{value}`は`%{expected}`の始端に変換できません"
407
412
  out_of_range: "`%{value}`は`%{range}`の範囲外です"
413
+ regexp:
414
+ cant_be_casted: "`%{value}`はStringに変換できません"
415
+ unexpected: "`%{value}`は`%{expected}`とマッチしませんでした"
408
416
  string:
409
417
  cant_be_casted: "`%{value}`はStringに変換できません"
410
418
  out_of_range: "`%{value}`の文字数は`%{range}`の範囲外です"
@@ -28,6 +28,10 @@ I18n.backend.store_translations(
28
28
  cant_be_casted: "`%{value}` can't be casted to the beginning of `%{expected}`",
29
29
  out_of_range: "`%{value}` is not within `%{range}`",
30
30
  },
31
+ regexp: {
32
+ cant_be_casted: "`%{value}` can't be casted to String",
33
+ unexpected: "`%{value}` did not match `%{expected}`",
34
+ },
31
35
  },
32
36
  string: {
33
37
  cant_be_casted: "`%{value}` can't be casted to String",
@@ -72,6 +72,20 @@ class StrongCSV
72
72
  end
73
73
  end
74
74
  end
75
+
76
+ refine ::Regexp do
77
+ # @param value [Object] Value to be casted to String
78
+ # @return [ValueResult]
79
+ def cast(value)
80
+ return ValueResult.new(original_value: value, error_messages: [I18n.t("strong_csv.literal.regexp.cant_be_casted", value: value.inspect, default: :"_strong_csv.literal.regexp.cant_be_casted")]) if value.nil?
81
+
82
+ if self =~ value
83
+ ValueResult.new(value: value, original_value: value)
84
+ else
85
+ ValueResult.new(original_value: value, error_messages: [I18n.t("strong_csv.literal.regexp.unexpected", value: value.inspect, expected: inspect, default: :"_strong_csv.literal.regexp.unexpected")])
86
+ end
87
+ end
88
+ end
75
89
  end
76
90
  end
77
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StrongCSV
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  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.3.0
4
+ version: 0.4.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-05-04 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n