json_path_attribute 0.1.2 → 0.1.3
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 +4 -4
- data/lib/json_path_attribute/type.rb +24 -17
- data/lib/json_path_attribute/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0fef25008872124e17037318f1374dbfaa0d34ad4ca487e69efc21193b9cdcac
|
|
4
|
+
data.tar.gz: 21dae1e8c01daf1381909367afd1f4d67e66ce91545b1b14e7a1ae3ab11764c0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 60362ab86bf9f2de4d4d97d92b8fe1f52b8f6535f66707c4948367dda38b7722e478e958d10346ffc4016498a9415625ea6494324157b0c80229dbcb00be7138
|
|
7
|
+
data.tar.gz: 95ca64559409b2edce350f534fbedd69319ce75e838895501eea3408f42f0fd6fb64cb939718b31fc72d8ead2f231459d8c7b6a5168d57b011670886fe6d41d1
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "active_model"
|
|
4
|
+
|
|
3
5
|
module JsonPathAttribute
|
|
4
6
|
# Provides a way to cast values to a specific type
|
|
5
7
|
# This class is intended to be used internally by JsonPathAttribute
|
|
@@ -7,31 +9,36 @@ module JsonPathAttribute
|
|
|
7
9
|
class << self
|
|
8
10
|
def cast_attribute(type, value, array: false)
|
|
9
11
|
return value if type == :source
|
|
12
|
+
return cast_object_attribute(type, value, array: array) if type.is_a?(Class)
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
return [] if array
|
|
13
|
-
return false if type == :boolean
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
array ? cast_array(type, value) : cast_value(type, value, array)
|
|
14
|
+
cast_to(type, value, array: array)
|
|
17
15
|
end
|
|
18
16
|
|
|
19
|
-
def
|
|
20
|
-
return
|
|
21
|
-
return false if type == :boolean
|
|
17
|
+
def cast_to(type, value, array: false)
|
|
18
|
+
return [] if value.nil? && array
|
|
19
|
+
return false if value.nil? && type == :boolean
|
|
20
|
+
|
|
21
|
+
cast_type = ActiveModel::Type.lookup(type)
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
value.to_s
|
|
26
|
-
when :integer
|
|
27
|
-
value.to_i
|
|
23
|
+
if array
|
|
24
|
+
cast_array(value, type, cast_type)
|
|
28
25
|
else
|
|
29
|
-
|
|
26
|
+
cast_value(value, type, cast_type)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cast_array(values, type, cast_type)
|
|
31
|
+
values.map do |value|
|
|
32
|
+
next false if value.nil? && type == :boolean
|
|
33
|
+
|
|
34
|
+
cast_type.cast(value)
|
|
30
35
|
end
|
|
31
36
|
end
|
|
32
37
|
|
|
33
|
-
def
|
|
34
|
-
|
|
38
|
+
def cast_value(value, type, cast_type)
|
|
39
|
+
value = value.to_s if type == :decimal && value.is_a?(Float)
|
|
40
|
+
|
|
41
|
+
cast_type.cast(value)
|
|
35
42
|
end
|
|
36
43
|
|
|
37
44
|
def cast_object_attribute(type, value, array:)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json_path_attribute
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniël de Vries
|
|
@@ -11,6 +11,20 @@ bindir: exe
|
|
|
11
11
|
cert_chain: []
|
|
12
12
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: activemodel
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - "~>"
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '7.2'
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "~>"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '7.2'
|
|
14
28
|
- !ruby/object:Gem::Dependency
|
|
15
29
|
name: activesupport
|
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|