rasti-types 1.1.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 668b604d95b67bb965b9860363c9708e7588ffdf4c2db16f82d7c4ec1c08e4bd
4
- data.tar.gz: 98f6a227331dae03e77d45813d40d34db1bef7e30c8724dd7a43c5f20369acd6
3
+ metadata.gz: cae616f3ffc603eeae05195b70c84866334862fd3e2d89a12f7fdab844a63d92
4
+ data.tar.gz: 85e9de6b5638d15368e16aa5bfd83c4aff1e5d9fc2d99ef1fdcf197e45e8b3fc
5
5
  SHA512:
6
- metadata.gz: 68255142316b26b850774f6bf07303f481a648d5ab81596685d4c15aee681bd1d5825867d0bc8c5168268f7eed9d431720f29393cd1e0d29fc0d278db8290f08
7
- data.tar.gz: 419d07e23e42296eac48d707d556cab894c7fccf4690caea1d79b77a7b27c99e2b897a90e80b34d55fea881578570e0200d3f156c106c57ca9303eeff618101e
6
+ metadata.gz: d1ea6bde041882f96f1d3dfce53db7b45ae6768bcac7792e09d32c7e339ca240e5bde5c43903cf4f782ef8461b1bf21339c28caa9eb2fbf8e5eb2c1fe00ccd6b
7
+ data.tar.gz: 6672abfc0879cdb0612bc5a32391a333288f2bc5746fb85cb0301bc8edf1713b17342c7dfb97a97ec94fd2e29cff7c98264ade1154feecd89cc85f4ab7c96f15
@@ -26,27 +26,13 @@ module Rasti
26
26
  end
27
27
 
28
28
  def transform(value)
29
- result = []
30
- errors = {}
31
-
32
- value.each_with_index do |e,i|
33
- index = i + 1
34
- begin
35
- result << type.cast(e)
36
-
37
- rescue CompoundError => ex
38
- ex.errors.each do |key, messages|
39
- errors["#{index}.#{key}"] = messages
40
- end
41
-
42
- rescue => ex
43
- errors[index] = [ex.message]
29
+ MultiCaster.cast!(self, value) do |multi_caster|
30
+ value.map.with_index do |e,i|
31
+ multi_caster.cast type: type,
32
+ value: e,
33
+ error_key: i+1
44
34
  end
45
35
  end
46
-
47
- raise MultiCastError.new(self, value, errors) unless errors.empty?
48
-
49
- result
50
36
  end
51
37
 
52
38
  end
@@ -27,11 +27,11 @@ module Rasti
27
27
  end
28
28
 
29
29
  def true_string?(value)
30
- !!value.match(TRUE_FORMAT)
30
+ !value.match(TRUE_FORMAT).nil?
31
31
  end
32
32
 
33
33
  def false_string?(value)
34
- !!value.match(FALSE_FORMAT)
34
+ !value.match(FALSE_FORMAT).nil?
35
35
  end
36
36
 
37
37
  end
@@ -3,6 +3,8 @@ module Rasti
3
3
  module Castable
4
4
 
5
5
  def cast(value)
6
+ return nil if value.nil?
7
+
6
8
  if valid? value
7
9
  transform! value
8
10
  else
@@ -24,8 +24,6 @@ module Rasti
24
24
 
25
25
  def valid?(value)
26
26
  values.include? String.cast(value)
27
- rescue
28
- false
29
27
  end
30
28
 
31
29
  def transform(value)
@@ -10,7 +10,7 @@ module Rasti
10
10
  private
11
11
 
12
12
  def valid?(value)
13
- !value.nil? && (valid_string?(value) || transformable?(value))
13
+ valid_string?(value) || transformable?(value)
14
14
  end
15
15
 
16
16
  def transform(value)
@@ -27,26 +27,19 @@ module Rasti
27
27
  end
28
28
 
29
29
  def transform(value)
30
- result = {}
31
- errors = {}
30
+ MultiCaster.cast!(self, value) do |multi_caster|
31
+ value.each_with_object({}) do |(k,v), hash|
32
+ casted_key = multi_caster.cast type: key_type,
33
+ value: k,
34
+ error_key: k
32
35
 
33
- value.each do |k,v|
34
- begin
35
- result[key_type.cast k] = value_type.cast v
36
+ casted_value = multi_caster.cast type: value_type,
37
+ value: v,
38
+ error_key: k
36
39
 
37
- rescue CompoundError => ex
38
- ex.errors.each do |key, messages|
39
- errors["#{k}.#{key}"] = messages
40
- end
41
-
42
- rescue => error
43
- errors[k] = [error.message]
40
+ hash[casted_key] = casted_value
44
41
  end
45
42
  end
46
-
47
- raise MultiCastError.new(self, value, errors) unless errors.empty?
48
-
49
- result
50
43
  end
51
44
 
52
45
  end
@@ -0,0 +1,40 @@
1
+ module Rasti
2
+ module Types
3
+ class MultiCaster
4
+
5
+ def self.cast!(type, value)
6
+ multi_caster = new type, value
7
+ result = yield multi_caster
8
+ multi_caster.raise_if_error!
9
+ result
10
+ end
11
+
12
+ def initialize(type, value)
13
+ @type = type
14
+ @value = value
15
+ @errors = ::Hash.new { |h,k| h[k] = [] }
16
+ end
17
+
18
+ def cast(type:, value:, error_key:)
19
+ type.cast value
20
+
21
+ rescue CompoundError => ex
22
+ ex.errors.each do |inner_error_key, messages|
23
+ errors["#{error_key}.#{inner_error_key}"] += messages
24
+ end
25
+
26
+ rescue => ex
27
+ errors[error_key] << ex.message
28
+ end
29
+
30
+ def raise_if_error!
31
+ raise MultiCastError.new(type, value, errors) unless errors.empty?
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :type, :value, :errors
37
+
38
+ end
39
+ end
40
+ end
@@ -21,7 +21,7 @@ module Rasti
21
21
  attr_reader :format
22
22
 
23
23
  def valid?(value)
24
- !value.nil? && value.respond_to?(:to_s) && valid_format?(value)
24
+ value.respond_to?(:to_s) && valid_format?(value)
25
25
  end
26
26
 
27
27
  def valid_format?(value)
@@ -8,7 +8,7 @@ module Rasti
8
8
  private
9
9
 
10
10
  def valid?(value)
11
- !value.nil? && (value.respond_to?(:to_sym) || value.respond_to?(:to_s))
11
+ value.respond_to?(:to_sym) || value.respond_to?(:to_s)
12
12
  end
13
13
 
14
14
  def transform(value)
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module Types
3
- VERSION = '1.1.2'
3
+ VERSION = '2.0.0'
4
4
  end
5
5
  end
data/spec/array_spec.rb CHANGED
@@ -3,7 +3,15 @@ require 'minitest_helper'
3
3
  describe Rasti::Types::Array do
4
4
 
5
5
  VALID_ARRAY = [1, '2', Time.now]
6
- INVALID_ARRAY = [nil, 1, 'text', :symbol, {a: 1, b: 2}, Object.new]
6
+ INVALID_ARRAY = [1, 'text', :symbol, {a: 1, b: 2}, Object.new]
7
+
8
+ it 'nil -> nil' do
9
+ Rasti::Types::Array[Rasti::Types::String].cast(nil).must_equal nil
10
+ end
11
+
12
+ it '[nil] -> [nil]' do
13
+ Rasti::Types::Array[Rasti::Types::String].cast([nil]).must_equal [nil]
14
+ end
7
15
 
8
16
  it "#{VALID_ARRAY.inspect} -> #{VALID_ARRAY.map(&:to_i)}" do
9
17
  Rasti::Types::Array[Rasti::Types::Integer].cast(VALID_ARRAY).must_equal VALID_ARRAY.map(&:to_i)
@@ -23,15 +31,14 @@ describe Rasti::Types::Array do
23
31
  describe 'Multi cast errors' do
24
32
 
25
33
  it 'Array of integers' do
26
- array = [1, 2 , 'a', 3, 'c', 4, nil]
34
+ array = [1, 2 , 'a', 3, 'c', 4]
27
35
 
28
36
  error = proc { Rasti::Types::Array[Rasti::Types::Integer].cast(array) }.must_raise Rasti::Types::MultiCastError
29
37
 
30
38
  error.errors.must_equal 3 => ["Invalid cast: 'a' -> Rasti::Types::Integer"],
31
- 5 => ["Invalid cast: 'c' -> Rasti::Types::Integer"],
32
- 7 => ["Invalid cast: nil -> Rasti::Types::Integer"]
39
+ 5 => ["Invalid cast: 'c' -> Rasti::Types::Integer"]
33
40
 
34
- error.message.must_equal "Cast errors:\n- 3: [\"Invalid cast: 'a' -> Rasti::Types::Integer\"]\n- 5: [\"Invalid cast: 'c' -> Rasti::Types::Integer\"]\n- 7: [\"Invalid cast: nil -> Rasti::Types::Integer\"]"
41
+ error.message.must_equal "Cast errors:\n- 3: [\"Invalid cast: 'a' -> Rasti::Types::Integer\"]\n- 5: [\"Invalid cast: 'c' -> Rasti::Types::Integer\"]"
35
42
  end
36
43
 
37
44
  it 'Array of models' do
data/spec/boolean_spec.rb CHANGED
@@ -2,6 +2,10 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Boolean do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Boolean.cast(nil).must_equal nil
7
+ end
8
+
5
9
  [true, 'true', 'True', 'TRUE', 'T'].each do |value|
6
10
  it "#{value.inspect} -> true" do
7
11
  Rasti::Types::Boolean.cast(value).must_equal true
@@ -14,7 +18,7 @@ describe Rasti::Types::Boolean do
14
18
  end
15
19
  end
16
20
 
17
- [nil, 'text', 123, :false, :true, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
21
+ ['text', 123, :false, :true, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
18
22
  it "#{value.inspect} -> CastError" do
19
23
  error = proc { Rasti::Types::Boolean.cast(value) }.must_raise Rasti::Types::CastError
20
24
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Boolean"
data/spec/enum_spec.rb CHANGED
@@ -2,17 +2,21 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Enum do
4
4
 
5
- enum = Rasti::Types::Enum[:a,:b,:c]
5
+ let(:enum) { Rasti::Types::Enum[:a,:b,:c] }
6
+
7
+ it 'nil -> nil' do
8
+ enum.cast(nil).must_be_nil
9
+ end
6
10
 
7
11
  [:a, 'b', "c"].each do |value|
8
- it "#{value.inspect} -> #{enum}" do
9
- Rasti::Types::Enum[:a,:b,:c].cast(value).must_equal value.to_s
12
+ it "#{value.inspect} -> #{value.to_s.inspect}" do
13
+ enum.cast(value).must_equal value.to_s
10
14
  end
11
15
  end
12
16
 
13
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
17
+ ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
14
18
  it "#{value.inspect} -> CastError" do
15
- error = proc { Rasti::Types::Enum[:a,:b,:c].cast(value) }.must_raise Rasti::Types::CastError
19
+ error = proc { enum.cast(value) }.must_raise Rasti::Types::CastError
16
20
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Enum[\"a\", \"b\", \"c\"]"
17
21
  end
18
22
  end
@@ -23,7 +27,7 @@ describe Rasti::Types::Enum do
23
27
  enum.first_value.must_equal 'first_value'
24
28
  enum.second_value.must_equal 'SecondValue'
25
29
  enum.third_value.must_equal 'THIRD_VALUE'
26
- enum.values.must_equal ['first_value', 'SecondValue', 'THIRD_VALUE']
30
+ enum.values.must_equal [enum.first_value, enum.second_value, enum.third_value]
27
31
  end
28
32
 
29
33
  end
data/spec/float_spec.rb CHANGED
@@ -2,13 +2,17 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Float do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Float.cast(nil).must_equal nil
7
+ end
8
+
5
9
  [100, '200', Time.now,2.0,"12.5"].each do |value|
6
10
  it "#{value.inspect} -> #{value.to_i}" do
7
11
  Rasti::Types::Float.cast(value).must_equal value.to_f
8
12
  end
9
13
  end
10
14
 
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, "1.", ".2","."].each do |value|
15
+ ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, "1.", ".2","."].each do |value|
12
16
  it "#{value.inspect} -> CastError" do
13
17
  error = proc { Rasti::Types::Float.cast(value) }.must_raise Rasti::Types::CastError
14
18
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Float"
data/spec/hash_spec.rb CHANGED
@@ -2,6 +2,14 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Hash do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::String].cast(nil).must_equal nil
7
+ end
8
+
9
+ it '{nil => nil} -> {nil => nil}' do
10
+ Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::String].cast(nil => nil).must_equal nil => nil
11
+ end
12
+
5
13
  it "{'a' => '123'} -> {a: 123}" do
6
14
  Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast('a' => '123').must_equal a: 123
7
15
  end
@@ -10,7 +18,7 @@ describe Rasti::Types::Hash do
10
18
  Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::String].cast('1' => :abc).must_equal 1 => 'abc'
11
19
  end
12
20
 
13
- [nil, 1, 'text', :symbol, Object.new].each do |value|
21
+ [1, 'text', :symbol, Object.new].each do |value|
14
22
  it "#{value.inspect} -> CastError" do
15
23
  error = proc { Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::CastError
16
24
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer]"
@@ -22,15 +30,17 @@ describe Rasti::Types::Hash do
22
30
  it 'Simple types' do
23
31
  value = {
24
32
  true => 1,
25
- 2 => false
33
+ 2 => false,
34
+ false => true
26
35
  }
27
36
 
28
37
  error = proc { Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::MultiCastError
29
38
 
30
39
  error.errors.must_equal true => ['Invalid cast: true -> Rasti::Types::Integer'],
31
- 2 => ['Invalid cast: false -> Rasti::Types::Integer']
40
+ 2 => ['Invalid cast: false -> Rasti::Types::Integer'],
41
+ false => ['Invalid cast: false -> Rasti::Types::Integer', 'Invalid cast: true -> Rasti::Types::Integer']
32
42
 
33
- error.message.must_equal "Cast errors:\n- true: [\"Invalid cast: true -> Rasti::Types::Integer\"]\n- 2: [\"Invalid cast: false -> Rasti::Types::Integer\"]"
43
+ error.message.must_equal "Cast errors:\n- true: [\"Invalid cast: true -> Rasti::Types::Integer\"]\n- 2: [\"Invalid cast: false -> Rasti::Types::Integer\"]\n- false: [\"Invalid cast: false -> Rasti::Types::Integer\", \"Invalid cast: true -> Rasti::Types::Integer\"]"
34
44
  end
35
45
 
36
46
  it 'Models' do
data/spec/integer_spec.rb CHANGED
@@ -2,13 +2,17 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Integer do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Integer.cast(nil).must_equal nil
7
+ end
8
+
5
9
  [100, '200', Time.now, 2.1, "12.5"].each do |value|
6
10
  it "#{value.inspect} -> #{value.to_i}" do
7
11
  Rasti::Types::Integer.cast(value).must_equal value.to_i
8
12
  end
9
13
  end
10
14
 
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
15
+ ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
16
  it "#{value.inspect} -> CastError" do
13
17
  error = proc { Rasti::Types::Integer.cast(value) }.must_raise Rasti::Types::CastError
14
18
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Integer"
data/spec/io_spec.rb CHANGED
@@ -2,13 +2,17 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::IO do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::IO.cast(nil).must_equal nil
7
+ end
8
+
5
9
  [StringIO.new, File.new(__FILE__)].each do |value|
6
10
  it "#{value.inspect} -> #{value}" do
7
11
  Rasti::Types::IO.cast(value).must_equal value
8
12
  end
9
13
  end
10
14
 
11
- [nil, 'text', 123, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
15
+ ['text', 123, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
16
  it "#{value.inspect} -> CastError" do
13
17
  error = proc { Rasti::Types::IO.cast(value) }.must_raise Rasti::Types::CastError
14
18
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::IO"
data/spec/model_spec.rb CHANGED
@@ -2,6 +2,10 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Model do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Model[Point].cast(nil).must_equal nil
7
+ end
8
+
5
9
  it 'Hash' do
6
10
  result = Rasti::Types::Model[Point].cast x: 1, y: 2
7
11
  result.must_be_instance_of Point
@@ -20,7 +24,7 @@ describe Rasti::Types::Model do
20
24
  error.message.must_equal "Errors:\n- x: [\"not present\"]\n- y: [\"not present\"]"
21
25
  end
22
26
 
23
- [nil, 'text', :symbol, 1, [1,2], Object.new].each do |value|
27
+ ['text', :symbol, 1, [1,2], Object.new].each do |value|
24
28
  it "#{value.inspect} -> CastError" do
25
29
  error = proc { Rasti::Types::Model[Point].cast(value) }.must_raise Rasti::Types::CastError
26
30
  error.message.must_equal "Invalid cast: #{as_string(value)} -> #{Rasti::Types::Model[Point]}"
data/spec/regexp_spec.rb CHANGED
@@ -2,13 +2,17 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Regexp do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Regexp.cast(nil).must_equal nil
7
+ end
8
+
5
9
  ['[a-z]', /[a-z]/].each do |value|
6
10
  it "#{value.inspect} -> #{Regexp.new(value).inspect}" do
7
11
  Rasti::Types::Regexp.cast(value).must_equal Regexp.new(value).source
8
12
  end
9
13
  end
10
14
 
11
- [nil, '[a-z', :symbol, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
15
+ ['[a-z', :symbol, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
12
16
  it "#{value.inspect} -> CastError" do
13
17
  error = proc { Rasti::Types::Regexp.cast(value) }.must_raise Rasti::Types::CastError
14
18
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Regexp"
@@ -2,7 +2,11 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::String, 'Formatted' do
4
4
 
5
- email_regexp = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
5
+ let(:email_regexp) { /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
6
+
7
+ it 'nil -> nil' do
8
+ Rasti::Types::String[email_regexp].cast(nil).must_equal nil
9
+ end
6
10
 
7
11
  ['user@mail.com'.to_sym, 'user.name-123@mail-test.com.ar'].each do |value|
8
12
  it "#{value.inspect} -> #{value.to_s}" do
@@ -10,7 +14,7 @@ describe Rasti::Types::String, 'Formatted' do
10
14
  end
11
15
  end
12
16
 
13
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
17
+ ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
14
18
  it "#{value.inspect} -> CastError" do
15
19
  error = proc { Rasti::Types::String[email_regexp].cast(value) }.must_raise Rasti::Types::CastError
16
20
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::String[#{as_string(email_regexp)}]"
data/spec/string_spec.rb CHANGED
@@ -2,15 +2,14 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::String do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::String.cast(nil).must_equal nil
7
+ end
8
+
5
9
  ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
10
  it "#{value.inspect} -> \"#{value.to_s}\"" do
7
11
  Rasti::Types::String.cast(value).must_equal value.to_s
8
12
  end
9
13
  end
10
14
 
11
- it 'nil -> CastError' do
12
- error = proc { Rasti::Types::String.cast(nil) }.must_raise Rasti::Types::CastError
13
- error.message.must_equal 'Invalid cast: nil -> Rasti::Types::String'
14
- end
15
-
16
15
  end
data/spec/symbol_spec.rb CHANGED
@@ -2,15 +2,14 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Symbol do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::Symbol.cast(nil).must_equal nil
7
+ end
8
+
5
9
  ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
10
  it "#{value.inspect} -> \"#{value.to_s.to_sym}\"" do
7
11
  Rasti::Types::Symbol.cast(value).must_equal value.to_s.to_sym
8
12
  end
9
13
  end
10
14
 
11
- it 'nil -> CastError' do
12
- error = proc { Rasti::Types::Symbol.cast(nil) }.must_raise Rasti::Types::CastError
13
- error.message.must_equal 'Invalid cast: nil -> Rasti::Types::Symbol'
14
- end
15
-
16
15
  end
data/spec/time_spec.rb CHANGED
@@ -2,20 +2,24 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::Time do
4
4
 
5
- time = Time.new 2016, 8, 18
5
+ TIME = Time.new 2016, 8, 18
6
6
 
7
- it "#{time.inspect} -> #{time.inspect}" do
8
- Rasti::Types::Time['%F %T %z'].cast(time).must_equal time
7
+ it 'nil -> nil' do
8
+ Rasti::Types::Time['%F %T %z'].cast(nil).must_equal nil
9
+ end
10
+
11
+ it "#{TIME.inspect} -> #{TIME.inspect}" do
12
+ Rasti::Types::Time['%F %T %z'].cast(TIME).must_equal TIME
9
13
  end
10
14
 
11
15
  ['%d/%m/%y', '%Y-%m-%d'].each do |format|
12
- time_string = time.strftime(format)
13
- it "#{time_string.inspect} -> #{time.inspect}" do
14
- Rasti::Types::Time[format].cast(time_string).must_equal time
16
+ time_string = TIME.strftime(format)
17
+ it "#{time_string.inspect} -> #{TIME.inspect}" do
18
+ Rasti::Types::Time[format].cast(time_string).must_equal TIME
15
19
  end
16
20
  end
17
21
 
18
- [time.strftime('%d/%m/%y'), 'text', nil, 1, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
22
+ [TIME.strftime('%d/%m/%y'), 'text', 1, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
19
23
  it "#{value.inspect} -> CastError" do
20
24
  error = proc { Rasti::Types::Time['%F'].cast(value) }.must_raise Rasti::Types::CastError
21
25
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Time['%F']"
@@ -4,13 +4,17 @@ describe Rasti::Types::TypedEnum do
4
4
 
5
5
  let(:enum) { Rasti::Types::TypedEnum[Colors] }
6
6
 
7
+ it 'nil -> nil' do
8
+ enum.cast(nil).must_equal nil
9
+ end
10
+
7
11
  ['BLUE', 'GREEN', 'RED'].each do |value|
8
12
  it "#{value.inspect} -> #{value}" do
9
13
  enum.cast(value).must_equal Colors[value]
10
14
  end
11
15
  end
12
16
 
13
- ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new, nil].each do |value|
17
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
14
18
  it "#{value.inspect} -> CastError" do
15
19
  error = proc { enum.cast(value) }.must_raise Rasti::Types::CastError
16
20
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::TypedEnum[Colors]"
data/spec/uuid_spec.rb CHANGED
@@ -2,13 +2,17 @@ require 'minitest_helper'
2
2
 
3
3
  describe Rasti::Types::UUID do
4
4
 
5
+ it 'nil -> nil' do
6
+ Rasti::Types::UUID.cast(nil).must_equal nil
7
+ end
8
+
5
9
  ['f09b7716-81a9-11e6-a549-bb8f165bcf02', '12345678-1234-1234-1234-123456789123'].each do |value|
6
10
  it "#{value.inspect} -> #{value.to_s}" do
7
11
  Rasti::Types::UUID.cast(value).must_equal value.to_s
8
12
  end
9
13
  end
10
14
 
11
- [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5, 'f09b7716-81a9-11e6-a549-bb16502', 'f09b7716-11e6-a549-bb8f16502', '-84a9-11e6-a549-bb8f16502', 'f09b7716-81a9-11e6-a549-bh16502'].each do |value|
15
+ ['text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5, 'f09b7716-81a9-11e6-a549-bb16502', 'f09b7716-11e6-a549-bb8f16502', '-84a9-11e6-a549-bb8f16502', 'f09b7716-81a9-11e6-a549-bh16502'].each do |value|
12
16
  it "#{value.inspect} -> CastError" do
13
17
  error = proc { Rasti::Types::UUID.cast(value) }.must_raise Rasti::Types::CastError
14
18
  error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::UUID"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-13 00:00:00.000000000 Z
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require
@@ -184,6 +184,7 @@ files:
184
184
  - lib/rasti/types/integer.rb
185
185
  - lib/rasti/types/io.rb
186
186
  - lib/rasti/types/model.rb
187
+ - lib/rasti/types/multi_caster.rb
187
188
  - lib/rasti/types/regexp.rb
188
189
  - lib/rasti/types/string.rb
189
190
  - lib/rasti/types/symbol.rb