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 +4 -4
- data/.travis.yml +5 -2
- data/lib/shallow_attributes/type.rb +3 -1
- data/lib/shallow_attributes/type/date.rb +41 -0
- data/lib/shallow_attributes/type/date_time.rb +6 -2
- data/lib/shallow_attributes/type/float.rb +1 -1
- data/lib/shallow_attributes/type/integer.rb +1 -1
- data/lib/shallow_attributes/type/string.rb +2 -1
- data/lib/shallow_attributes/type/time.rb +6 -2
- data/lib/shallow_attributes/version.rb +1 -1
- 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: 66b9498568bc80ce5c41a20d54ded1f7b1fefbdc7f70fab1b8ac9c019f5e8c8b
|
4
|
+
data.tar.gz: 35dcaaa5383491b2c147fd602ea4bd53d380d178e8462a09bce759ed64c13d46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 284908aeee2af16130a7a3ab4a09c2e8d74e23c67389de1cffd6c3226089ea35698250fefbc8183a7d6e63a70b2a48b56e96ecd478b9bebe6455c5a6af5d848f
|
7
|
+
data.tar.gz: 1af071876c23165a2548b68610a93b3312e512c814b02489037e5f50084e9113f28a6e65eb98ef1d905c368e7cd3cc3107e3d25d631cb61a0b5239a9119f1949
|
data/.travis.yml
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
language: ruby
|
2
2
|
sudo: false
|
3
3
|
cache: bundler
|
4
|
-
before_install:
|
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,
|
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
|
-
|
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
|
@@ -20,8 +20,9 @@ module ShallowAttributes
|
|
20
20
|
# @return [Sting]
|
21
21
|
#
|
22
22
|
# @since 0.1.0
|
23
|
-
def coerce(value,
|
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,
|
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
|
-
|
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
|
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
|
+
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:
|
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
|