json_path_attribute 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2b74d1b64eefe49f524f7204997ba6879177092ecaa50dd1f0a39ee1a5e1f9c
4
- data.tar.gz: 5d27f669c336f92ff6144cd863175bbbfe139cf3c738a2fb2778157a48deebf0
3
+ metadata.gz: 7cbdb6b78f02c0b74ea6c83f79043cd413406b0132b607039d83983f79f40ac4
4
+ data.tar.gz: 7bf941b5f2e227d2238338ef631c1db0b69beb1ac859a78d46ae3c96348fd827
5
5
  SHA512:
6
- metadata.gz: 807da0b92649d4274798f2ea5d651e62533ef3e2b97866f5c3630ec9c13f4751ab9f8c5f2bc896f39761a72ab906ff34872880684b3000a7562dfe5024f52d62
7
- data.tar.gz: 7c1753866d8d7583098d3c1a65a3ea2e5c2d36641da3864d1ce9c0382f6af2a0bbc0c51d2675675dec6d768254f05c587513fff191b0ef199992518e801f05cf
6
+ metadata.gz: 513cefe620c4cacda89b8428455e6f0b51f0d088bbada1ed5de20d0075b0bf3291c1a7e178cdfb8c220930ce2eb0902e613e02a7ca49ee58f413c88b57535930
7
+ data.tar.gz: d53a201625ef2a294b61d72d3bef765bafc6be5b9e31b63580dc412575b68ebd4deb09a527f6b316274c297f46dd1572065dd1cc70ece567d048110e000536d2
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
-
5
3
  module JsonPathAttribute
6
4
  # Provides a way to cast values to a specific type
7
5
  # This class is intended to be used internally by JsonPathAttribute
@@ -9,36 +7,31 @@ module JsonPathAttribute
9
7
  class << self
10
8
  def cast_attribute(type, value, array: false)
11
9
  return value if type == :source
12
- return cast_object_attribute(type, value, array: array) if type.is_a?(Class)
13
-
14
- cast_to(type, value, array: array)
15
- end
16
-
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
10
 
23
- if array
24
- cast_array(value, type, cast_type)
25
- else
26
- cast_value(value, type, cast_type)
11
+ if value.nil?
12
+ return [] if array
13
+ return false if type == :boolean
27
14
  end
15
+
16
+ array ? cast_array(type, value) : cast_value(type, value, array)
28
17
  end
29
18
 
30
- def cast_array(values, type, cast_type)
31
- values.map do |value|
32
- next false if value.nil? && type == :boolean
19
+ def cast_value(type, value, array)
20
+ return cast_object_attribute(type, value, array: array) if type.is_a?(Class)
21
+ return false if type == :boolean && value.nil?
33
22
 
34
- cast_type.cast(value)
23
+ case type
24
+ when :string, :decimal
25
+ value.to_s
26
+ when :integer
27
+ value.to_i
28
+ else
29
+ raise TypeError, "Unable to cast #{value.inspect} to #{type}"
35
30
  end
36
31
  end
37
32
 
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)
33
+ def cast_array(type, values)
34
+ values.map { |element| cast_value(type, element, false) }
42
35
  end
43
36
 
44
37
  def cast_object_attribute(type, value, array:)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonPathAttribute
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniël de Vries
@@ -11,20 +11,6 @@ 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'
28
14
  - !ruby/object:Gem::Dependency
29
15
  name: activesupport
30
16
  requirement: !ruby/object:Gem::Requirement