ostruct_deep 0.1.3 → 0.1.4
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/open_struct_extend/hash/hash.rb +20 -0
- data/lib/open_struct_extend/ostruct_deep/open_struct_deep.rb +39 -0
- data/lib/open_struct_extend/ostruct_deep.rb +2 -0
- data/lib/open_struct_extend/version.rb +1 -1
- data/lib/ostruct_deep.rb +2 -0
- metadata +6 -3
- data/lib/open_struct_deep.rb +0 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4587d8f5002f9fba22152b058af45eed9a07d1d5
|
|
4
|
+
data.tar.gz: c3a245eae8ba1d5d9103b30fb4fd00e85666ed5b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9c6c6bbbcd233fb3e9ce748dc50956596fbeb4f5df0ac923b632319a160143d935f414ec5af65976b2ef20e3f96abee9e5dc7c3bf9a4744a5fad79609fe53e0b
|
|
7
|
+
data.tar.gz: d70e2c079eac5c72c961258fbe3f6f0597d880d06847cb556876f5d7190851f530260dd07d7905db043905c15ac29bb762708b61eda1657a52651c2123b3e968
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# OpenStructDeep
|
|
2
2
|
[](https://badge.fury.io/rb/ostruct_deep)
|
|
3
|
+
[](https://travis-ci.org/yhk1038/ostruct_deep)
|
|
3
4
|
|
|
4
5
|
Make deeper ostruct, such as method chain of javascript object
|
|
5
6
|
|
|
@@ -21,7 +22,7 @@ Or install it yourself as:
|
|
|
21
22
|
|
|
22
23
|
## Usage
|
|
23
24
|
```ruby
|
|
24
|
-
require '
|
|
25
|
+
require 'ostruct_deep'
|
|
25
26
|
|
|
26
27
|
# Put your hash structure for the first parameter
|
|
27
28
|
hash = {name: "John Smith", age: 70, pension: 300}
|
|
@@ -47,6 +48,21 @@ puts person.age # -> 70
|
|
|
47
48
|
puts person.address # -> nil
|
|
48
49
|
```
|
|
49
50
|
|
|
51
|
+
#### <Hash obj>.to_ostruct_deep
|
|
52
|
+
- return : object of OpenStructDeep
|
|
53
|
+
```ruby
|
|
54
|
+
hash = {name: "John Smith", age: 70, pension: 300}
|
|
55
|
+
hash.to_ostruct_deep # -> #<OpenStructDeep name="John Smith", age=70, pension=300>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### <OpenStructDeep obj>.to_h
|
|
59
|
+
- return : object of hash
|
|
60
|
+
```ruby
|
|
61
|
+
hash = {name: "John Smith", age: 70, pension: 300}
|
|
62
|
+
osd = OpenStructDeep.new(hash) # -> <OpenStructDeep name="John Smith", age=70, pension=300>
|
|
63
|
+
osd.to_h # -> {:name=>"John Smith", :age=>70, :pension=>300}
|
|
64
|
+
```
|
|
65
|
+
|
|
50
66
|
## What's new?
|
|
51
67
|
### Originals Problem in (OpenStructure)
|
|
52
68
|
In original ostruct (rubysl-ostruct) do below:
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require_relative '../ostruct_deep/open_struct_deep'
|
|
2
|
+
|
|
3
|
+
class Hash
|
|
4
|
+
def to_ostruct_deep
|
|
5
|
+
OpenStructDeep.new self
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def symbolize_keys
|
|
9
|
+
transform_keys{ |key| key.to_sym rescue key }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def transform_keys
|
|
13
|
+
return enum_for(:transform_keys) unless block_given?
|
|
14
|
+
result = self.class.new
|
|
15
|
+
each_key do |key|
|
|
16
|
+
result[yield(key)] = self[key]
|
|
17
|
+
end
|
|
18
|
+
result
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'ostruct'
|
|
2
|
+
|
|
3
|
+
class OpenStructDeep < OpenStruct
|
|
4
|
+
|
|
5
|
+
def initialize(hash=nil)
|
|
6
|
+
@table = {}
|
|
7
|
+
if hash
|
|
8
|
+
hash.each_pair do |k, v|
|
|
9
|
+
k = k.to_sym
|
|
10
|
+
v = case v
|
|
11
|
+
when Hash
|
|
12
|
+
OpenStructDeep.new v
|
|
13
|
+
when Array
|
|
14
|
+
v.map { |x| OpenStructDeep.new x }
|
|
15
|
+
else
|
|
16
|
+
v
|
|
17
|
+
end
|
|
18
|
+
@table[k] = v
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_h
|
|
24
|
+
{}.tap do |dup|
|
|
25
|
+
@table.dup.each { |k, v| dup[k] = map_value(v) }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def map_value(thing)
|
|
32
|
+
case thing
|
|
33
|
+
when OpenStructDeep then thing.to_h
|
|
34
|
+
when Array then thing.map { |v| map_value(v) }
|
|
35
|
+
else thing
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
data/lib/ostruct_deep.rb
ADDED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ostruct_deep
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Fred Kim
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-04-
|
|
11
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -69,8 +69,11 @@ files:
|
|
|
69
69
|
- Rakefile
|
|
70
70
|
- bin/console
|
|
71
71
|
- bin/setup
|
|
72
|
-
- lib/
|
|
72
|
+
- lib/open_struct_extend/hash/hash.rb
|
|
73
|
+
- lib/open_struct_extend/ostruct_deep.rb
|
|
74
|
+
- lib/open_struct_extend/ostruct_deep/open_struct_deep.rb
|
|
73
75
|
- lib/open_struct_extend/version.rb
|
|
76
|
+
- lib/ostruct_deep.rb
|
|
74
77
|
- open_struct_extend.gemspec
|
|
75
78
|
homepage: https://github.com/yhk1038/ostruct_deep
|
|
76
79
|
licenses:
|
data/lib/open_struct_deep.rb
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
require "open_struct_extend/version"
|
|
2
|
-
require 'ostruct'
|
|
3
|
-
|
|
4
|
-
class OpenStructDeep < OpenStruct
|
|
5
|
-
def initialize(hash=nil)
|
|
6
|
-
@table = {}
|
|
7
|
-
if hash
|
|
8
|
-
hash.each_pair do |k, v|
|
|
9
|
-
k = k.to_sym
|
|
10
|
-
v = OpenStructDeep.new v if v.is_a? Hash
|
|
11
|
-
v = v.map { |x| OpenStructDeep.new x } if v.is_a? Array
|
|
12
|
-
@table[k] = v
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|