class_kit 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c098100e74808a81385dc01f687de12ed23d536
4
- data.tar.gz: 6ce23029485fa7381189d86477c9d0bd37f8ff36
3
+ metadata.gz: 9b9871d9c695b48328a3cfb824cd00f067b8bc22
4
+ data.tar.gz: 92960a8b8122c87ad1396f56e4c533d075f7f9cb
5
5
  SHA512:
6
- metadata.gz: af363a65e1e3b5d8278468ae08f232c14b4c4ba7e5d9ee171bbf3629c9261a1828325b78344eca4c281f84518841455f5ffc64351ba5884937f6cd4ccda380b4
7
- data.tar.gz: ff2db177937e4f8f2598a011c4bd8d8a793fb30e67d12852198d2c1ea1a5dd2617362b1c3a49647e93258e3988a47845ccebf48a9feee396ac96a09eda94718c
6
+ metadata.gz: 6d3d6abba9ec05a35da1cd4d8406727a56948e85fd6edba8b3b6a8318488c078c23283211634a3f63dc5d6aca672a6bd6aca55bbfc427b6c18f1420697877af4
7
+ data.tar.gz: f3731f6ea0744eb62715dc2bc5e3fd560c1201a9617b096ee295f17de3fc4a3993860532958d862344f7a0fac50384f672ed3126a5e558865e60c875a05a260f
@@ -44,49 +44,7 @@ module ClassKit
44
44
  #check if the value being set is not of the specified type and should attempt to parse the value
45
45
  if cka[:type] != nil && value != nil && (cka[:type] == :bool || !value.is_a?(cka[:type]))
46
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
47
+ value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)
90
48
  rescue => e
91
49
  raise ClassKit::Exceptions::InvalidAttributeValueError.new("Attribute: #{name}, must be of type: #{cka[:type]}. Error: #{e}")
92
50
  end
@@ -3,6 +3,7 @@ module ClassKit
3
3
 
4
4
  def initialize
5
5
  @hash_helper = HashKit::Helper.new
6
+ @json_helper = JsonKit::Helper.new
6
7
  @attribute_helper = ClassKit::AttributeHelper.new
7
8
  @value_helper = ClassKit::ValueHelper.new
8
9
  end
@@ -28,17 +29,19 @@ module ClassKit
28
29
 
29
30
  def to_hash(object)
30
31
  validate_class_kit(object.class)
31
- @hash_helper.to_hash(object)
32
+ return @hash_helper.to_hash(object)
32
33
  end
33
34
 
34
35
  def from_hash(hash:, klass:)
35
36
  validate_class_kit(klass)
37
+ hash = @hash_helper.symbolize(hash)
36
38
  entity = klass.new
37
39
  attributes = @attribute_helper.get_attributes(klass)
38
40
  attributes.each do |a|
39
41
  key = a[:name]
40
42
  type = a[:type]
41
43
 
44
+ #if the hash value is nil skip it
42
45
  if hash[key] == nil
43
46
  next
44
47
  end
@@ -65,5 +68,14 @@ module ClassKit
65
68
  return entity
66
69
  end
67
70
 
71
+ def to_json(object)
72
+ @json_helper.to_json(object)
73
+ end
74
+
75
+ def from_json(json:, klass:)
76
+ hash = JSON.load(json)
77
+ return from_hash(hash: hash, klass: klass)
78
+ end
79
+
68
80
  end
69
81
  end
@@ -1,5 +1,13 @@
1
1
  module ClassKit
2
2
  class ValueHelper
3
+
4
+ def self.instance
5
+ if !class_variable_defined?(:@@instance)
6
+ class_variable_set(:@@instance, ClassKit::ValueHelper.new)
7
+ end
8
+ return @@instance
9
+ end
10
+
3
11
  def parse(type: type, value: value)
4
12
  begin
5
13
  if type == Time
@@ -40,14 +48,21 @@ module ClassKit
40
48
  value = Integer(value)
41
49
  elsif type == Float
42
50
  value = Float(value)
51
+ elsif type == BigDecimal
52
+ Float(value)
53
+ value = BigDecimal.new(value.to_s)
43
54
  elsif type == String
44
55
  value = String(value)
56
+ elsif type == Regexp
57
+ value = Regexp.new(value)
45
58
  elsif type == Hash
46
- raise 'Unable to parse Hash'
59
+ if !value.is_a?(Hash)
60
+ raise 'Unable to parse Hash'
61
+ end
47
62
  elsif type == Array
48
- raise 'Unable to parse Array'
49
- elsif type == Hash
50
- raise 'Unable to parse Hash'
63
+ if !value.is_a?(Array)
64
+ raise 'Unable to parse Array'
65
+ end
51
66
  else
52
67
  raise 'Unable to parse'
53
68
  end
@@ -1,3 +1,3 @@
1
1
  module ClassKit
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/class_kit.rb CHANGED
@@ -7,4 +7,5 @@ require_relative 'class_kit/helper'
7
7
  require 'hash_kit'
8
8
  require 'json'
9
9
  require 'date'
10
+ require 'bigdecimal'
10
11
 
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: class_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-14 00:00:00.000000000 Z
11
+ date: 2016-08-15 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
@@ -84,28 +84,28 @@ dependencies:
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.0.14.1
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: