clavatar 0.0.2 → 0.0.3

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 +17 -12
  3. data/lib/version.rb +1 -1
  4. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bf8acd0bef190c30df6f83601381a111eb9995b
4
- data.tar.gz: b39b837348b64a8830e81fdb1592f397e91ce4d4
3
+ metadata.gz: 17de1bf0a3047061ba02eda6f95c6c8541f8600d
4
+ data.tar.gz: 8b314fa0809ee5608ae487be0c935f53b0eff8c3
5
5
  SHA512:
6
- metadata.gz: 5e0d642c1a5e629c2452028c74b4ddd87447a11c61ddf0b8d1b23006df762c8fe3019391a7609be792d37f94bc470741f803d117bf87da53819aa878c005605f
7
- data.tar.gz: 49fe7d4193bf09212b0da64c6271216f05dbe1cbe995bbea453b48db6ede0249eec54da3fd6a152be121b30aefd7ff0fb42d35f8c594ced31fb62683be0ddfc7
6
+ metadata.gz: 1fe98ec084a697bcc000c5d9b80582ac2d63bcd337788b88f2bc17ce4bf3e66cdbbae49d83e40daac91db21a063f02857bd22a7846bae7f32f04c049b394bb7a
7
+ data.tar.gz: 5e1f4a32117a612a0412ea51cc74a664e7c5c8bbc635d86647b0cadbc77fb5763934b1904a05e47b2941735d29c785d066fbce6cbe8fd48c830c206f1b7e8ee6
data/lib/clavatar.rb CHANGED
@@ -3,26 +3,31 @@ require 'ostruct'
3
3
  # Clavatar module
4
4
  module Clavatar
5
5
  def self.cast(hash, klass, example)
6
- name_space = klass.respond_to?(:parent) ? klass.parent : Object
7
- klass_name = "#{klass.name.split('::').last}Avatar"
8
- if name_space.const_defined? klass_name
9
- klass = name_space.const_get klass_name
10
- else
11
- klass = name_space.const_set klass_name, Class.new(klass)
12
- end
13
-
14
- args = {}
15
6
  accessible_attr = []
16
7
  example.instance_variables.each do |attr|
17
8
  attr = attr.to_s.sub('@', '').to_sym
18
9
  if example.respond_to? ("#{attr}=")
19
10
  accessible_attr << attr
20
- else
21
- args[attr] = hash[attr]
22
11
  end
23
12
  end
24
13
 
25
- instance = klass.new args.merge(hash)
14
+ args = {}
15
+ construct_params = example.method(:initialize).parameters
16
+ construct_params.each do |param|
17
+ arg_required = param.first
18
+ arg_name = param.last
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
25
+ end
26
+
27
+ args[arg_name] = hash[arg_name]
28
+ end
29
+
30
+ instance = klass.new args
26
31
  accessible_attr.each do |attr|
27
32
  instance.send("#{attr}=", hash[attr])
28
33
  end
data/lib/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # Clavatar gem version
2
2
  module Clavatar
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clavatar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Han
@@ -10,7 +10,7 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-06-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Class avatar which convert a hash to an object
13
+ description: Apply a hash into a Ruby Class and return an instance of it.
14
14
  email: hex0cter@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -28,18 +28,18 @@ require_paths:
28
28
  - lib
29
29
  required_ruby_version: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.0.0
34
34
  required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
- - - '>='
36
+ - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 2.0.14.1
41
+ rubygems_version: 2.5.1
42
42
  signing_key:
43
43
  specification_version: 4
44
- summary: Class avatar which convert a hash to an object
44
+ summary: Convert a hash into a Ruby class instance
45
45
  test_files: []