acfs 0.45.0 → 0.46.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
  SHA1:
3
- metadata.gz: 94d04da7b680edf457505737b4bcc1d18d6e3fb3
4
- data.tar.gz: 629b89356e8be9625b20722ca89bfe88ff3d5bc9
3
+ metadata.gz: 2ba49d6dd8c50e454b8f3840420655bb3c4ce682
4
+ data.tar.gz: 643a02f5bf2bee3a9dc58fc2bd1eacc4bbac4b03
5
5
  SHA512:
6
- metadata.gz: 7afc2b923b468780cd3dabd0d6ff592e829b2b158d3b96469513ca324e2644a6e4d30f48408efb0ef48bc53e35595b963b8700131d8a8a81933eb4bef8f324b1
7
- data.tar.gz: 45349a36fa4c76f564f95897102e74e4a934b9ddee69cb785868a9b75a2fd652b4ada7687d4c974a856a65b1dfb18caf372eaa963211851edf75edbc8a1a40d1
6
+ metadata.gz: 72c52711056563f03ab5ccd497ea81470934c899e0c3e0f0bc5c66ad3e05e60194066281f2545afd37365876d7c7910c26bcad2efd6c60d10ff295e7dd9ac0f5
7
+ data.tar.gz: a72dc58cc2f8934aba2d57d69a1171c316bbbb98b5ae912727833afbb0e66d7b3a981b7b6332ad8ab0fd74f70f1d51e0616dc741a4df5d9d911cb2792c31817a
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.46.0
4
+
5
+ * Rework types system (#39)
6
+
3
7
  ## 0.45.0
4
8
 
5
9
  * Fetching multiple records (`find(ary)`) is stable now, but untested (#38)
@@ -1,35 +1,27 @@
1
1
  module Acfs::Resource::Attributes
2
2
  #
3
3
  class Base
4
- attr_reader :options
4
+ attr_reader :default
5
5
 
6
- def initialize(opts = {})
7
- @options = opts
8
- @options.reverse_merge! allow_nil: true
6
+ def initialize(default: nil)
7
+ @default = default
9
8
  end
10
9
 
11
- def nil_allowed?
12
- options[:allow_nil]
13
- end
14
-
15
- def blank_allowed?
16
- options[:allow_blank]
10
+ def cast(value)
11
+ cast_value(value) unless value.nil?
17
12
  end
18
13
 
19
14
  def default_value
20
- if options[:default].is_a? Proc
21
- options[:default]
15
+ if default.respond_to? :call
16
+ default
22
17
  else
23
- cast options[:default]
18
+ cast default
24
19
  end
25
20
  end
26
21
 
27
- def cast(obj)
28
- return nil if obj.nil? && nil_allowed? || (obj == '' && blank_allowed?)
29
- cast_type obj
30
- end
22
+ private
31
23
 
32
- def cast_type(_obj)
24
+ def cast_value(_value)
33
25
  raise NotImplementedError
34
26
  end
35
27
  end
@@ -14,20 +14,24 @@ module Acfs::Resource::Attributes
14
14
  # true, on, yes
15
15
  #
16
16
  class Boolean < Base
17
- TRUE_VALUES = %w(true on yes 1)
17
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].to_set
18
18
 
19
19
  # @api public
20
20
  #
21
21
  # Cast given object to boolean.
22
22
  #
23
- # @param [Object] obj Object to cast.
23
+ # @param [Object] value Object to cast.
24
24
  # @return [TrueClass, FalseClass] Casted boolean.
25
25
  #
26
- def cast_type(obj)
27
- return true if obj.is_a? TrueClass
28
- return false if obj.is_a? FalseClass
26
+ def cast_value(value)
27
+ return true if value == true
28
+ return false if value == false
29
29
 
30
- TRUE_VALUES.include? obj.to_s
30
+ if value.blank?
31
+ nil
32
+ else
33
+ !FALSE_VALUES.include?(value)
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -10,22 +10,21 @@ module Acfs::Resource::Attributes
10
10
  # end
11
11
  #
12
12
  class DateTime < Base
13
+
13
14
  # @api public
14
15
  #
15
16
  # Cast given object to DateTime.
16
17
  #
17
- # @param [Object] obj Object to cast.
18
+ # @param [Object] value Object to cast.
18
19
  # @return [DateTime] Casted object as DateTime.
19
20
  #
20
- def cast_type(obj)
21
- if nil_allowed? && obj.blank?
21
+ def cast_value(value)
22
+ if value.blank?
22
23
  nil
23
- elsif obj.is_a? ::DateTime
24
- obj
25
- elsif obj.is_a?(Time) || obj.is_a?(Date)
26
- ::DateTime.iso8601 obj.iso8601
24
+ elsif value.acts_like?(:time) || value.acts_like?(:date)
25
+ value.to_datetime
27
26
  else
28
- ::DateTime.iso8601 obj
27
+ ::DateTime.iso8601 value
29
28
  end
30
29
  end
31
30
  end
@@ -14,14 +14,24 @@ module Acfs::Resource::Attributes
14
14
  #
15
15
  # Cast given object to a dict/hash.
16
16
  #
17
- # @param [Object] obj Object to cast.
17
+ # @param [Object] value Object to cast.
18
18
  # @return [Hash] Casted object as hash.
19
19
  # @raise [TypeError] If object cannot be casted to a hash.
20
20
  #
21
- def cast_type(obj)
22
- return obj if obj.is_a? Hash
23
- return obj.to_h if obj.respond_to? :to_h
24
- raise TypeError.new "Cannot cast #{obj.inspect} to hash."
21
+ def cast_value(value)
22
+ return nil if value.blank?
23
+
24
+ if value.is_a?(Hash)
25
+ value
26
+ elsif value.respond_to?(:serializable_hash)
27
+ value.serializable_hash
28
+ elsif value.respond_to?(:to_hash)
29
+ value.to_hash
30
+ elsif value.respond_to?(:to_h)
31
+ value.to_h
32
+ else
33
+ Hash(value)
34
+ end
25
35
  end
26
36
  end
27
37
  end
@@ -13,11 +13,19 @@ module Acfs::Resource::Attributes
13
13
  #
14
14
  # Cast given object to float.
15
15
  #
16
- # @param [Object] obj Object to cast.
16
+ # @param [Object] value Object to cast.
17
17
  # @return [Float] Casted object as float.
18
18
  #
19
- def cast_type(obj)
20
- Float obj
19
+ def cast_value(value)
20
+ return 0.0 if value.blank?
21
+
22
+ case value
23
+ when ::Float then value
24
+ when "Infinity" then ::Float::INFINITY
25
+ when "-Infinity" then -::Float::INFINITY
26
+ when "NaN" then ::Float::NAN
27
+ else Float(value)
28
+ end
21
29
  end
22
30
  end
23
31
  end
@@ -13,15 +13,15 @@ module Acfs::Resource::Attributes
13
13
  #
14
14
  # Cast given object to integer.
15
15
  #
16
- # @param [Object] obj Object to cast.
16
+ # @param [Object] value Object to cast.
17
17
  # @return [Fixnum] Casted object as fixnum.
18
18
  #
19
- def cast_type(obj)
20
- return 0 if obj == ''
21
-
22
- Integer obj
23
- rescue ArgumentError => e
24
- raise TypeError.new e.message
19
+ def cast_value(value)
20
+ if value.blank?
21
+ 0
22
+ else
23
+ Integer(value)
24
+ end
25
25
  end
26
26
  end
27
27
  end
@@ -13,13 +13,22 @@ module Acfs::Resource::Attributes
13
13
  #
14
14
  # Cast given object to a list.
15
15
  #
16
- # @param [Object] obj Object to cast.
16
+ # @param [Object] value Object to cast.
17
17
  # @return [Fixnum] Casted object as list.
18
18
  # @raise [TypeError] If object cannot be casted to a list.
19
19
  #
20
- def cast_type(obj)
21
- return obj.to_a if obj.respond_to? :to_a
22
- raise TypeError.new "Cannot cast #{obj.inspect} to array."
20
+ def cast_value(value)
21
+ return nil if value.blank?
22
+
23
+ if value.is_a?(::Array)
24
+ value
25
+ elsif value.respond_to?(:to_ary)
26
+ value.to_ary
27
+ elsif value.respond_to?(:to_a)
28
+ value.to_a
29
+ else
30
+ Array(value)
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -14,11 +14,11 @@ module Acfs::Resource::Attributes
14
14
  #
15
15
  # Cast given object to string.
16
16
  #
17
- # @param [Object] obj Object to cast.
17
+ # @param [Object] value Object to cast.
18
18
  # @return [String] Casted string.
19
19
  #
20
- def cast_type(obj)
21
- obj.to_s
20
+ def cast_value(value)
21
+ value.to_s
22
22
  end
23
23
  end
24
24
  end
@@ -10,7 +10,7 @@ module Acfs::Resource::Attributes
10
10
  #
11
11
  class UUID < Base
12
12
  #
13
- REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
13
+ UUID_REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
14
14
 
15
15
  # @api public
16
16
  #
@@ -28,23 +28,16 @@ module Acfs::Resource::Attributes
28
28
  # | 4 | 4 |
29
29
  # | 5 | 12 |
30
30
  #
31
- # @param [Object] obj Object to cast.
31
+ # @param [Object] value Object to cast.
32
32
  # @return [String] Casted object as UUID.
33
33
  #
34
- def cast_type(obj)
35
- cast_string obj.to_s
36
- end
37
-
38
- private
39
-
40
- def cast_string(str)
41
- if nil_allowed? && str.blank?
42
- return nil
43
- elsif str =~ REGEXP
44
- str
34
+ def cast_value(value)
35
+ if value.blank?
36
+ nil
37
+ elsif value.to_s =~ UUID_REGEXP
38
+ value
45
39
  else
46
- raise ArgumentError.new "given String `#{str}` " \
47
- 'does not look like a UUID'
40
+ raise TypeError.new "Invalid UUID: `#{value.to_s}'"
48
41
  end
49
42
  end
50
43
  end
@@ -1,7 +1,7 @@
1
1
  module Acfs
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 45
4
+ MINOR = 46
5
5
  PATCH = 0
6
6
  STAGE = nil
7
7
 
@@ -3,23 +3,54 @@ require 'spec_helper'
3
3
  describe Acfs::Resource::Attributes::Boolean do
4
4
  subject { Acfs::Resource::Attributes::Boolean.new }
5
5
 
6
- describe 'cast' do
7
- it 'should preserve boolean values' do
6
+ describe '#cast' do
7
+ it 'casts nil' do
8
+ expect(subject.cast(nil)).to eq nil
9
+ end
10
+
11
+ it 'casts empty string to false' do
12
+ expect(subject.cast('')).to eq nil
13
+ end
14
+
15
+ it 'casts blank string to false' do
16
+ expect(subject.cast(" \t")).to eq nil
17
+ end
18
+
19
+ it 'preserves boolean values' do
8
20
  expect(subject.cast(false)).to eq false
9
21
  expect(subject.cast(true)).to eq true
10
22
  end
11
23
 
12
- it 'should cast TRUE_VALUES to true' do
24
+ it 'casts falsy values to false' do
25
+ expect(subject.cast(false)).to eq false
26
+ expect(subject.cast(0)).to eq false
27
+ expect(subject.cast('0')).to eq false
28
+ expect(subject.cast('no')).to eq false
29
+ expect(subject.cast('NO')).to eq false
30
+ expect(subject.cast('off')).to eq false
31
+ expect(subject.cast('OFF')).to eq false
32
+ expect(subject.cast('false')).to eq false
33
+ expect(subject.cast('FALSE')).to eq false
34
+ expect(subject.cast('f')).to eq false
35
+ expect(subject.cast('F')).to eq false
36
+ end
37
+
38
+ it 'casts any other value to true' do
39
+ expect(subject.cast(true)).to eq true
40
+ expect(subject.cast(1)).to eq true
41
+ expect(subject.cast('1')).to eq true
13
42
  expect(subject.cast('yes')).to eq true
43
+ expect(subject.cast('YES')).to eq true
14
44
  expect(subject.cast('on')).to eq true
45
+ expect(subject.cast('ON')).to eq true
15
46
  expect(subject.cast('true')).to eq true
16
- expect(subject.cast('1')).to eq true
17
- end
47
+ expect(subject.cast('TRUE')).to eq true
48
+ expect(subject.cast('t')).to eq true
49
+ expect(subject.cast('T')).to eq true
18
50
 
19
- it 'should cast any other value to false' do
20
- expect(subject.cast('')).to eq false
21
- expect(subject.cast('wrong')).to eq false
22
- expect(subject.cast('random')).to eq false
51
+ expect(subject.cast(2)).to eq true
52
+ expect(subject.cast('wrong')).to eq true
53
+ expect(subject.cast('random')).to eq true
23
54
  end
24
55
  end
25
56
  end
@@ -1,55 +1,49 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::DateTime do
4
- let(:model) { Class.new Acfs::Resource }
5
- let(:params) { {} }
6
- subject { Acfs::Resource::Attributes::DateTime.new params }
4
+ let(:type) { Acfs::Resource::Attributes::DateTime.new }
7
5
 
8
- describe 'cast' do
9
- it 'should return same object, if obj is already of DateTime class' do
10
- date_time = DateTime.now
11
- retval = subject.cast(date_time)
12
- expect(retval).to be == date_time
6
+ describe '#cast' do
7
+ subject { -> { type.cast value } }
8
+
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
13
12
  end
14
13
 
15
- it 'should return parsed object, if obj is of Time class' do
16
- time = Time.now
17
- retval = subject.cast(time)
18
- expect(retval).to be == DateTime.iso8601(time.iso8601)
14
+ context 'with empty string' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq nil }
19
17
  end
20
18
 
21
- it 'should return parsed object, if obj is of Date class' do
22
- date = Date.today
23
- retval = subject.cast(date)
24
- expect(retval).to be == DateTime.iso8601(date.iso8601)
19
+ context 'with blank string' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq nil }
25
22
  end
26
23
 
27
- it 'should return parsed object, if obj is of String class in ISO-8601 format' do
28
- date_time_string = DateTime.now.iso8601
29
- retval = subject.cast(date_time_string)
30
- expect(retval).to be == DateTime.iso8601(date_time_string)
24
+ context 'with DateTime' do
25
+ let(:value) { DateTime.now }
26
+ it { expect(subject.call).to eq value }
31
27
  end
32
28
 
33
- it 'should raise an error if obj is of String class not in valid ISO-8601 format' do
34
- malformed_string = 'qwe123'
35
- expect do
36
- subject.cast(malformed_string)
37
- end.to raise_error ArgumentError
29
+ context 'with Time' do
30
+ let(:value) { Time.now }
31
+ it { expect(subject.call).to eq value.to_datetime }
38
32
  end
39
33
 
40
- it 'should raise an error if obj is of wrong class (Fixnum)' do
41
- fixnum = 12
42
- expect do
43
- subject.cast(fixnum)
44
- end.to raise_error TypeError
34
+ context 'with Date' do
35
+ let(:value) { Date.today }
36
+ it { expect(subject.call).to eq value.to_datetime }
45
37
  end
46
38
 
47
- context 'with allow_nil option' do
48
- let(:params) { {allow_nil: true} }
39
+ context 'with ISO8601' do
40
+ let(:value) { DateTime.now.iso8601 }
41
+ it { expect(subject.call.iso8601).to eq value }
42
+ end
49
43
 
50
- it 'should accept empty string as nil' do
51
- expect(subject.cast('')).to eq nil
52
- end
44
+ context 'with invalid string' do
45
+ let(:value) { 'qwe123' }
46
+ it { is_expected.to raise_error ArgumentError }
53
47
  end
54
48
  end
55
49
  end
@@ -1,50 +1,75 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::Dict do
4
- let(:model) { Class.new(Acfs::Resource) }
5
- subject { Acfs::Resource::Attributes::Dict.new }
4
+ let(:type) { Acfs::Resource::Attributes::Dict.new }
6
5
 
7
- describe '.cast' do
8
- context 'with hash' do
9
- let(:sample) { {3 => true, 'asfd' => 4} }
6
+ describe '#cast' do
7
+ subject { -> { type.cast value } }
10
8
 
11
- it 'should return unmodified hash' do
12
- expect(subject.cast(sample)).to be sample
13
- end
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
12
+ end
13
+
14
+ context 'with blank string (I)' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq nil }
17
+ end
18
+
19
+ context 'with blank string (II)' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq nil }
22
+ end
23
+
24
+ context 'with hash' do
25
+ let(:value) { {3 => true, abc: 4} }
26
+ it { expect(subject.call).to eq value }
14
27
  end
15
28
 
16
- context 'with not hashable object' do
17
- let(:sample) { Object.new }
29
+ context 'with non hashable object' do
30
+ let(:value) { Object.new }
31
+ it { is_expected.to raise_error TypeError }
32
+ end
18
33
 
19
- it 'should raise a TypeError' do
20
- expect do
21
- subject.cast(sample)
22
- end.to raise_error TypeError
34
+ context 'with hashable object (I)' do
35
+ let(:value) do
36
+ Class.new do
37
+ def to_hash
38
+ {id: object_id}
39
+ end
40
+ end.new
23
41
  end
42
+
43
+ it { expect(subject.call).to eq id: value.object_id }
24
44
  end
25
45
 
26
- context 'with hashable object' do
27
- let(:sample) do
28
- o = Object.new
29
- class << o
46
+ context 'with hashable object (II)' do
47
+ let(:value) do
48
+ Class.new do
30
49
  def to_h
31
- {3 => 4, 'test' => true}
50
+ {id: object_id}
32
51
  end
33
- end
34
- o
52
+ end.new
35
53
  end
36
54
 
37
- it 'should cast object to hash' do
38
- expect(subject.cast(sample)).to eq 3 => 4, 'test' => true
39
- end
55
+ it { expect(subject.call).to eq id: value.object_id }
40
56
  end
41
57
 
42
- context 'with hash subclass' do
43
- let(:sample) { HashWithIndifferentAccess.new :test => :foo, 34 => 12 }
44
-
45
- it 'should return obj unmodified' do
46
- expect(subject.cast(sample)).to be sample
58
+ context 'with serializable object' do
59
+ let(:value) do
60
+ Class.new do
61
+ def serializable_hash
62
+ {id: object_id}
63
+ end
64
+ end.new
47
65
  end
66
+
67
+ it { expect(subject.call).to eq id: value.object_id }
68
+ end
69
+
70
+ context 'with hash subclass object' do
71
+ let(:value) { HashWithIndifferentAccess.new test: :foo }
72
+ it { expect(subject.call).to be value }
48
73
  end
49
74
  end
50
75
  end
@@ -1,20 +1,59 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::Float do
4
- let(:model) { Class.new Acfs::Resource }
5
- subject { described_class.new }
4
+ let(:type) { Acfs::Resource::Attributes::Float.new }
6
5
 
7
- describe 'cast' do
8
- it 'should return same object, if obj is already of float class' do
9
- expect(subject.cast(1.3)).to be == 1.3
6
+ describe '#cast' do
7
+ subject { -> { type.cast value } }
8
+
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
12
+ end
13
+
14
+ context 'with blank string (I)' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq 0.0 }
17
+ end
18
+
19
+ context 'with blank string (II)' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq 0.0 }
22
+ end
23
+
24
+ context 'with float' do
25
+ let(:value) { 1.7 }
26
+ it { expect(subject.call).to eq 1.7 }
27
+ end
28
+
29
+ context 'with Infinity' do
30
+ let(:value) { 'Infinity' }
31
+ it { expect(subject.call).to eq ::Float::INFINITY }
32
+ end
33
+
34
+ context 'with -Infinity' do
35
+ let(:value) { '-Infinity' }
36
+ it { expect(subject.call).to eq -::Float::INFINITY }
37
+ end
38
+
39
+ context 'with NaN' do
40
+ let(:value) { 'NaN' }
41
+ it { expect(subject.call).to be_nan }
42
+ end
43
+
44
+ context 'with fixnum' do
45
+ let(:value) { 1 }
46
+ it { expect(subject.call).to eq 1.0 }
10
47
  end
11
48
 
12
- it 'should return parsed object, if obj is of Fixnum class' do
13
- expect(subject.cast(7)).to be == 7.0
49
+ context 'with valid string' do
50
+ let(:value) { '1.7' }
51
+ it { expect(subject.call).to eq 1.7 }
14
52
  end
15
53
 
16
- it 'should return parsed object, if obj is of String class containing a float' do
17
- expect(subject.cast('1.7')).to be == 1.7
54
+ context 'with invalid string (I)' do
55
+ let(:value) { '1.7a' }
56
+ it { is_expected.to raise_error ArgumentError }
18
57
  end
19
58
  end
20
59
  end
@@ -1,19 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::Integer do
4
- subject { Acfs::Resource::Attributes::Integer.new }
4
+ let(:type) { Acfs::Resource::Attributes::Integer.new }
5
5
 
6
- describe 'cast' do
7
- it 'should cast integer strings' do
8
- expect(subject.cast('123')).to eq 123
6
+ describe '#cast' do
7
+ subject { -> { type.cast value } }
8
+
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
12
+ end
13
+
14
+ context 'with empty string' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq 0 }
17
+ end
18
+
19
+ context 'with blank string' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq 0 }
9
22
  end
10
23
 
11
- it 'should cast empty string (backward compatibility)' do
12
- expect(subject.cast('')).to eq 0
24
+ context 'with string' do
25
+ let(:value) { '123' }
26
+ it { expect(subject.call).to eq 123 }
13
27
  end
14
28
 
15
- it 'should not cast invalid integers' do
16
- expect { subject.cast 'abc' }.to raise_error TypeError
29
+ context 'with invalid string' do
30
+ let(:value) { '123a' }
31
+ it { is_expected.to raise_error ArgumentError }
17
32
  end
18
33
  end
19
34
  end
@@ -1,34 +1,58 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::List do
4
- let(:model) { Class.new Acfs::Resource }
5
- subject { described_class.new }
4
+ let(:type) { Acfs::Resource::Attributes::List.new }
6
5
 
7
- describe '.cast' do
8
- context 'with array' do
9
- let(:sample) { %w(abc cde efg) }
6
+ describe '#cast' do
7
+ subject { -> { type.cast value } }
10
8
 
11
- it 'should return unmodified array' do
12
- expect(subject.cast(sample)).to be == %w(abc cde efg)
13
- end
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
14
12
  end
15
13
 
16
- context 'with not listable object' do
17
- let(:sample) { Object.new }
14
+ context 'with blank string (I)' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq nil }
17
+ end
18
18
 
19
- it 'should raise a TypeError' do
20
- expect do
21
- subject.cast(sample)
22
- end.to raise_error TypeError
23
- end
19
+ context 'with blank string (II)' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq nil }
24
22
  end
25
23
 
26
- context 'with listable object' do
27
- let(:sample) { 5..10 }
24
+ context 'with array' do
25
+ let(:value) { %w(abc cde efg) }
26
+ it { expect(subject.call).to eq value }
27
+ end
28
28
 
29
- it 'should cast object to array' do
30
- expect(subject.cast(sample)).to be == [5, 6, 7, 8, 9, 10]
29
+ context 'with convertable object (I)' do
30
+ let(:value) do
31
+ Class.new do
32
+ def to_ary
33
+ [1, 2, 3]
34
+ end
35
+ end.new
31
36
  end
37
+
38
+ it { expect(subject.call).to eq [1, 2, 3] }
39
+ end
40
+
41
+ context 'with convertable object (II)' do
42
+ let(:value) do
43
+ Class.new do
44
+ def to_a
45
+ [1, 2, 3]
46
+ end
47
+ end.new
48
+ end
49
+
50
+ it { expect(subject.call).to eq [1, 2, 3] }
51
+ end
52
+
53
+ context 'with non castable object' do
54
+ let(:value) { Object.new }
55
+ it { expect(subject.call).to eq [value] }
32
56
  end
33
57
  end
34
58
  end
@@ -1,63 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Resource::Attributes::UUID do
4
- let(:model) { Class.new Acfs::Resource }
5
- let(:params) { {} }
6
- let(:instance) { described_class.new params }
7
- subject { instance }
8
-
9
- describe '#cast_type' do
10
- let(:param) { '450b7a40-94ad-11e3-baa8-0800200c9a66' }
11
- let(:action) { instance.cast param }
12
- subject { action }
13
-
14
- context 'with String as param' do
15
- context 'with valid UUID' do
16
- let(:param) { '450b7a40-94ad-11e3-baa8-0800200c9a66' }
17
- it { should be_a String }
18
- it { should eq param }
19
- end
20
-
21
- context 'with invalid UUID' do
22
- subject { -> { action } }
23
-
24
- context 'with random non-empty string' do
25
- let(:param) { 'invalid string' }
26
- it { should raise_error ArgumentError }
27
- end
28
-
29
- context 'with UUID string containing invalid characters' do
30
- let(:param) { 'xxxxxxxx-yyyy-11e3-baa8-0800200c9a66' }
31
- it { should raise_error ArgumentError }
32
- end
33
-
34
- context 'with empty string' do
35
- let(:param) { '' }
36
-
37
- context 'with allow_nil option' do
38
- let(:params) { {allow_nil: true} }
39
- subject { action }
40
- it { should eq nil }
41
- end
42
-
43
- context 'without allow_nil option' do
44
- let(:params) { {allow_nil: false} }
45
- it { should raise_error ArgumentError }
46
- end
47
- end
48
- end
4
+ let(:type) { Acfs::Resource::Attributes::UUID.new }
5
+
6
+ describe '#cast' do
7
+ subject { -> { type.cast(value) } }
8
+
9
+ context 'with nil' do
10
+ let(:value) { nil }
11
+ it { expect(subject.call).to eq nil }
49
12
  end
50
13
 
51
- context 'with non-String as param' do
52
- subject { -> { action } }
14
+ context 'with empty string' do
15
+ let(:value) { '' }
16
+ it { expect(subject.call).to eq nil }
17
+ end
18
+
19
+ context 'with blank string' do
20
+ let(:value) { " \t" }
21
+ it { expect(subject.call).to eq nil }
22
+ end
23
+
24
+ context 'with string UUID' do
25
+ let(:value) { '450b7a40-94ad-11e3-baa8-0800200c9a66' }
26
+ it { expect(subject.call).to be_a String }
27
+ it { expect(subject.call).to eq value }
28
+ end
29
+
30
+ context 'with invalid string' do
31
+ let(:value) { 'invalid string' }
32
+ it { is_expected.to raise_error TypeError, /invalid UUID/i }
33
+ end
53
34
 
54
- invalid_params = {fixnum: 1, float: 3.2, symbol: :invalid, boolean: true}
55
- invalid_params.each do |type, incorrect_param|
56
- context "with #{type} as param" do
57
- let(:param) { incorrect_param }
58
- it { should raise_error ArgumentError }
59
- end
60
- end
35
+ context 'with invalid UUID' do
36
+ let(:value) { 'xxxxxxxx-yyyy-11e3-baa8-0800200c9a66' }
37
+ it { is_expected.to raise_error TypeError, /invalid UUID/i }
61
38
  end
62
39
  end
63
40
  end
@@ -162,32 +162,6 @@ describe Acfs::Resource::Attributes do
162
162
 
163
163
  expect(model.attributes.symbolize_keys).to eq age: 12
164
164
  end
165
-
166
- context 'allow nil option' do
167
- it 'should allow nil as value' do
168
- model.send :attribute, :updated_at, :date_time,
169
- default: DateTime.new, allow_nil: true
170
-
171
- resource = model.new
172
- expect(resource.updated_at).to eq DateTime.new
173
-
174
- resource.updated_at = ''
175
- expect(resource.updated_at).to eq nil
176
- end
177
- end
178
-
179
- context 'allow blank option' do
180
- it 'should allow blank as value' do
181
- model.send :attribute, :updated_at, :date_time,
182
- default: DateTime.new, allow_blank: true
183
-
184
- resource = model.new
185
- expect(resource.updated_at).to eq DateTime.new
186
-
187
- resource.updated_at = ''
188
- expect(resource.updated_at).to eq nil
189
- end
190
- end
191
165
  end
192
166
  end
193
167
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.0
4
+ version: 0.46.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-25 00:00:00.000000000 Z
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -228,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
228
  version: '0'
229
229
  requirements: []
230
230
  rubyforge_project:
231
- rubygems_version: 2.5.1
231
+ rubygems_version: 2.6.4
232
232
  signing_key:
233
233
  specification_version: 4
234
234
  summary: An abstract API base client for service oriented application.