class_kit 0.2.2 → 0.2.3
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 +26 -11
- data/lib/class_kit/class_methods.rb +23 -26
- data/lib/class_kit/helper.rb +35 -45
- data/lib/class_kit/value_helper.rb +33 -37
- data/lib/class_kit/version.rb +1 -1
- metadata +21 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6175988d95d1b402e572de4e37188609b472faf
|
4
|
+
data.tar.gz: cb3d17f32c2d552fd0a75f82d6aa07c6767a4b03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63fba237db2c0dfdd7ee309696beb40d4715ce14b79ec3cac04eb8452ca779f759edbc689f3f297a2a13c874270218652b13a96b6d6aa798aab8f49c576ecb8b
|
7
|
+
data.tar.gz: e187ffdfdf9acc30fd4eec3ce2a983899c8ddc495e3eed2ec797e8fd0a040de4c53c3750acb9ded8906782fd655569b11adf5439c7c33c45a569fd435363fcde
|
@@ -1,20 +1,35 @@
|
|
1
1
|
module ClassKit
|
2
2
|
class AttributeHelper
|
3
|
+
# Get attributes for a given class
|
4
|
+
#
|
5
|
+
# @param klass [ClassKit] a class that has been extended with ClassKit
|
6
|
+
#
|
7
|
+
# @return [Hash]
|
3
8
|
def get_attributes(klass)
|
4
|
-
|
9
|
+
klass.class_variable_get(:@@class_kit_attributes).values.freeze
|
5
10
|
end
|
6
11
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
# Get attribute for a given class and name
|
13
|
+
#
|
14
|
+
# @param klass [ClassKit] a class that has been extended with ClassKit
|
15
|
+
# @param name [Symbol] an attribute name
|
16
|
+
#
|
17
|
+
# @raise [ClassKit::Exceptions::AttributeNotFoundError] if the given attribute could not be found
|
18
|
+
#
|
19
|
+
# @return [Hash] that describes the attribute
|
20
|
+
def get_attribute(klass:, name:)
|
21
|
+
klass.class_variable_get(:@@class_kit_attributes)[name] ||
|
22
|
+
raise(ClassKit::Exceptions::AttributeNotFoundError, "Attribute: #{name}, could not be found.")
|
13
23
|
end
|
14
24
|
|
15
|
-
|
16
|
-
|
25
|
+
# Get the type of a given attribute on a given class
|
26
|
+
#
|
27
|
+
# @param klass [ClassKit] a class that has been extended with ClassKit
|
28
|
+
# @param name [Symbol]
|
29
|
+
#
|
30
|
+
# @return [Class]
|
31
|
+
def get_attribute_type(klass:, name:)
|
32
|
+
get_attribute(klass: klass, name: name)[:type]
|
17
33
|
end
|
18
|
-
|
19
34
|
end
|
20
|
-
end
|
35
|
+
end
|
@@ -1,48 +1,46 @@
|
|
1
1
|
module ClassKit
|
2
|
-
def attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, default: nil, auto_init: false,
|
2
|
+
def attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, default: nil, auto_init: false,
|
3
|
+
meta: {})
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
unless class_variable_defined?(:@@class_kit_attributes)
|
6
|
+
class_variable_set(:@@class_kit_attributes, {})
|
6
7
|
end
|
7
8
|
|
8
|
-
|
9
|
-
self.class_variable_set(:@@is_class_kit_class, true)
|
10
|
-
end
|
9
|
+
attributes = class_variable_get(:@@class_kit_attributes)
|
11
10
|
|
12
|
-
attributes =
|
13
|
-
|
14
|
-
self.class_variable_set(:@@class_kit_attributes, attributes)
|
11
|
+
attributes[name] = { name: name, type: type, collection_type: collection_type, allow_nil: allow_nil,
|
12
|
+
default: default, auto_init: auto_init, meta: meta }
|
15
13
|
|
16
|
-
|
14
|
+
class_eval do
|
17
15
|
define_method name do
|
16
|
+
cka = self.class.class_variable_get(:@@class_kit_attributes)[name]
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
if eval("@#{name}") == nil && cka[:default] != nil
|
22
|
-
eval("@#{name}=cka[:default]")
|
23
|
-
end
|
18
|
+
current_value = instance_variable_get(:"@#{name}")
|
24
19
|
|
25
|
-
if
|
26
|
-
|
20
|
+
if current_value.nil?
|
21
|
+
if cka[:default] != nil
|
22
|
+
current_value = instance_variable_set(:"@#{name}", cka[:default])
|
23
|
+
elsif cka[:auto_init]
|
24
|
+
current_value = instance_variable_set(:"@#{name}", cka[:type].new)
|
25
|
+
end
|
27
26
|
end
|
28
27
|
|
29
|
-
|
28
|
+
current_value
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
|
-
|
32
|
+
class_eval do
|
34
33
|
define_method "#{name}=" do |value|
|
35
|
-
|
36
34
|
#get the attribute meta data
|
37
|
-
cka = self.class.class_variable_get(:@@class_kit_attributes)
|
35
|
+
cka = self.class.class_variable_get(:@@class_kit_attributes)[name]
|
38
36
|
|
39
37
|
#verify if the attribute is allowed to be set to nil
|
40
|
-
if value
|
38
|
+
if value.nil? && cka[:allow_nil] == false
|
41
39
|
raise ClassKit::Exceptions::InvalidAttributeValueError.new("Attribute: #{name}, must not be nil.")
|
42
40
|
end
|
43
41
|
|
44
42
|
#check if the value being set is not of the specified type and should attempt to parse the value
|
45
|
-
if cka[:type]
|
43
|
+
if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type]))
|
46
44
|
begin
|
47
45
|
value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)
|
48
46
|
rescue => e
|
@@ -50,9 +48,8 @@ module ClassKit
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
|
-
|
51
|
+
instance_variable_set(:"@#{name}", value)
|
54
52
|
end
|
55
53
|
end
|
56
|
-
|
57
54
|
end
|
58
|
-
end
|
55
|
+
end
|
data/lib/class_kit/helper.rb
CHANGED
@@ -9,73 +9,63 @@ module ClassKit
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def is_class_kit?(klass)
|
12
|
-
|
13
|
-
if !klass.class_variable_defined?(:@@is_class_kit_class)
|
14
|
-
return false
|
15
|
-
end
|
16
|
-
rescue
|
17
|
-
return false
|
18
|
-
end
|
19
|
-
|
20
|
-
return true
|
12
|
+
klass.is_a?(ClassKit)
|
21
13
|
end
|
22
14
|
|
23
15
|
def validate_class_kit(klass)
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
return true
|
16
|
+
is_class_kit?(klass) || raise(ClassKit::Exceptions::InvalidClassError,
|
17
|
+
"Class: #{klass} does not implement ClassKit.")
|
28
18
|
end
|
29
19
|
|
30
20
|
def to_hash(object)
|
31
21
|
validate_class_kit(object.class)
|
32
|
-
|
22
|
+
|
23
|
+
@hash_helper.to_hash(object)
|
33
24
|
end
|
34
25
|
|
35
|
-
def from_hash(hash
|
26
|
+
def from_hash(hash:, klass:)
|
36
27
|
validate_class_kit(klass)
|
28
|
+
|
37
29
|
hash = @hash_helper.symbolize(hash)
|
38
30
|
entity = klass.new
|
39
31
|
attributes = @attribute_helper.get_attributes(klass)
|
40
|
-
attributes.each do |
|
41
|
-
key =
|
42
|
-
type =
|
32
|
+
attributes.each do |attribute|
|
33
|
+
key = attribute[:name]
|
34
|
+
type = attribute[:type]
|
43
35
|
|
44
36
|
#if the hash value is nil skip it
|
45
|
-
if hash[key]
|
46
|
-
|
47
|
-
|
37
|
+
next if hash[key].nil?
|
38
|
+
|
39
|
+
value = if is_class_kit?(type)
|
40
|
+
from_hash(hash: hash[key], klass: type)
|
41
|
+
elsif type == Array
|
42
|
+
hash[key].map do |array_element|
|
43
|
+
if attribute[:collection_type].nil?
|
44
|
+
array_element
|
45
|
+
else
|
46
|
+
if is_class_kit?(attribute[:collection_type])
|
47
|
+
from_hash(hash: array_element, klass: attribute[:collection_type])
|
48
|
+
else
|
49
|
+
@value_helper.parse(type: attribute[:collection_type], value: array_element)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
else
|
54
|
+
hash[key]
|
55
|
+
end
|
48
56
|
|
49
|
-
|
50
|
-
value = from_hash(hash: hash[key], klass: type)
|
51
|
-
elsif type == Array
|
52
|
-
value = hash[key].map do |i|
|
53
|
-
if a[:collection_type] != nil
|
54
|
-
if is_class_kit?(a[:collection_type])
|
55
|
-
from_hash(hash: i, klass: a[:collection_type])
|
56
|
-
else
|
57
|
-
@value_helper.parse(type: a[:collection_type], value: i)
|
58
|
-
end
|
59
|
-
else
|
60
|
-
i
|
61
|
-
end
|
62
|
-
end
|
63
|
-
else
|
64
|
-
value = hash[key]
|
65
|
-
end
|
66
|
-
eval("entity.#{key}=value")
|
57
|
+
entity.public_send(:"#{key}=", value)
|
67
58
|
end
|
68
|
-
|
59
|
+
|
60
|
+
entity
|
69
61
|
end
|
70
62
|
|
71
63
|
def to_json(object)
|
72
64
|
@json_helper.to_json(object)
|
73
65
|
end
|
74
66
|
|
75
|
-
def from_json(json
|
76
|
-
hash
|
77
|
-
return from_hash(hash: hash, klass: klass)
|
67
|
+
def from_json(json:, klass:)
|
68
|
+
from_hash(hash: JSON.load(json), klass: klass)
|
78
69
|
end
|
79
|
-
|
80
70
|
end
|
81
|
-
end
|
71
|
+
end
|
@@ -2,76 +2,72 @@ module ClassKit
|
|
2
2
|
class ValueHelper
|
3
3
|
|
4
4
|
def self.instance
|
5
|
-
|
6
|
-
class_variable_set(:@@instance, ClassKit::ValueHelper.new)
|
7
|
-
end
|
8
|
-
return @@instance
|
5
|
+
@@instance ||= new
|
9
6
|
end
|
10
7
|
|
11
|
-
def parse(type
|
12
|
-
begin
|
8
|
+
def parse(type:, value:)
|
13
9
|
if type == Time
|
14
10
|
if value.is_a?(Time)
|
15
|
-
value
|
11
|
+
value
|
16
12
|
elsif value.is_a?(Integer) || value.is_a?(Float)
|
17
|
-
|
13
|
+
Time.at(value)
|
18
14
|
else
|
19
|
-
|
15
|
+
Time.parse(value)
|
20
16
|
end
|
21
17
|
elsif type == Date
|
22
18
|
if value.is_a?(Date)
|
23
|
-
value
|
19
|
+
value
|
24
20
|
elsif value.is_a?(Integer)
|
25
|
-
|
21
|
+
Date.at(value)
|
26
22
|
else
|
27
|
-
|
23
|
+
Date.parse(value)
|
28
24
|
end
|
29
25
|
elsif type == DateTime
|
30
26
|
if value.is_a?(DateTime)
|
31
|
-
value
|
27
|
+
value
|
32
28
|
elsif value.is_a?(Integer)
|
33
|
-
|
29
|
+
DateTime.at(value)
|
34
30
|
else
|
35
|
-
|
31
|
+
DateTime.parse(value)
|
36
32
|
end
|
37
33
|
elsif type == :bool
|
38
34
|
if value == true || value == false
|
39
|
-
value
|
35
|
+
value
|
40
36
|
elsif(/(true|t|yes|y|1)$/i === value.to_s.downcase)
|
41
|
-
|
37
|
+
true
|
42
38
|
elsif (/(false|f|no|n|0)$/i === value.to_s.downcase)
|
43
|
-
|
39
|
+
false
|
44
40
|
elsif value != nil
|
45
41
|
raise 'Unable to parse bool'
|
46
42
|
end
|
47
43
|
elsif type == Integer
|
48
|
-
|
44
|
+
Integer(value)
|
49
45
|
elsif type == Float
|
50
|
-
value = Float(value)
|
51
|
-
elsif type == BigDecimal
|
52
46
|
Float(value)
|
53
|
-
|
47
|
+
elsif type == BigDecimal
|
48
|
+
if value.is_a?(BigDecimal)
|
49
|
+
value
|
50
|
+
else
|
51
|
+
value = value.to_s
|
52
|
+
raise 'Unable to parse BigDecimal' unless value =~ /\A\d+(\.\d*)?/
|
53
|
+
BigDecimal(value)
|
54
|
+
end
|
54
55
|
elsif type == String
|
55
|
-
|
56
|
+
String(value)
|
56
57
|
elsif type == Regexp
|
57
|
-
|
58
|
+
Regexp.new(value)
|
58
59
|
elsif type == Hash
|
59
|
-
|
60
|
-
|
61
|
-
end
|
60
|
+
raise 'Unable to parse Hash' unless value.is_a?(Hash)
|
61
|
+
value
|
62
62
|
elsif type == Array
|
63
|
-
|
64
|
-
|
65
|
-
end
|
63
|
+
raise 'Unable to parse Array' unless value.is_a?(Array)
|
64
|
+
value
|
66
65
|
else
|
67
66
|
raise 'Unable to parse'
|
68
67
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
return value
|
68
|
+
rescue => e
|
69
|
+
raise ClassKit::Exceptions::InvalidParseValueError,
|
70
|
+
"Unable to parse value: #{value} into type: #{type}. Error: #{e}"
|
74
71
|
end
|
75
|
-
|
76
72
|
end
|
77
|
-
end
|
73
|
+
end
|
data/lib/class_kit/version.rb
CHANGED
metadata
CHANGED
@@ -1,111 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.11'
|
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
26
|
version: '1.11'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.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
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.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
54
|
version: '3.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: json
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: hash_kit
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: json_kit
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
description: Toolkit for working with classes
|
@@ -115,17 +115,17 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- lib/class_kit.rb
|
118
119
|
- lib/class_kit/attribute_helper.rb
|
119
120
|
- lib/class_kit/class_methods.rb
|
121
|
+
- lib/class_kit/exceptions.rb
|
120
122
|
- lib/class_kit/exceptions/attribute_not_found_error.rb
|
121
123
|
- lib/class_kit/exceptions/invalid_attribute_value_error.rb
|
122
124
|
- lib/class_kit/exceptions/invalid_class_error.rb
|
123
125
|
- lib/class_kit/exceptions/invalid_parse_value_error.rb
|
124
|
-
- lib/class_kit/exceptions.rb
|
125
126
|
- lib/class_kit/helper.rb
|
126
127
|
- lib/class_kit/value_helper.rb
|
127
128
|
- lib/class_kit/version.rb
|
128
|
-
- lib/class_kit.rb
|
129
129
|
homepage: https://github.com/vaughanbrittonsage/class_kit
|
130
130
|
licenses:
|
131
131
|
- MIT
|
@@ -136,19 +136,18 @@ require_paths:
|
|
136
136
|
- lib
|
137
137
|
required_ruby_version: !ruby/object:Gem::Requirement
|
138
138
|
requirements:
|
139
|
-
- -
|
139
|
+
- - ">="
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
143
|
requirements:
|
144
|
-
- -
|
144
|
+
- - ">="
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
148
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.
|
149
|
+
rubygems_version: 2.5.1
|
150
150
|
signing_key:
|
151
151
|
specification_version: 4
|
152
152
|
summary: Toolkit for working with classes
|
153
153
|
test_files: []
|
154
|
-
has_rdoc:
|