initializr 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7430620066af434f85ec79e91287bc59e3cdd68e
4
+ data.tar.gz: 4d51307d2b24e3818af66b0238120ea663208f5a
5
+ SHA512:
6
+ metadata.gz: 3c95a8e8b076e51a844b888f138511b649b14946b6579a7cd9b600bdf964cabb8c4f239da850b0d7125989d1b8dd73baa3d73add4d335d8481d164916b96e4ab
7
+ data.tar.gz: c093b377494559fcf66f78ad363159232c58ea8452972e114de2fa019c9c89b9e1b0b429f24af11b5cb3dad5114f128e922f39c08b3dadf4af6812efc797aad6
@@ -0,0 +1,15 @@
1
+ module Initializr
2
+
3
+ def self.instantiate obj, schemas
4
+ if schemas.is_a? Hash
5
+ children = schemas.keys.map do |key|
6
+ value = schemas[key].instantiate(obj[key])
7
+ Hash[key, value]
8
+ end.reduce({}, &:merge)
9
+ obj.merge(children)
10
+ else
11
+ schemas.instantiate(obj)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ module Initializr
2
+ class ArrayOf
3
+
4
+ def initialize schema
5
+ @schema = schema
6
+ end
7
+
8
+ def instantiate objs
9
+ objs.map { |obj| @schema.instantiate(obj) }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module Initializr
2
+ class Default
3
+
4
+ def initialize schema, default
5
+ @schema = schema
6
+ @default = default
7
+ end
8
+
9
+ def instantiate obj
10
+ if !obj.nil?
11
+ @schema.instantiate obj
12
+ else
13
+ @default
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Initializr
2
+ class HashOf
3
+
4
+ def initialize schema
5
+ @schema = schema
6
+ end
7
+
8
+ def instantiate obj
9
+ obj.keys.map do |key|
10
+ Hash[key, @schema.instantiate(obj[key])]
11
+ end.reduce({}, &:merge)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module Initializr
2
+ class Schema
3
+
4
+ def initialize klass, schemas = {}
5
+ @klass = klass
6
+ @schemas = schemas
7
+ end
8
+
9
+ def instantiate obj
10
+ children = @schemas.keys.map do |key|
11
+ value = @schemas[key].instantiate(obj[key])
12
+ Hash[key, value]
13
+ end.reduce({}, &:merge)
14
+
15
+ args = obj.merge(children)
16
+
17
+ @klass.new args
18
+ end
19
+ end
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: initializr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Okhravi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/initializr.rb
20
+ - lib/initializr/array_of.rb
21
+ - lib/initializr/default.rb
22
+ - lib/initializr/hash_of.rb
23
+ - lib/initializr/schema.rb
24
+ homepage: https://github.com/chrokh/initializr
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.5.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Initialize object graph from deeply nested hash based on a schema
48
+ test_files: []