clavatar 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/clavatar.rb +26 -11
  3. data/lib/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd9039cd2b20396bdc527d9f3ae3ac30ad379966
4
- data.tar.gz: 341b95214049b68a3f819a6f7f8113ec824aded6
3
+ metadata.gz: 8fbb3f61f81e4c9cc49182ca5ac6f2b6680cf005
4
+ data.tar.gz: 7c1c21fc5bd31874c002a8a8303b567b304e2fba
5
5
  SHA512:
6
- metadata.gz: 286a0ab327c6450d5fa4dd1a23b3e7d0a3bf2276553ae2bda807de241de0f7194e3e0f47a8af20d92de76e1deea0cc7e83907fa93c8f61578ac91db0764cdfce
7
- data.tar.gz: bff9c0b8830f6b6c7d5a9285ee9e4be798420867abc685df5b9c1ea51348f643cd32ec639ff34af3a62f9b78a94abc1269c88fe8b0ca10f99ac81d6507ea9324
6
+ metadata.gz: 4354d16304659fff8bff453692ea09f08393191aa465bdbeb3e0bb0f0f2238ca9766f5861d74596b42a349a423d63860b6017aaebf942c1fbcd9c0ced0def301
7
+ data.tar.gz: 9cf359755e58ec5c95273501396b8076a912cd68d18966452b34efe1cbb980723b46f43b03be7f8f20ac3281bfa6487fa623f590457ad1947efb2e716a6d0a47
@@ -11,26 +11,41 @@ module Clavatar
11
11
  end
12
12
  end
13
13
 
14
- args = {}
14
+ key_args = {}
15
+ args = []
15
16
  construct_params = example.method(:initialize).parameters
16
17
  construct_params.each do |param|
17
- arg_required = param.first
18
- arg_name = param.last
18
+ arg_required, arg_name = param
19
19
 
20
- if arg_required == :keyreq
21
- raise "parameter #{arg_name} is mandatory but missing in #{hash}" unless hash.key? arg_name
22
- elsif arg_required == :keyrest
23
- args.merge! hash
24
- break
20
+ case arg_required
21
+ when :req
22
+ args << arg_name
23
+ when :opt
24
+ args << arg_name if hash.key? arg_name
25
+ when :keyreq
26
+ raise "parameter #{arg_name} is mandatory but missing in #{hash}" unless hash.key? arg_name
27
+ key_args[arg_name] = hash[arg_name]
28
+ when :keyrest
29
+ key_args.merge! hash
30
+ break
25
31
  end
32
+ end
26
33
 
27
- args[arg_name] = hash[arg_name]
34
+ args.map! { |val| hash[val] }
35
+ if args.empty? and key_args.empty?
36
+ instance = klass.send(:new)
37
+ elsif args.empty?
38
+ instance = klass.send(:new, key_args)
39
+ elsif key_args.empty?
40
+ instance = klass.send(:new, *args)
41
+ else
42
+ instance = klass.send(:new, *args, **key_args)
28
43
  end
29
44
 
30
- instance = klass.new args
31
45
  accessible_attr.each do |attr|
32
46
  instance.send("#{attr}=", hash[attr])
33
47
  end
34
- return instance
48
+
49
+ instance
35
50
  end
36
51
  end
@@ -1,4 +1,4 @@
1
1
  # Clavatar gem version
2
2
  module Clavatar
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clavatar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-12 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Apply a hash into a Ruby class and return an instance of it.
14
14
  email: hex0cter@gmail.com