shallow_attributes 0.9.4 → 0.9.5

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: 6ce8dffffc3a488bf4a14e990dd56c694c91586b2adb7f7c5a8ffbc081247434
4
- data.tar.gz: 17ad85260516b5ed8fde0b05b1bec9df67976228be3620114d757e9c31c94fdd
3
+ metadata.gz: 66b9498568bc80ce5c41a20d54ded1f7b1fefbdc7f70fab1b8ac9c019f5e8c8b
4
+ data.tar.gz: 35dcaaa5383491b2c147fd602ea4bd53d380d178e8462a09bce759ed64c13d46
5
5
  SHA512:
6
- metadata.gz: 230ee60533f000e733c057d2aaeee7b8a8de29b8208acb26903189fb09ceef28b0a85ed9799cfa737bd522741fd15fa9ea27ecb010464f0286d6266f7d016e0c
7
- data.tar.gz: b2c91b2a052da58ca1eba5c6c679336b078da85430a93f4d0da2e8100335aeaa6875ead0c3c080000d5a8dae0d86c292ada6c1715fbae631654d13819cbffead
6
+ metadata.gz: 284908aeee2af16130a7a3ab4a09c2e8d74e23c67389de1cffd6c3226089ea35698250fefbc8183a7d6e63a70b2a48b56e96ecd478b9bebe6455c5a6af5d848f
7
+ data.tar.gz: 1af071876c23165a2548b68610a93b3312e512c814b02489037e5f50084e9113f28a6e65eb98ef1d905c368e7cd3cc3107e3d25d631cb61a0b5239a9119f1949
@@ -1,11 +1,14 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  cache: bundler
4
- before_install: gem install bundler -v 1.11.2
4
+ before_install:
5
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
6
+ - gem install bundler -v '< 2'
5
7
  rvm:
6
- - 2.2.5
7
8
  - 2.3.1
8
9
  - 2.4.0
10
+ - 2.5.0
11
+ - 2.6.0
9
12
  - jruby-head
10
13
  - rbx-2
11
14
  - ruby-head
@@ -5,6 +5,7 @@ require 'shallow_attributes/type/float'
5
5
  require 'shallow_attributes/type/integer'
6
6
  require 'shallow_attributes/type/string'
7
7
  require 'shallow_attributes/type/time'
8
+ require 'shallow_attributes/type/date'
8
9
 
9
10
  module ShallowAttributes
10
11
  # Namespace for standard type classes
@@ -28,7 +29,8 @@ module ShallowAttributes
28
29
  ::Float => ShallowAttributes::Type::Float.new,
29
30
  ::Integer => ShallowAttributes::Type::Integer.new,
30
31
  ::String => ShallowAttributes::Type::String.new,
31
- ::Time => ShallowAttributes::Type::Time.new
32
+ ::Time => ShallowAttributes::Type::Time.new,
33
+ ::Date => ShallowAttributes::Type::Date.new
32
34
  }.freeze
33
35
 
34
36
  class << self
@@ -0,0 +1,41 @@
1
+ module ShallowAttributes
2
+ module Type
3
+ # Abstract class for typecast object to Date type.
4
+ #
5
+ # @abstract
6
+ #
7
+ # @since 0.1.0
8
+ class Date
9
+ # Convert value to Date type
10
+ #
11
+ # @private
12
+ #
13
+ # @param [Object] value
14
+ # @param [Hash] _options
15
+ #
16
+ # @example Convert string to Date value
17
+ # ShallowAttributes::Type::Date.new.coerce('Thu Nov 29 2001')
18
+ # # => #<Date: 2001-11-29 ((2452243j,0s,0n),+0s,2299161j)>
19
+ #
20
+ # @raise [InvalidValueError] if value is not a string
21
+ #
22
+ # @return [Date]
23
+ #
24
+ # @since 0.1.0
25
+ def coerce(value, options = {})
26
+ case value
27
+ when ::DateTime, ::Time then value.to_date
28
+ when ::Date then value
29
+ else
30
+ ::Date.parse(value.to_s)
31
+ end
32
+ rescue
33
+ if options.fetch(:strict, true)
34
+ raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Date")
35
+ else
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -24,7 +24,7 @@ module ShallowAttributes
24
24
  # @return [DateTime]
25
25
  #
26
26
  # @since 0.1.0
27
- def coerce(value, _options = {})
27
+ def coerce(value, options = {})
28
28
  case value
29
29
  when ::DateTime then value
30
30
  when ::Time then ::DateTime.parse(value.to_s)
@@ -32,7 +32,11 @@ module ShallowAttributes
32
32
  ::DateTime.parse(value)
33
33
  end
34
34
  rescue
35
- raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "DateTime")
35
+ if options.fetch(:strict, true)
36
+ raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "DateTime")
37
+ else
38
+ nil
39
+ end
36
40
  end
37
41
  end
38
42
  end
@@ -25,7 +25,7 @@ module ShallowAttributes
25
25
  # @since 0.1.0
26
26
  def coerce(value, options = {})
27
27
  case value
28
- when nil then options[:allow_nil] ? 0.0 : nil
28
+ when nil then options[:allow_nil] ? nil : 0.0
29
29
  when ::TrueClass then 1.0
30
30
  when ::FalseClass then 0.0
31
31
  else
@@ -25,7 +25,7 @@ module ShallowAttributes
25
25
  # @since 0.1.0
26
26
  def coerce(value, options = {})
27
27
  case value
28
- when nil then options[:allow_nil] ? 0 : nil
28
+ when nil then options[:allow_nil] ? nil : 0
29
29
  when ::TrueClass then 1
30
30
  when ::FalseClass then 0
31
31
  else
@@ -20,8 +20,9 @@ module ShallowAttributes
20
20
  # @return [Sting]
21
21
  #
22
22
  # @since 0.1.0
23
- def coerce(value, _options = {})
23
+ def coerce(value, options = {})
24
24
  case value
25
+ when nil then options[:allow_nil] ? nil : ''
25
26
  when ::Array then value.join
26
27
  when ::Hash, ::Class then error(value)
27
28
  else
@@ -22,7 +22,7 @@ module ShallowAttributes
22
22
  # @return [Time]
23
23
  #
24
24
  # @since 0.1.0
25
- def coerce(value, _options = {})
25
+ def coerce(value, options = {})
26
26
  case value
27
27
  when ::Time then value
28
28
  when ::Integer then ::Time.at(value)
@@ -30,7 +30,11 @@ module ShallowAttributes
30
30
  ::Time.parse(value.to_s)
31
31
  end
32
32
  rescue
33
- raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Time")
33
+ if options.fetch(:strict, true)
34
+ raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Time")
35
+ else
36
+ nil
37
+ end
34
38
  end
35
39
  end
36
40
  end
@@ -2,5 +2,5 @@ module ShallowAttributes
2
2
  # Defines the full version
3
3
  #
4
4
  # @since 0.1.0
5
- VERSION = "0.9.4".freeze
5
+ VERSION = "0.9.5".freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shallow_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Davydov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-12 00:00:00.000000000 Z
11
+ date: 2019-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - lib/shallow_attributes/type.rb
89
89
  - lib/shallow_attributes/type/array.rb
90
90
  - lib/shallow_attributes/type/boolean.rb
91
+ - lib/shallow_attributes/type/date.rb
91
92
  - lib/shallow_attributes/type/date_time.rb
92
93
  - lib/shallow_attributes/type/float.rb
93
94
  - lib/shallow_attributes/type/integer.rb