deep-hash-struct 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca7fd7431f8ba9c095d2b71a3664df4376f9eab0
4
- data.tar.gz: 1d9cc7bae3de0094e77d11b7dd0aa8b03ac9d327
3
+ metadata.gz: 1aa824e97416264b19d1bc4436292c26cfc62aab
4
+ data.tar.gz: 8df3bff21c7c12ff5c690fa01c22d84126a739e0
5
5
  SHA512:
6
- metadata.gz: b143885725f4001a8a41ba69b5561a2202afc3faed403fb94cef23745e6ff9057d468bade4a2b0f1ce9977fa2d3c8dd631271d76b78ae2b575ff064507bd1cd4
7
- data.tar.gz: f048170c01ff4ff115db75cff4d99332ce851d7b8ee5dd33c09320eb81208ce72c4b2e52599d6de0960ebd8cabf5a61fd17dbe39a79b01c7eabee1c55f09ee3e
6
+ metadata.gz: 8d9353d008e6f32345e64233d8841c23eb4f0a2b2786eebeeb46e53322b123ff551be42cbe9b71542ce7225dce02b0c5314ba60976cb88cacb2946da35d2dbb7
7
+ data.tar.gz: 659123eb11918ca7ea2926a72baf2b0ddc0e9cee02c5206e7da39b7044700dd50eabf0615f5ba94c39cdb299890fd0b41e233e6666c3cc871078a2d6c8cdbdb4
data/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/deep/hash/struct`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
@@ -22,7 +20,12 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ wrapper = Deep::Hash::Struct::Wrapper.new
24
+ wrapper.a = 1
25
+ wrapper.b.a = 2
26
+ wrapper[:c][:a] = 3
27
+ wrapper[:c].b = 4
28
+ wrapper.to_h # => => {:a=>1, :b=>{:a=>2}, :c=>{:a=>3, :b=>4}}
26
29
 
27
30
  ## Development
28
31
 
@@ -32,7 +35,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
35
 
33
36
  ## Contributing
34
37
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep-hash-struct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/etiopiamokamame/deep-hash-struct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
39
 
37
40
 
38
41
  ## License
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["adachi"]
10
10
  spec.email = ["haimaki4222@gmail.com"]
11
11
 
12
- spec.summary = "test"
13
- spec.description = "test"
14
- spec.homepage = "https://github.com/adachi/deep-hash-struct"
12
+ spec.summary = "Convenient hash."
13
+ spec.description = "A slightly useful hash-like object."
14
+ spec.homepage = "https://github.com/etiopiamokamame/deep-hash-struct"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -5,6 +5,11 @@ module Deep
5
5
  module Hash
6
6
  module Struct
7
7
  class Wrapper
8
+ def initialize(h = {})
9
+ return if h.nil? || h.count.zero?
10
+ wrap h, self
11
+ end
12
+
8
13
  def keys
9
14
  instance_variables.map do |k|
10
15
  k.to_s[1..-1].to_sym
@@ -59,6 +64,65 @@ module Deep
59
64
  nil
60
65
  end
61
66
 
67
+ def merge(hash)
68
+ klass = self.dup
69
+
70
+ hash.each do |k, v|
71
+ klass[k] = v
72
+ end
73
+
74
+ klass
75
+ end
76
+
77
+ def merge!(hash)
78
+ hash.each do |k, v|
79
+ self[k] = v
80
+ end
81
+ self
82
+ end
83
+
84
+ def deep_merge(hash)
85
+ klass = self.dup
86
+
87
+ hash.each do |k , v|
88
+ klass[k] = hash?(v) ? deep_merge(v) : v
89
+ end
90
+
91
+ klass
92
+ end
93
+
94
+ def deep_merge!(hash, klass = self)
95
+ hash.each do |k, v|
96
+ klass[k] = hash?(v) ? deep_merge!(v, klass[k]) : v
97
+ end
98
+
99
+ klass
100
+ end
101
+
102
+ def fetch(k, msg = nil)
103
+ v = self[k]
104
+ if block_given?
105
+ if v.class == self.class || v.nil?
106
+ yield k
107
+ else
108
+ v
109
+ end
110
+ else
111
+ case v
112
+ when self.class
113
+ if v.blank?
114
+ msg
115
+ else
116
+ to_h
117
+ end
118
+ when nil
119
+ msg
120
+ else
121
+ v
122
+ end
123
+ end
124
+ end
125
+
62
126
  def to_hash
63
127
  deep_hash self
64
128
  end
@@ -95,8 +159,8 @@ module Deep
95
159
  h
96
160
  end
97
161
 
98
- def wrap(hash)
99
- base = self.class.new
162
+ def wrap(hash, klass = nil)
163
+ base = klass || self.class.new
100
164
 
101
165
  hash.each do |k, v|
102
166
  base[k] = hash?(v) ? wrap(v) : v
@@ -1,7 +1,7 @@
1
1
  module Deep
2
2
  module Hash
3
3
  module Struct
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep-hash-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - adachi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-23 00:00:00.000000000 Z
11
+ date: 2017-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: test
55
+ description: A slightly useful hash-like object.
56
56
  email:
57
57
  - haimaki4222@gmail.com
58
58
  executables: []
@@ -72,7 +72,7 @@ files:
72
72
  - deep-hash-struct.gemspec
73
73
  - lib/deep/hash/struct.rb
74
74
  - lib/deep/hash/struct/version.rb
75
- homepage: https://github.com/adachi/deep-hash-struct
75
+ homepage: https://github.com/etiopiamokamame/deep-hash-struct
76
76
  licenses:
77
77
  - MIT
78
78
  metadata: {}
@@ -95,5 +95,5 @@ rubyforge_project:
95
95
  rubygems_version: 2.6.8
96
96
  signing_key:
97
97
  specification_version: 4
98
- summary: test
98
+ summary: Convenient hash.
99
99
  test_files: []