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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1449c43755a27d4bd9b936540cb8ecabb94745a9
4
- data.tar.gz: c398c1184a4dd1a6287eae9ec8af6e0879021f2f
3
+ metadata.gz: 4587d8f5002f9fba22152b058af45eed9a07d1d5
4
+ data.tar.gz: c3a245eae8ba1d5d9103b30fb4fd00e85666ed5b
5
5
  SHA512:
6
- metadata.gz: 4d46efbe0ca190d7549ba7b6aec829a2c8b28e8c555004d97d9088b5ba9270bd41bb26d9edb143d894e48f15379dfd6bac2fc97539373ab102913cfc6bacac4c
7
- data.tar.gz: e66edf93297806731644e13c280aedba2de70a8b918d880f598746b424a795661d9db28a85a47089932b93f25c5e1a4a15fd46d575f28915b296a5029a69ebcd
6
+ metadata.gz: 9c6c6bbbcd233fb3e9ce748dc50956596fbeb4f5df0ac923b632319a160143d935f414ec5af65976b2ef20e3f96abee9e5dc7c3bf9a4744a5fad79609fe53e0b
7
+ data.tar.gz: d70e2c079eac5c72c961258fbe3f6f0597d880d06847cb556876f5d7190851f530260dd07d7905db043905c15ac29bb762708b61eda1657a52651c2123b3e968
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ostruct_deep (0.1.3)
4
+ ostruct_deep (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # OpenStructDeep
2
2
  [![Gem Version](https://badge.fury.io/rb/ostruct_deep.png)](https://badge.fury.io/rb/ostruct_deep)
3
+ [![Build Status](https://travis-ci.org/yhk1038/ostruct_deep.svg?branch=master)](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 'open_struct_deep'
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
@@ -0,0 +1,2 @@
1
+ require_relative 'ostruct_deep/open_struct_deep'
2
+ require_relative 'hash/hash'
@@ -1,3 +1,3 @@
1
1
  module OpenStructExtend
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -0,0 +1,2 @@
1
+ require 'open_struct_extend/version'
2
+ require 'open_struct_extend/ostruct_deep'
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.3
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-17 00:00:00.000000000 Z
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/open_struct_deep.rb
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:
@@ -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