json_to_openstruct 0.1.0
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/json_to_openstruct.rb +83 -0
- data/lib/string_helper.rb +13 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d023c841b4ab50a9ba98a91669d5dca326221cb5
|
4
|
+
data.tar.gz: cf2f1b788c0d8918bca37aad372ee1a65db56ffd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ae2c2d9ee5621a983199c395836337a3e5e74b9affbf1ed6bb25c4dab88dc2b8f97ef5446832dab5c9c96c2da682b10de078143d33a2e062386305fd8628d1d2
|
7
|
+
data.tar.gz: 452fed4ac179543819fd6deeabdd40272ce60dca5126fcd1b62cbdc2663c47d9b7281192d4dd9fb0ebe4c6acbf1bd546f18ee1b4bdd727058cbdb3d2d9811907
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'string_helper'
|
3
|
+
|
4
|
+
module JsonToOpenStruct
|
5
|
+
|
6
|
+
def self.included(host_class)
|
7
|
+
host_class.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_json
|
11
|
+
hash = self.to_hash
|
12
|
+
hash.to_json
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_hash
|
16
|
+
hash = {}
|
17
|
+
self.to_h.each do |key, value|
|
18
|
+
if self[key].kind_of?(Array)
|
19
|
+
hash[key] = []
|
20
|
+
self[key].each do |array_item|
|
21
|
+
hash[key] << array_item.to_hash
|
22
|
+
end
|
23
|
+
elsif self[key].kind_of?(self.class)
|
24
|
+
hash[key] = value.to_hash
|
25
|
+
else
|
26
|
+
hash[key] = value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
|
32
|
+
def join(params)
|
33
|
+
hash = self.to_hash
|
34
|
+
parse(hash, params)
|
35
|
+
hash
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def parse(hash, params)
|
41
|
+
params.keys.each do |key|
|
42
|
+
if params[key].kind_of?(Hash)
|
43
|
+
if key.end_with?("_attributes")
|
44
|
+
params[key].keys.each do |index|
|
45
|
+
parse(hash[StringHelper.normalize(key).to_sym][index.to_i], params[key][index])
|
46
|
+
end
|
47
|
+
else
|
48
|
+
parse(hash[key.to_sym], params[key])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
hash[key.to_sym] = params[key]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module ClassMethods
|
57
|
+
|
58
|
+
def parse_hash(hash)
|
59
|
+
struct = self.new
|
60
|
+
hash.keys.each do |key|
|
61
|
+
attr_name = StringHelper.underscore(key.to_s)
|
62
|
+
struct.send(attr_name)
|
63
|
+
if hash[key].kind_of?(Array)
|
64
|
+
struct[attr_name] = []
|
65
|
+
hash[key].each do |arr_item|
|
66
|
+
struct[attr_name] << self.parse_hash(arr_item)
|
67
|
+
end
|
68
|
+
elsif hash[key].kind_of?(Hash)
|
69
|
+
struct[attr_name] = self.parse_hash(hash[key])
|
70
|
+
else
|
71
|
+
struct[attr_name] = hash[key]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
struct
|
75
|
+
end
|
76
|
+
|
77
|
+
def parse_json(json)
|
78
|
+
hash = JSON.parse(json)
|
79
|
+
parse_hash(hash)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module StringHelper
|
2
|
+
def self.underscore(word)
|
3
|
+
word.split(/(?=[A-Z])/).each { |str| str.downcase! }.join("_")
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.normalize(word)
|
7
|
+
underscore(word.sub(/_attributes/, ''))
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.is_i?(word)
|
11
|
+
/\A[-+]?\d+\z/ === word
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_to_openstruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jiri Prochazka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: A simple Ruby gem adding OpenStruct::parse_json and OpenStruct::parse_hash
|
28
|
+
methods and reverse OpenStruct#to_hash and OpenStruct#to_json methods to (obviously)
|
29
|
+
OpenStruct. It is able to parse and reconstruct nested json and hash objects as
|
30
|
+
well. There is also a OpenStruct#join method adding possibility to replace values
|
31
|
+
in the hash tree with the values passed as a hash in parameter (supporting accepts_nested_attributes_for
|
32
|
+
format) and return it as a new hash.
|
33
|
+
email: prochazka@coderocket.co
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/json_to_openstruct.rb
|
39
|
+
- lib/string_helper.rb
|
40
|
+
homepage: https://github.com/CodeRocketCo/json_to_openstruct
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.6.8
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: A simple Ruby gem adding a functionality to OpenStruct to load deep nested
|
64
|
+
json objects and hashes as OpenStruct. It also adds a posibility to generate json
|
65
|
+
and hash back.
|
66
|
+
test_files: []
|