r2-oas 0.2.0 → 0.3.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/CHANGELOG.md +41 -0
- data/GEMSPEC.md +19 -22
- data/README.ja.md +61 -0
- data/README.md +60 -0
- data/lib/r2-oas.rb +2 -4
- data/lib/r2-oas/app_configuration.rb +1 -1
- data/lib/r2-oas/deploy/client.rb +33 -6
- 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/schema/editor.rb +13 -2
- data/lib/r2-oas/schema/ui.rb +0 -1
- data/lib/r2-oas/task_logging.rb +0 -4
- data/lib/r2-oas/tasks/common.rake +0 -1
- data/lib/r2-oas/tasks/tool.rake +9 -2
- data/lib/r2-oas/version.rb +1 -1
- data/r2-oas.gemspec +12 -13
- metadata +61 -78
- 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 -249
- data/.travis.yml +0 -24
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -224
- 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 -29
- 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 -176
- 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 -412
- 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 -181
- data/docs/usage/use_tag_namespace.md +0 -180
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74e2483dd3c1980c894757819275ef05b794820d5aebe22b08bf737df2f31c63
|
4
|
+
data.tar.gz: 52b4b0abf260d18852cdaad9ea1d7b70aa76cc63e15a7793e528b8769e715b94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23f55926a604e6f49424294867e183a4cb4d1dc0a5b6a0e3fa9751eb56bc667477868a56436a08a3c3855939f3d9b8959c56ca7d5496086be6ffe8b2f45a06bc
|
7
|
+
data.tar.gz: 799eae1d0bf672a6615cb8917b4aa633b92cb01df314adbfd00cab520176dff47b97be49316430e1b2c51bf0083f114331f6bcb48c5cab14837985b910846ace
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,46 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## v0.3.4
|
4
|
+
|
5
|
+
2020-07-11
|
6
|
+
|
7
|
+
- [`Other`] Modify gemspec `files` fields ([#147](https://github.com/yukihirop/r2-oas/pull/147))
|
8
|
+
- Add `r2-oas.gemspec` into gemspec `files`
|
9
|
+
|
10
|
+
## v0.3.3
|
11
|
+
|
12
|
+
2020-07-11
|
13
|
+
|
14
|
+
- [`FixBugs`] A cute pet store syndrome 🐈 ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
15
|
+
- [`FixBugs`] R2OAS.logger.level does not work ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
16
|
+
- [`Breaking`] Change default namespace type from `:underbar` to `:dot` ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
17
|
+
- [`Other`] Modify gemspec `files` fields ([f3f4c30](https://github.com/yukihirop/r2-oas/pull/144))
|
18
|
+
|
19
|
+
See milestone [v0.3.3](https://github.com/yukihirop/r2-oas/milestone/3?closed=1)
|
20
|
+
|
21
|
+
## v0.3.2
|
22
|
+
|
23
|
+
2020-07-05
|
24
|
+
|
25
|
+
- [`Feature`] The file size at the time of gem install was reduced by about 10MB. ([dbdbce9 ](https://github.com/yukihirop/r2-oas/pull/138))
|
26
|
+
|
27
|
+
## v0.3.1
|
28
|
+
|
29
|
+
2020-06-07
|
30
|
+
|
31
|
+
- [`Breaking`] Remove unnecessary runtime dependencies ([384ea1a](https://github.com/yukihirop/r2-oas/pull/132))
|
32
|
+
|
33
|
+
## v0.3.0
|
34
|
+
|
35
|
+
2020-05-30
|
36
|
+
|
37
|
+
- [`Feature/Breaking`] Support Ruby 2.7 🎉 ([931ec4b](https://github.com/yukihirop/r2-oas/pull/122))
|
38
|
+
|
39
|
+
- Remove `Gemfile.lock`
|
40
|
+
|
41
|
+
- [`Developer`] Create script to test all support ruby in development ([8d0df98](https://github.com/yukihirop/r2-oas/pull/124))
|
42
|
+
- [`Docs`] Add docs about Trableshouting ([f4a782f](https://github.com/yukihirop/r2-oas/pull/125))
|
43
|
+
|
3
44
|
## v0.2.0
|
4
45
|
|
5
46
|
2020-05-02
|
data/GEMSPEC.md
CHANGED
@@ -1,23 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
$ bundle exec rake routes:oas:
|
15
|
-
$ bundle exec rake routes:oas:
|
16
|
-
$ bundle exec rake routes:oas:
|
17
|
-
$ bundle exec rake routes:oas:
|
18
|
-
|
19
|
-
|
20
|
-
$ bundle exec rake routes:oas:analyze # analyze
|
21
|
-
$ bundle exec rake routes:oas:deploy # deploy
|
22
|
-
|
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
|
+
|
23
20
|
Happy Coding ❗️
|
data/README.ja.md
CHANGED
@@ -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
@@ -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,8 +22,6 @@ 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'
|
29
27
|
autoload :Builder, 'r2-oas/schema/builder'
|
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
|
@@ -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
|