normalizr.rb 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/normalizr/array_of.rb +13 -0
- data/lib/normalizr/bag.rb +22 -0
- data/lib/normalizr/normalizr.rb +17 -0
- data/lib/normalizr/schema.rb +19 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 14a62252ac650d4490d1b11254c29f0c30e0c67b
|
|
4
|
+
data.tar.gz: 62deb44c0f628abb59d6f48e19db4fcc0bc23e09
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0d6a6235fa1171c8b2555482fc2e608898e06fef7ea9a25e3d795a4052335d2e6cd022f7d43c6db0bdaae6d01638828665b8820b7715b2b0fc8b2473a2c80099
|
|
7
|
+
data.tar.gz: 1c5bdd80223d1a2d79b6747fb8fb24e0f54abf5f0d59b7116cd4719a9c9d897284efec5af9d41b92b0e4e4493988f8f3af91a451bca23119b1b2a47172cf977f
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Normalizr
|
|
2
|
+
class Bag
|
|
3
|
+
def initialize bag={}
|
|
4
|
+
@bag = bag
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def add name, value
|
|
8
|
+
value[:id] ||= next_guid!
|
|
9
|
+
@bag[name.to_sym] ||= {}
|
|
10
|
+
@bag[name.to_sym][value[:id]] = value
|
|
11
|
+
value[:id]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_hash
|
|
15
|
+
@bag.dup
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def next_guid!
|
|
19
|
+
SecureRandom.uuid
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'normalizr/bag'
|
|
2
|
+
|
|
3
|
+
module Normalizr
|
|
4
|
+
def self.normalize! obj, schema
|
|
5
|
+
bag = Normalizr::Bag.new
|
|
6
|
+
|
|
7
|
+
if schema.is_a? Hash
|
|
8
|
+
schema.keys.each do |key|
|
|
9
|
+
schema[key].visit(obj[key], bag)
|
|
10
|
+
end
|
|
11
|
+
else
|
|
12
|
+
schema.visit(obj, bag)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
bag.to_hash
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module Normalizr
|
|
2
|
+
class Schema
|
|
3
|
+
|
|
4
|
+
def initialize name, definition={}
|
|
5
|
+
@name = name
|
|
6
|
+
@definition = definition
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def visit obj, bag
|
|
10
|
+
relationships = @definition.keys.map do |key|
|
|
11
|
+
id = @definition[key].visit(obj[key], bag)
|
|
12
|
+
Hash[key, id]
|
|
13
|
+
end.reduce({}, &:merge)
|
|
14
|
+
|
|
15
|
+
bag.add(@name, obj.merge(relationships))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: normalizr.rb
|
|
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: Normalize deeply nested hash based on a schema
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/normalizr/array_of.rb
|
|
20
|
+
- lib/normalizr/bag.rb
|
|
21
|
+
- lib/normalizr/normalizr.rb
|
|
22
|
+
- lib/normalizr/schema.rb
|
|
23
|
+
homepage: https://github.com/chrokh/normalizr
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata: {}
|
|
27
|
+
post_install_message:
|
|
28
|
+
rdoc_options: []
|
|
29
|
+
require_paths:
|
|
30
|
+
- lib
|
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '0'
|
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
requirements: []
|
|
42
|
+
rubyforge_project:
|
|
43
|
+
rubygems_version: 2.5.1
|
|
44
|
+
signing_key:
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Normalize deeply nested hash based on a schema
|
|
47
|
+
test_files: []
|