r2-oas 0.1.3 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -0
- data/GEMSPEC.md +20 -0
- data/README.ja.md +63 -2
- data/README.md +62 -2
- data/lib/r2-oas.rb +3 -4
- data/lib/r2-oas/app_configuration.rb +1 -1
- data/lib/r2-oas/deploy/client.rb +33 -6
- data/lib/r2-oas/errors.rb +1 -0
- data/lib/r2-oas/lib/core_ext/hash/deep_merge.rb +44 -0
- data/lib/r2-oas/lib/core_ext/object/blank.rb +135 -0
- data/lib/r2-oas/lib/three-way-merge/twm.rb +83 -0
- data/lib/r2-oas/schema/builder.rb +23 -0
- data/lib/r2-oas/schema/editor.rb +13 -2
- data/lib/r2-oas/schema/generator.rb +1 -1
- data/lib/r2-oas/schema/manager/file_manager.rb +26 -0
- data/lib/r2-oas/schema/ui.rb +0 -1
- data/lib/r2-oas/schema/v3/analyzer/base_analyzer.rb +1 -6
- data/lib/r2-oas/schema/v3/builder.rb +28 -0
- data/lib/r2-oas/schema/v3/builder/base_builder.rb +60 -0
- data/lib/r2-oas/schema/v3/builder/doc_builder.rb +43 -0
- data/lib/r2-oas/schema/v3/generator.rb +1 -5
- data/lib/r2-oas/schema/v3/generator/base_generator.rb +30 -1
- data/lib/r2-oas/schema/v3/generator/components/object_generator.rb +6 -25
- data/lib/r2-oas/schema/v3/generator/components/request_body_generator.rb +6 -6
- data/lib/r2-oas/schema/v3/generator/doc_generator.rb +84 -19
- data/lib/r2-oas/schema/v3/generator/path_generator.rb +7 -19
- data/lib/r2-oas/schema/v3/generator/schema_generator.rb +5 -25
- data/lib/r2-oas/schema/v3/manager/file/base_file_manager.rb +3 -4
- data/lib/r2-oas/schema/v3/manager/pathname_manager.rb +6 -1
- data/lib/r2-oas/store.rb +118 -0
- data/lib/r2-oas/task_logging.rb +6 -4
- data/lib/r2-oas/tasks/common.rake +0 -1
- data/lib/r2-oas/tasks/main.rake +66 -73
- data/lib/r2-oas/tasks/tool.rake +32 -28
- data/lib/r2-oas/version.rb +1 -1
- metadata +85 -69
- data/.github/ISSUE_TEMPLATE.md +0 -12
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -12
- data/.gitignore +0 -12
- data/.rspec +0 -3
- data/.rubocop.yml +0 -7
- data/.rubocop_todo.yml +0 -228
- data/.travis.yml +0 -22
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -222
- data/Rakefile +0 -8
- data/bin/console +0 -12
- data/bin/setup +0 -8
- data/docs/.nojekyll +0 -0
- data/docs/README.md +0 -173
- data/docs/_sidebar.md +0 -23
- data/docs/attention/if_clash.md +0 -19
- data/docs/index.html +0 -28
- data/docs/schema/3.0.0.md +0 -155
- data/docs/setting/COC.md +0 -14
- data/docs/setting/CORS.md +0 -22
- data/docs/setting/configure.md +0 -163
- data/docs/usage/analyze_docs.md +0 -875
- data/docs/usage/clean_docs.md +0 -19
- data/docs/usage/deploy_docs.md +0 -839
- data/docs/usage/display_paths_list.md +0 -35
- data/docs/usage/display_paths_stats.md +0 -54
- data/docs/usage/edit_docs.md +0 -218
- data/docs/usage/generate_docs.md +0 -256
- data/docs/usage/monitor_docs.md +0 -219
- data/docs/usage/use_hook_methods.md +0 -236
- data/docs/usage/use_hook_to_generate_docs.md +0 -235
- data/docs/usage/use_schema_namespace.md +0 -183
- data/docs/usage/use_tag_namespace.md +0 -182
- data/docs/usage/view_docs.md +0 -262
- data/lib/r2-oas/deploy/swagger-ui/dist/favicon-16x16.png +0 -0
- data/lib/r2-oas/deploy/swagger-ui/dist/favicon-32x32.png +0 -0
- data/lib/r2-oas/deploy/swagger-ui/dist/oauth2-redirect.html +0 -68
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-bundle.js +0 -134
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-bundle.js.map +0 -1
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-standalone-preset.js +0 -22
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui-standalone-preset.js.map +0 -1
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.css +0 -4
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.css.map +0 -1
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.js +0 -9
- data/lib/r2-oas/deploy/swagger-ui/dist/swagger-ui.js.map +0 -1
- data/r2-oas.gemspec +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70122a2e55b9332c9e8d8c1bc50254b274a782451d1c7d1beac5fc8f04e152b6
|
4
|
+
data.tar.gz: 8f3f42e1fd68954b4a2d0f9e4fa6b8cd8d4cc5b655f94d93022813023a9a9f45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f9c9ed6a2a384a9bc2246946265e5e57f4f0f33c29c2c42c16cf8322bc7499d06aaf8f2a70696093e0a897d5cb0599622af86d25709285479169962d26775e2
|
7
|
+
data.tar.gz: 26c3ae7cc8f45b0acd46bb2d8662379d2cad4fa499d25d7f358435b63d706ed73b0fa6b52fabd82aca9f59d42f10fbf4014cb240e6ec678a278a6407cc521536
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,54 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## v0.3.3
|
4
|
+
|
5
|
+
2020-07-11
|
6
|
+
|
7
|
+
- [`FixBugs`] A cute pet store syndrome 🐈 ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
8
|
+
- [`FixBugs`] R2OAS.logger.level does not work ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
9
|
+
- [`Breaking`] Change default namespace type from `:underbar` to `:dot` ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
10
|
+
- [`Other`] Modify gemspec `files` fields ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
11
|
+
|
12
|
+
See milestone [v0.3.3](https://github.com/yukihirop/r2-oas/milestone/3?closed=1)
|
13
|
+
|
14
|
+
## v0.3.2
|
15
|
+
|
16
|
+
2020-07-05
|
17
|
+
|
18
|
+
- [`Feature`] The file size at the time of gem install was reduced by about 10MB. ([dbdbce9 ](https://github.com/yukihirop/r2-oas/pull/138))
|
19
|
+
|
20
|
+
## v0.3.1
|
21
|
+
|
22
|
+
2020-06-07
|
23
|
+
|
24
|
+
- [`Breaking`] Remove unnecessary runtime dependencies ([384ea1a](https://github.com/yukihirop/r2-oas/pull/132))
|
25
|
+
|
26
|
+
## v0.3.0
|
27
|
+
|
28
|
+
2020-05-30
|
29
|
+
|
30
|
+
- [`Feature/Breaking`] Support Ruby 2.7 🎉 ([931ec4b](https://github.com/yukihirop/r2-oas/pull/122))
|
31
|
+
|
32
|
+
- Remove `Gemfile.lock`
|
33
|
+
|
34
|
+
- [`Developer`] Create script to test all support ruby in development ([8d0df98](https://github.com/yukihirop/r2-oas/pull/124))
|
35
|
+
- [`Docs`] Add docs about Trableshouting ([f4a782f](https://github.com/yukihirop/r2-oas/pull/125))
|
36
|
+
|
37
|
+
## v0.2.0
|
38
|
+
|
39
|
+
2020-05-02
|
40
|
+
|
41
|
+
- [`Feature/Breaking`] Upgrade `routes:oas:docs` cmd ([37ccddf](https://github.com/yukihirop/r2-oas/pull/117))
|
42
|
+
|
43
|
+
- `.docs` is needed to continue using r2-oas. `If you are using previous version`,you need to run the following command to generate `.docs`.
|
44
|
+
|
45
|
+
```bash
|
46
|
+
CACHE_DOCS=true bundle exec rake routes:oas:docs
|
47
|
+
```
|
48
|
+
|
49
|
+
- [`Breaking`] Divide into `Generator` and `Builder` ([00fea7d](https://github.com/yukihirop/r2-oas/pull/116))
|
50
|
+
- `routes:oas:docs` cmd do not generate `oas_docs/oas_doc.yml`
|
51
|
+
|
3
52
|
## v0.1.3
|
4
53
|
|
5
54
|
2020-04-27
|
data/GEMSPEC.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Configureless and no DSL. 👍 But you can also configure.
|
2
|
+
Manage with yaml file instead of Ruby code.
|
3
|
+
Loosely coupled to your Rails project. 👍
|
4
|
+
So if you want to use another tool, you can port it right away. 😢
|
5
|
+
Generate api docment(OpenAPI) side only from `Rails` routing.
|
6
|
+
|
7
|
+
Provide rake tasks to management API Docment (OpenAPI) 🎉
|
8
|
+
|
9
|
+
|
10
|
+
$ bundle exec rake routes:oas:docs
|
11
|
+
$ bundle exec rake routes:oas:ui
|
12
|
+
$ bundle exec rake routes:oas:editor
|
13
|
+
$ bundle exec rake routes:oas:monitor
|
14
|
+
$ bundle exec rake routes:oas:dist
|
15
|
+
$ bundle exec rake routes:oas:clean
|
16
|
+
$ bundle exec rake routes:oas:analyze
|
17
|
+
$ bundle exec rake routes:oas:deploy
|
18
|
+
|
19
|
+
|
20
|
+
Happy Coding ❗️
|
data/README.ja.md
CHANGED
@@ -55,12 +55,12 @@ bundle exec routes:oas:editor
|
|
55
55
|
|
56
56
|
#### Generate docs
|
57
57
|
|
58
|
-
![oas_docs](https://user-images.githubusercontent.com/11146767/
|
58
|
+
![oas_docs](https://user-images.githubusercontent.com/11146767/80856236-0b839a80-8c83-11ea-888f-d0e659e0c251.gif)
|
59
59
|
|
60
60
|
|
61
61
|
#### Edit docs
|
62
62
|
|
63
|
-
![oas_editor](https://user-images.githubusercontent.com/11146767/
|
63
|
+
![oas_editor](https://user-images.githubusercontent.com/11146767/80856240-15a59900-8c83-11ea-9dbd-4382528944f2.gif)
|
64
64
|
|
65
65
|
## 📚 Documents
|
66
66
|
|
@@ -157,6 +157,67 @@ OpenAPIの3.0.0をサポートしてます。
|
|
157
157
|
|
158
158
|
公式ドキュメントはこちら => https://yukihirop.github.io/r2-oas/#/usage/use_hook_methods
|
159
159
|
|
160
|
+
|
161
|
+
## Bundle and Rspec with multiple ruby versions
|
162
|
+
|
163
|
+
#### Bundle
|
164
|
+
|
165
|
+
```bash
|
166
|
+
/bin/bash devscript/all_support_ruby.sh bundle
|
167
|
+
.
|
168
|
+
.
|
169
|
+
.
|
170
|
+
===== Bundle install for All Support Ruby Result =====
|
171
|
+
ruby-2.3.3: 0
|
172
|
+
ruby-2.4.2: 0
|
173
|
+
ruby-2.5.8: 0
|
174
|
+
ruby-2.6.6: 0
|
175
|
+
ruby-2.7.1: 0
|
176
|
+
======================================================
|
177
|
+
```
|
178
|
+
|
179
|
+
rubyのバージョンを `2.6.6` と `2.7.1` に指定する場合
|
180
|
+
|
181
|
+
```bash
|
182
|
+
/bin/bash devscript/all_support_ruby.sh bundle 2.6.6 2.7.1
|
183
|
+
.
|
184
|
+
.
|
185
|
+
.
|
186
|
+
===== Bundle install for All Support Ruby Result =====
|
187
|
+
ruby-2.6.6: 0
|
188
|
+
ruby-2.7.1: 0
|
189
|
+
======================================================
|
190
|
+
```
|
191
|
+
|
192
|
+
#### Rspec
|
193
|
+
|
194
|
+
```bash
|
195
|
+
/bin/bash devscript/all_support_ruby.sh rspec
|
196
|
+
.
|
197
|
+
.
|
198
|
+
.
|
199
|
+
===== Rspec for All Support Ruby Result =====
|
200
|
+
ruby-2.3.3: 0
|
201
|
+
ruby-2.4.2: 0
|
202
|
+
ruby-2.5.8: 0
|
203
|
+
ruby-2.6.6: 0
|
204
|
+
ruby-2.7.1: 0
|
205
|
+
=============================================
|
206
|
+
```
|
207
|
+
|
208
|
+
rubyのバージョンを `2.6.6` と `2.7.1` に指定する場合
|
209
|
+
|
210
|
+
```bash
|
211
|
+
/bin/bash devscript/all_support_ruby.sh rspec 2.6.6 2.7.1
|
212
|
+
.
|
213
|
+
.
|
214
|
+
.
|
215
|
+
===== Rspec for All Support Ruby Result =====
|
216
|
+
ruby-2.6.6: 0
|
217
|
+
ruby-2.7.1: 0
|
218
|
+
=============================================
|
219
|
+
```
|
220
|
+
|
160
221
|
## 🔩 CORS
|
161
222
|
|
162
223
|
[rack-cors](https://github.com/cyu/rack-cors)を使用する事でCORSを可能にします。
|
data/README.md
CHANGED
@@ -65,12 +65,12 @@ bundle exec routes:oas:editor
|
|
65
65
|
|
66
66
|
#### Generate docs
|
67
67
|
|
68
|
-
![oas_docs](https://user-images.githubusercontent.com/11146767/
|
68
|
+
![oas_docs](https://user-images.githubusercontent.com/11146767/80856236-0b839a80-8c83-11ea-888f-d0e659e0c251.gif)
|
69
69
|
|
70
70
|
|
71
71
|
#### Edit docs
|
72
72
|
|
73
|
-
![oas_editor](https://user-images.githubusercontent.com/11146767/
|
73
|
+
![oas_editor](https://user-images.githubusercontent.com/11146767/80856240-15a59900-8c83-11ea-9dbd-4382528944f2.gif)
|
74
74
|
|
75
75
|
## Usage
|
76
76
|
|
@@ -156,6 +156,66 @@ Supported hook(life cycle methods) is like this:
|
|
156
156
|
|
157
157
|
Full docs are available at https://yukihirop.github.io/r2-oas/#/usage/use_hook_methods
|
158
158
|
|
159
|
+
## Bundle and Rspec with multiple ruby versions
|
160
|
+
|
161
|
+
#### Bundle
|
162
|
+
|
163
|
+
```bash
|
164
|
+
/bin/bash devscript/all_support_ruby.sh bundle
|
165
|
+
.
|
166
|
+
.
|
167
|
+
.
|
168
|
+
===== Bundle install for All Support Ruby Result =====
|
169
|
+
ruby-2.3.3: 0
|
170
|
+
ruby-2.4.2: 0
|
171
|
+
ruby-2.5.8: 0
|
172
|
+
ruby-2.6.6: 0
|
173
|
+
ruby-2.7.1: 0
|
174
|
+
======================================================
|
175
|
+
```
|
176
|
+
|
177
|
+
If specify ruby version `2.6.6` and `2.7.1`
|
178
|
+
|
179
|
+
```bash
|
180
|
+
/bin/bash devscript/all_support_ruby.sh bundle 2.6.6 2.7.1
|
181
|
+
.
|
182
|
+
.
|
183
|
+
.
|
184
|
+
===== Bundle install for All Support Ruby Result =====
|
185
|
+
ruby-2.6.6: 0
|
186
|
+
ruby-2.7.1: 0
|
187
|
+
======================================================
|
188
|
+
```
|
189
|
+
|
190
|
+
#### Rspec
|
191
|
+
|
192
|
+
```bash
|
193
|
+
/bin/bash devscript/all_support_ruby.sh rspec
|
194
|
+
.
|
195
|
+
.
|
196
|
+
.
|
197
|
+
===== Rspec for All Support Ruby Result =====
|
198
|
+
ruby-2.3.3: 0
|
199
|
+
ruby-2.4.2: 0
|
200
|
+
ruby-2.5.8: 0
|
201
|
+
ruby-2.6.6: 0
|
202
|
+
ruby-2.7.1: 0
|
203
|
+
=============================================
|
204
|
+
```
|
205
|
+
|
206
|
+
If specify ruby version `2.6.6` and `2.7.1`
|
207
|
+
|
208
|
+
```bash
|
209
|
+
/bin/bash devscript/all_support_ruby.sh rspec 2.6.6 2.7.1
|
210
|
+
.
|
211
|
+
.
|
212
|
+
.
|
213
|
+
===== Rspec for All Support Ruby Result =====
|
214
|
+
ruby-2.6.6: 0
|
215
|
+
ruby-2.7.1: 0
|
216
|
+
=============================================
|
217
|
+
```
|
218
|
+
|
159
219
|
## 🔩 CORS
|
160
220
|
|
161
221
|
Use [rack-cors](https://github.com/cyu/rack-cors) to enable CORS.
|
data/lib/r2-oas.rb
CHANGED
@@ -6,14 +6,14 @@ require 'r2-oas/errors'
|
|
6
6
|
require 'r2-oas/schema/v3/object/public'
|
7
7
|
|
8
8
|
module R2OAS
|
9
|
-
extend ActiveSupport::Autoload
|
10
|
-
|
11
9
|
if !defined?(::Rails)
|
12
10
|
raise NoImplementError, 'Can not load Rails'
|
13
11
|
# support Rails version
|
14
12
|
elsif ::Rails::VERSION::STRING >= '4.2.5.1'
|
15
13
|
extend Configuration
|
16
14
|
require 'r2-oas/task'
|
15
|
+
require 'r2-oas/lib/core_ext/hash/deep_merge'
|
16
|
+
require 'r2-oas/lib/core_ext/object/blank'
|
17
17
|
|
18
18
|
autoload :Base, 'r2-oas/base'
|
19
19
|
autoload :NoImplementError, 'r2-oas/errors'
|
@@ -22,10 +22,9 @@ module R2OAS
|
|
22
22
|
autoload :Sortable, 'r2-oas/shared/all'
|
23
23
|
|
24
24
|
module Schema
|
25
|
-
extend ActiveSupport::Autoload
|
26
|
-
|
27
25
|
autoload :Base, 'r2-oas/schema/base'
|
28
26
|
autoload :Generator, 'r2-oas/schema/generator'
|
27
|
+
autoload :Builder, 'r2-oas/schema/builder'
|
29
28
|
autoload :Analyzer, 'r2-oas/schema/analyzer'
|
30
29
|
autoload :Squeezer, 'r2-oas/schema/squeezer'
|
31
30
|
autoload :Cleaner, 'r2-oas/schema/cleaner'
|
data/lib/r2-oas/deploy/client.rb
CHANGED
@@ -6,22 +6,33 @@ require 'fileutils'
|
|
6
6
|
module R2OAS
|
7
7
|
module Deploy
|
8
8
|
class Client < Schema::Base
|
9
|
+
SWAGGER_UI_DIST_URL = 'https://github.com/swagger-api/swagger-ui/trunk/dist'
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
super(options)
|
13
|
+
@download_dir = "#{SecureRandom.uuid[0..7]}/dist"
|
14
|
+
@dist_path = File.expand_path(Rails.root.join(@download_dir), __FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
def download_swagger_ui_dist
|
18
|
+
system("svn export #{SWAGGER_UI_DIST_URL} #{@dist_path}")
|
19
|
+
end
|
20
|
+
|
9
21
|
def deploy
|
10
22
|
copy_swagger_ui_dist
|
11
23
|
copy_swagger_ui_index
|
12
24
|
copy_oas_doc_file
|
25
|
+
ensure
|
26
|
+
remove_download_dist
|
13
27
|
end
|
14
28
|
|
15
29
|
private
|
16
30
|
|
17
31
|
def copy_swagger_ui_dist
|
18
32
|
docs_path = File.expand_path(Rails.root.join(deploy_dir_path), __FILE__)
|
19
|
-
return if FileTest.exists?(docs_path)
|
20
|
-
|
21
33
|
FileUtils.mkdir_p(docs_path) unless FileTest.exists?(docs_path)
|
22
|
-
|
23
|
-
|
24
|
-
FileUtils.cp_r(dist_path, docs_path)
|
34
|
+
FileUtils.mkdir_p(@dist_path) unless FileTest.exists?(@dist_path)
|
35
|
+
FileUtils.cp_r(@dist_path, docs_path)
|
25
36
|
end
|
26
37
|
|
27
38
|
def copy_swagger_ui_index
|
@@ -29,7 +40,7 @@ module R2OAS
|
|
29
40
|
@schema_file_path = doc_save_file_name
|
30
41
|
template_path = File.expand_path('swagger-ui/index.html.erb', __dir__)
|
31
42
|
template = File.read(template_path)
|
32
|
-
index =
|
43
|
+
index = make_index(template)
|
33
44
|
File.write(index_path, index)
|
34
45
|
end
|
35
46
|
|
@@ -38,6 +49,22 @@ module R2OAS
|
|
38
49
|
oas_doc_file_path = File.expand_path("#{root_dir_path}/#{doc_save_file_name}")
|
39
50
|
FileUtils.cp_r(oas_doc_file_path, swagger_file_path)
|
40
51
|
end
|
52
|
+
|
53
|
+
def remove_download_dist
|
54
|
+
FileUtils.rm_rf(File.expand_path('..', @dist_path))
|
55
|
+
end
|
56
|
+
|
57
|
+
# [ref]
|
58
|
+
# https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/ErbNewArguments
|
59
|
+
def make_index(template)
|
60
|
+
if RUBY_VERSION >= '2.6'
|
61
|
+
ERB.new(template, trim_mode: '%').result(binding)
|
62
|
+
else
|
63
|
+
# rubocop:disable Lint/ErbNewArguments
|
64
|
+
ERB.new(template, nil, trim_mode: '%').result(binding)
|
65
|
+
# rubocop:enable Lint/ErbNewArguments
|
66
|
+
end
|
67
|
+
end
|
41
68
|
end
|
42
69
|
end
|
43
70
|
end
|
data/lib/r2-oas/errors.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copy From https://github.com/rails/rails/blob/v4.2.5/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
|
4
|
+
|
5
|
+
# rubocop:disable all
|
6
|
+
class Hash
|
7
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
8
|
+
#
|
9
|
+
# h1 = { a: true, b: { c: [1, 2, 3] } }
|
10
|
+
# h2 = { a: false, b: { x: [3, 4, 5] } }
|
11
|
+
#
|
12
|
+
# h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
13
|
+
#
|
14
|
+
# Like with Hash#merge in the standard library, a block can be provided
|
15
|
+
# to merge values:
|
16
|
+
#
|
17
|
+
# h1 = { a: 100, b: 200, c: { c1: 100 } }
|
18
|
+
# h2 = { b: 250, c: { c1: 200 } }
|
19
|
+
# h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
|
20
|
+
# # => { a: 100, b: 450, c: { c1: 300 } }
|
21
|
+
def deep_merge(other_hash, &block)
|
22
|
+
dup.deep_merge!(other_hash, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Same as +deep_merge+, but modifies +self+.
|
26
|
+
def deep_merge!(other_hash, &block)
|
27
|
+
other_hash.each_pair do |current_key, other_value|
|
28
|
+
this_value = self[current_key]
|
29
|
+
|
30
|
+
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
|
31
|
+
this_value.deep_merge(other_value, &block)
|
32
|
+
else
|
33
|
+
if block_given? && key?(current_key)
|
34
|
+
block.call(current_key, this_value, other_value)
|
35
|
+
else
|
36
|
+
other_value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
# rubocop:enable all
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copy From https://github.com/rails/rails/blob/v4.2.5/activesupport/lib/active_support/core_ext/object/blank.rb
|
4
|
+
|
5
|
+
# rubocop:disable all
|
6
|
+
class Object
|
7
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
8
|
+
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
9
|
+
#
|
10
|
+
# This simplifies
|
11
|
+
#
|
12
|
+
# !address || address.empty?
|
13
|
+
#
|
14
|
+
# to
|
15
|
+
#
|
16
|
+
# address.blank?
|
17
|
+
#
|
18
|
+
# @return [true, false]
|
19
|
+
def blank?
|
20
|
+
respond_to?(:empty?) ? !!empty? : !self
|
21
|
+
end
|
22
|
+
|
23
|
+
# An object is present if it's not blank.
|
24
|
+
#
|
25
|
+
# @return [true, false]
|
26
|
+
def present?
|
27
|
+
!blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
31
|
+
# <tt>object.presence</tt> is equivalent to
|
32
|
+
#
|
33
|
+
# object.present? ? object : nil
|
34
|
+
#
|
35
|
+
# For example, something like
|
36
|
+
#
|
37
|
+
# state = params[:state] if params[:state].present?
|
38
|
+
# country = params[:country] if params[:country].present?
|
39
|
+
# region = state || country || 'US'
|
40
|
+
#
|
41
|
+
# becomes
|
42
|
+
#
|
43
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
44
|
+
#
|
45
|
+
# @return [Object]
|
46
|
+
def presence
|
47
|
+
self if present?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class NilClass
|
52
|
+
# +nil+ is blank:
|
53
|
+
#
|
54
|
+
# nil.blank? # => true
|
55
|
+
#
|
56
|
+
# @return [true]
|
57
|
+
def blank?
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class FalseClass
|
63
|
+
# +false+ is blank:
|
64
|
+
#
|
65
|
+
# false.blank? # => true
|
66
|
+
#
|
67
|
+
# @return [true]
|
68
|
+
def blank?
|
69
|
+
true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class TrueClass
|
74
|
+
# +true+ is not blank:
|
75
|
+
#
|
76
|
+
# true.blank? # => false
|
77
|
+
#
|
78
|
+
# @return [false]
|
79
|
+
def blank?
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Array
|
85
|
+
# An array is blank if it's empty:
|
86
|
+
#
|
87
|
+
# [].blank? # => true
|
88
|
+
# [1,2,3].blank? # => false
|
89
|
+
#
|
90
|
+
# @return [true, false]
|
91
|
+
alias_method :blank?, :empty?
|
92
|
+
end
|
93
|
+
|
94
|
+
class Hash
|
95
|
+
# A hash is blank if it's empty:
|
96
|
+
#
|
97
|
+
# {}.blank? # => true
|
98
|
+
# { key: 'value' }.blank? # => false
|
99
|
+
#
|
100
|
+
# @return [true, false]
|
101
|
+
alias_method :blank?, :empty?
|
102
|
+
end
|
103
|
+
|
104
|
+
class String
|
105
|
+
BLANK_REGEXP = /\A[[:space:]]*\z/
|
106
|
+
|
107
|
+
# A string is blank if it's empty or contains whitespaces only:
|
108
|
+
#
|
109
|
+
# ''.blank? # => true
|
110
|
+
# ' '.blank? # => true
|
111
|
+
# "\t\n\r".blank? # => true
|
112
|
+
# ' blah '.blank? # => false
|
113
|
+
#
|
114
|
+
# Unicode whitespace is supported:
|
115
|
+
#
|
116
|
+
# "\u00a0".blank? # => true
|
117
|
+
#
|
118
|
+
# @return [true, false]
|
119
|
+
def blank?
|
120
|
+
BLANK_REGEXP === self
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class Numeric #:nodoc:
|
125
|
+
# No number is blank:
|
126
|
+
#
|
127
|
+
# 1.blank? # => false
|
128
|
+
# 0.blank? # => false
|
129
|
+
#
|
130
|
+
# @return [false]
|
131
|
+
def blank?
|
132
|
+
false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
# rubocop:enable all
|