hash2obj 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hash2obj.rb +18 -5
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf96b8a0913d8c460be9dad0bef4af1a872ea641
|
4
|
+
data.tar.gz: 13406035c6efb29585251ca87ff17881c65fe5bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b26ed2f4c41b0854012e052dfb09d538aed259c1ac4e0e76178a4b37638345e948c7d167f768ddbc785eae85ddf02a884a40a81c9239cbcf984e7b76ee1ea81
|
7
|
+
data.tar.gz: dc9e77240fd79f6ef3d2b99154baa190d03b01778956e4f82c49c2bd7a8c22499f62b6c48213af6945041a47d767e4cfbe6039fae3e5f55c4c73077a7b7c4552
|
data/lib/hash2obj.rb
CHANGED
@@ -4,11 +4,20 @@ require 'ostruct'
|
|
4
4
|
module Hash2obj
|
5
5
|
def self.cast(hash, example)
|
6
6
|
klass = example.class
|
7
|
-
|
7
|
+
writable_attr = []
|
8
|
+
potential_keyrest_args = example.instance_variables.inject({}) { |r, s|
|
9
|
+
field = s.to_s.sub('@', '')
|
10
|
+
if hash.key? field
|
11
|
+
r.merge!({field.to_sym => hash[field.to_sym]})
|
12
|
+
elsif example.respond_to?(field)
|
13
|
+
r.merge!({field.to_sym => example.send(field)})
|
14
|
+
end
|
15
|
+
r
|
16
|
+
}
|
8
17
|
example.instance_variables.each do |attr|
|
9
18
|
attr = attr.to_s.sub('@', '').to_sym
|
10
19
|
if example.respond_to? ("#{attr}=")
|
11
|
-
|
20
|
+
writable_attr << attr
|
12
21
|
end
|
13
22
|
end
|
14
23
|
|
@@ -17,6 +26,7 @@ module Hash2obj
|
|
17
26
|
construct_params = example.method(:initialize).parameters
|
18
27
|
construct_params.each do |param|
|
19
28
|
arg_required, arg_name = param
|
29
|
+
potential_keyrest_args.delete "#{arg_name}".to_sym
|
20
30
|
|
21
31
|
case arg_required
|
22
32
|
when :req
|
@@ -32,7 +42,10 @@ module Hash2obj
|
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
35
|
-
|
45
|
+
key_args = potential_keyrest_args.merge(key_args)
|
46
|
+
|
47
|
+
args.map! { |val| hash[val] || example.send(val) }
|
48
|
+
|
36
49
|
if args.empty? and key_args.empty?
|
37
50
|
instance = klass.send(:new)
|
38
51
|
elsif args.empty?
|
@@ -43,8 +56,8 @@ module Hash2obj
|
|
43
56
|
instance = klass.send(:new, *args, **key_args)
|
44
57
|
end
|
45
58
|
|
46
|
-
|
47
|
-
instance.send("#{attr}=", hash[attr])
|
59
|
+
writable_attr.each do |attr|
|
60
|
+
instance.send("#{attr}=", hash[attr]) if hash.key? attr
|
48
61
|
end
|
49
62
|
|
50
63
|
instance
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash2obj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Han
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Apply a hash into a Ruby object and return another instance of the same
|
14
14
|
class using the data in the hash.
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
41
|
rubyforge_project:
|
42
|
-
rubygems_version: 2.5.1
|
42
|
+
rubygems_version: 2.4.5.1
|
43
43
|
signing_key:
|
44
44
|
specification_version: 4
|
45
45
|
summary: Convert a hash into a Ruby object
|