hash2obj 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hash2obj.rb +18 -5
  3. data/lib/version.rb +1 -1
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4945db3c7dbed44b672a4094305e825c5fa70d9
4
- data.tar.gz: a9a1ccdfe4ea6348cffe4878458aa379d03b7902
3
+ metadata.gz: bf96b8a0913d8c460be9dad0bef4af1a872ea641
4
+ data.tar.gz: 13406035c6efb29585251ca87ff17881c65fe5bb
5
5
  SHA512:
6
- metadata.gz: 8f32bd19381dfc50b226e5d8eb5909bc2aee7ba988bcccf633383021b3a79b8c69312aabc778595002d65180f358d644040bfd97712b9afc90ecf4719eaf3252
7
- data.tar.gz: b2b74ba55792cf6ec09f36dbd6db081748a4ed2cb1b704e8d946144d67519c7cbb11118c8b5e79f047c71694b5a13ccee6b7ae046c72187041b95605e72af2e9
6
+ metadata.gz: 3b26ed2f4c41b0854012e052dfb09d538aed259c1ac4e0e76178a4b37638345e948c7d167f768ddbc785eae85ddf02a884a40a81c9239cbcf984e7b76ee1ea81
7
+ data.tar.gz: dc9e77240fd79f6ef3d2b99154baa190d03b01778956e4f82c49c2bd7a8c22499f62b6c48213af6945041a47d767e4cfbe6039fae3e5f55c4c73077a7b7c4552
@@ -4,11 +4,20 @@ require 'ostruct'
4
4
  module Hash2obj
5
5
  def self.cast(hash, example)
6
6
  klass = example.class
7
- accessible_attr = []
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
- accessible_attr << attr
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
- args.map! { |val| hash[val] }
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
- accessible_attr.each do |attr|
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
@@ -1,4 +1,4 @@
1
1
  # Hash2obj gem version
2
2
  module Hash2Obj
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
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.1
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-06-14 00:00:00.000000000 Z
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