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