hash2obj 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/hash2obj.rb +52 -0
- data/lib/version.rb +4 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c4945db3c7dbed44b672a4094305e825c5fa70d9
|
4
|
+
data.tar.gz: a9a1ccdfe4ea6348cffe4878458aa379d03b7902
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f32bd19381dfc50b226e5d8eb5909bc2aee7ba988bcccf633383021b3a79b8c69312aabc778595002d65180f358d644040bfd97712b9afc90ecf4719eaf3252
|
7
|
+
data.tar.gz: b2b74ba55792cf6ec09f36dbd6db081748a4ed2cb1b704e8d946144d67519c7cbb11118c8b5e79f047c71694b5a13ccee6b7ae046c72187041b95605e72af2e9
|
data/lib/hash2obj.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# Hash2obj module
|
4
|
+
module Hash2obj
|
5
|
+
def self.cast(hash, example)
|
6
|
+
klass = example.class
|
7
|
+
accessible_attr = []
|
8
|
+
example.instance_variables.each do |attr|
|
9
|
+
attr = attr.to_s.sub('@', '').to_sym
|
10
|
+
if example.respond_to? ("#{attr}=")
|
11
|
+
accessible_attr << attr
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
key_args = {}
|
16
|
+
args = []
|
17
|
+
construct_params = example.method(:initialize).parameters
|
18
|
+
construct_params.each do |param|
|
19
|
+
arg_required, arg_name = param
|
20
|
+
|
21
|
+
case arg_required
|
22
|
+
when :req
|
23
|
+
args << arg_name
|
24
|
+
when :opt
|
25
|
+
args << arg_name if hash.key? arg_name
|
26
|
+
when :keyreq
|
27
|
+
raise "parameter #{arg_name} is mandatory but missing in #{hash}" unless hash.key? arg_name
|
28
|
+
key_args[arg_name] = hash[arg_name]
|
29
|
+
when :keyrest
|
30
|
+
key_args.merge! hash
|
31
|
+
break
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
args.map! { |val| hash[val] }
|
36
|
+
if args.empty? and key_args.empty?
|
37
|
+
instance = klass.send(:new)
|
38
|
+
elsif args.empty?
|
39
|
+
instance = klass.send(:new, key_args)
|
40
|
+
elsif key_args.empty?
|
41
|
+
instance = klass.send(:new, *args)
|
42
|
+
else
|
43
|
+
instance = klass.send(:new, *args, **key_args)
|
44
|
+
end
|
45
|
+
|
46
|
+
accessible_attr.each do |attr|
|
47
|
+
instance.send("#{attr}=", hash[attr])
|
48
|
+
end
|
49
|
+
|
50
|
+
instance
|
51
|
+
end
|
52
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash2obj
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Han
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Apply a hash into a Ruby object and return another instance of the same
|
14
|
+
class using the data in the hash.
|
15
|
+
email: hex0cter@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/hash2obj.rb
|
21
|
+
- lib/version.rb
|
22
|
+
homepage: https://github.com/hex0cter/hash2obj
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.1.0
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Convert a hash into a Ruby object
|
46
|
+
test_files: []
|