class_kit 0.5.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/class_kit/attribute_helper.rb +10 -1
- data/lib/class_kit/custom_type.rb +22 -0
- data/lib/class_kit/helper.rb +16 -0
- data/lib/class_kit/value_helper.rb +3 -5
- data/lib/class_kit/version.rb +5 -0
- data/lib/class_kit.rb +2 -0
- metadata +32 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f501847a8ffea1775ff197945234f28865db2ba0188b35d469b9e2968f00d2f
|
4
|
+
data.tar.gz: 37c4acff0fc0114dc9cc4e4fe9c2c41acc294fa4d54e3e53e99149ac0afb17ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77507663db94f9e8d8da1a77bde21ef2000f40124a3a776de446715a133b944aa132fe71cf68dd7197b1434a6a70b0817dfcb455b4a2808d57f3878ad4472d59
|
7
|
+
data.tar.gz: b2f047deffc4c43fb07b09b6ba8d9a7d9d732c1f0b5ed5192af614182e1bf6703fb72bcd3b0fb4a4dee010db515bc7f700f0b4c09cdf3b03b7cd43e297b000fe
|
@@ -5,12 +5,18 @@ module ClassKit
|
|
5
5
|
@instance ||= ClassKit::AttributeHelper.new
|
6
6
|
end
|
7
7
|
|
8
|
+
def initialize
|
9
|
+
@attribute_store = {}
|
10
|
+
end
|
11
|
+
|
8
12
|
# Get attributes for a given class
|
9
13
|
#
|
10
14
|
# @param klass [ClassKit] a class that has been extended with ClassKit
|
11
15
|
#
|
12
16
|
# @return [Hash]
|
13
17
|
def get_attributes(klass)
|
18
|
+
return @attribute_store[klass] if @attribute_store.key?(klass)
|
19
|
+
|
14
20
|
attributes = []
|
15
21
|
klass.ancestors.map do |k|
|
16
22
|
hash = k.instance_variable_get(:@class_kit_attributes)
|
@@ -20,7 +26,10 @@ module ClassKit
|
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
23
|
-
attributes.compact
|
29
|
+
attributes.compact!
|
30
|
+
|
31
|
+
@attribute_store[klass] = attributes
|
32
|
+
attributes
|
24
33
|
end
|
25
34
|
|
26
35
|
# Get attribute for a given class and name
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ClassKit
|
2
|
+
module CustomType
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(CustomType)
|
5
|
+
end
|
6
|
+
|
7
|
+
# This method must return the parsed value when +from_hash+ is called
|
8
|
+
def self.parse_from_hash(_value)
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
# This method must return the parsed value when the class' attribute is assigned a value
|
13
|
+
def self.parse_assign(_value)
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
# This method must return the value that will be serialised for the attribute
|
18
|
+
def to_hash_value
|
19
|
+
raise NotImplementedError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/class_kit/helper.rb
CHANGED
@@ -11,6 +11,10 @@ module ClassKit
|
|
11
11
|
klass.is_a?(ClassKit)
|
12
12
|
end
|
13
13
|
|
14
|
+
def is_class_kit_custom_type?(klass)
|
15
|
+
!klass.nil? && klass != :bool && klass.include?(ClassKit::CustomType)
|
16
|
+
end
|
17
|
+
|
14
18
|
def validate_class_kit(klass)
|
15
19
|
is_class_kit?(klass) || raise(ClassKit::Exceptions::InvalidClassError,
|
16
20
|
"Class: #{klass} does not implement ClassKit.")
|
@@ -18,6 +22,8 @@ module ClassKit
|
|
18
22
|
|
19
23
|
# This method is called to convert a ClassKit object into a Hash.
|
20
24
|
def to_hash(object, use_alias = false)
|
25
|
+
return object.map { |i| to_hash(i, use_alias) } if object.is_a?(Array)
|
26
|
+
|
21
27
|
validate_class_kit(object.class)
|
22
28
|
|
23
29
|
hash = {}
|
@@ -30,10 +36,14 @@ module ClassKit
|
|
30
36
|
if value != nil
|
31
37
|
hash[key] = if is_class_kit?(type)
|
32
38
|
to_hash(value, use_alias)
|
39
|
+
elsif is_class_kit_custom_type?(type)
|
40
|
+
value.to_hash_value
|
33
41
|
elsif type == Array
|
34
42
|
value.map do |i|
|
35
43
|
if is_class_kit?(i.class)
|
36
44
|
to_hash(i, use_alias)
|
45
|
+
elsif is_class_kit_custom_type?(i.class)
|
46
|
+
i.to_hash_value
|
37
47
|
else
|
38
48
|
i
|
39
49
|
end
|
@@ -51,6 +61,8 @@ module ClassKit
|
|
51
61
|
def from_hash(hash:, klass:, use_alias: false)
|
52
62
|
validate_class_kit(klass)
|
53
63
|
|
64
|
+
return hash.map { |i| from_hash(hash: i, klass: klass, use_alias: use_alias) } if hash.is_a?(Array)
|
65
|
+
|
54
66
|
@hash_helper.indifferent!(hash)
|
55
67
|
entity = klass.new
|
56
68
|
attributes = @attribute_helper.get_attributes(klass)
|
@@ -63,6 +75,8 @@ module ClassKit
|
|
63
75
|
|
64
76
|
value = if is_class_kit?(type)
|
65
77
|
from_hash(hash: hash[key], klass: type, use_alias: use_alias)
|
78
|
+
elsif is_class_kit_custom_type?(type)
|
79
|
+
type.parse_from_hash(hash[key])
|
66
80
|
elsif type == Array
|
67
81
|
hash[key].map do |array_element|
|
68
82
|
if attribute[:collection_type].nil?
|
@@ -70,6 +84,8 @@ module ClassKit
|
|
70
84
|
else
|
71
85
|
if is_class_kit?(attribute[:collection_type])
|
72
86
|
from_hash(hash: array_element, klass: attribute[:collection_type], use_alias: use_alias)
|
87
|
+
elsif is_class_kit_custom_type?(attribute[:collection_type])
|
88
|
+
attribute[:collection_type].parse_from_hash(array_element)
|
73
89
|
else
|
74
90
|
@value_helper.parse(type: attribute[:collection_type], value: array_element)
|
75
91
|
end
|
@@ -9,7 +9,7 @@ module ClassKit
|
|
9
9
|
if type == Time
|
10
10
|
if value.is_a?(Time)
|
11
11
|
value
|
12
|
-
elsif value.is_a?(Integer) || value.is_a?(Float)
|
12
|
+
elsif value.is_a?(Integer) || value.is_a?(Float) || value.is_a?(BigDecimal)
|
13
13
|
Time.at(value)
|
14
14
|
else
|
15
15
|
Time.parse(value)
|
@@ -17,16 +17,12 @@ module ClassKit
|
|
17
17
|
elsif type == Date
|
18
18
|
if value.is_a?(Date)
|
19
19
|
value
|
20
|
-
elsif value.is_a?(Integer)
|
21
|
-
Date.at(value)
|
22
20
|
else
|
23
21
|
Date.parse(value)
|
24
22
|
end
|
25
23
|
elsif type == DateTime
|
26
24
|
if value.is_a?(DateTime)
|
27
25
|
value
|
28
|
-
elsif value.is_a?(Integer)
|
29
|
-
DateTime.at(value)
|
30
26
|
else
|
31
27
|
DateTime.parse(value)
|
32
28
|
end
|
@@ -62,6 +58,8 @@ module ClassKit
|
|
62
58
|
elsif type == Array
|
63
59
|
raise 'Unable to parse Array' unless value.is_a?(Array)
|
64
60
|
value
|
61
|
+
elsif type.include?(ClassKit::CustomType)
|
62
|
+
type.parse_assign(value)
|
65
63
|
else
|
66
64
|
raise 'Unable to parse'
|
67
65
|
end
|
data/lib/class_kit.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require_relative 'class_kit/class_methods'
|
2
2
|
require_relative 'class_kit/exceptions'
|
3
3
|
require_relative 'class_kit/attribute_helper'
|
4
|
+
require_relative 'class_kit/custom_type'
|
4
5
|
require_relative 'class_kit/value_helper'
|
5
6
|
require_relative 'class_kit/helper'
|
7
|
+
require_relative 'class_kit/version'
|
6
8
|
require 'hash_kit'
|
7
9
|
require 'json'
|
8
10
|
require 'date'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sage One
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,44 +16,44 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: pry
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,21 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "<"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.18.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "<"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.18.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: hash_kit
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
@@ -81,7 +95,7 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: json
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -104,6 +118,7 @@ files:
|
|
104
118
|
- lib/class_kit.rb
|
105
119
|
- lib/class_kit/attribute_helper.rb
|
106
120
|
- lib/class_kit/class_methods.rb
|
121
|
+
- lib/class_kit/custom_type.rb
|
107
122
|
- lib/class_kit/exceptions.rb
|
108
123
|
- lib/class_kit/exceptions/attribute_not_found_error.rb
|
109
124
|
- lib/class_kit/exceptions/invalid_attribute_value_error.rb
|
@@ -111,6 +126,7 @@ files:
|
|
111
126
|
- lib/class_kit/exceptions/invalid_parse_value_error.rb
|
112
127
|
- lib/class_kit/helper.rb
|
113
128
|
- lib/class_kit/value_helper.rb
|
129
|
+
- lib/class_kit/version.rb
|
114
130
|
homepage: https://github.com/sage/class_kit
|
115
131
|
licenses:
|
116
132
|
- MIT
|
@@ -130,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
146
|
- !ruby/object:Gem::Version
|
131
147
|
version: '0'
|
132
148
|
requirements: []
|
133
|
-
|
134
|
-
rubygems_version: 2.7.6
|
149
|
+
rubygems_version: 3.1.2
|
135
150
|
signing_key:
|
136
151
|
specification_version: 4
|
137
152
|
summary: Toolkit for working with classes
|