active_swagger 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1cbd139744bbd64760c651e49b31a701c365bf6279ecec334e60bcf445760a2f
4
+ data.tar.gz: a6139ac97b5df13bab870ca8c58f7d220d24ae6d41550bd5a738ac92b99d232b
5
+ SHA512:
6
+ metadata.gz: 27ca73c5131228aedc74450bf512049812c46cd650630377524093dcf865272516432664a65073b3b4ef364eb5da071189cbec196c451b99c3e6c578015c42a9
7
+ data.tar.gz: c89a11019eeab1fb0a9ac3cd0a109eca67e49e02b7aff577384f3b039a9f91f10fedef5df668ed60f4b201b84e0e6ec2d2bbc3af34247e014ece537ebe3b33cb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 aladac
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # ActiveSwagger
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'active_swagger'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install active_swagger
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'bundler/gem_tasks'
6
+
7
+ require 'rake/testtask'
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'test'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = false
13
+ end
14
+
15
+ task default: :test
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSwagger
4
+ module Definitions
5
+ @registry = {}
6
+
7
+ def self.registry
8
+ @registry
9
+ end
10
+
11
+ def self.add_to_registry(klass, key, name, options = nil)
12
+ set_default_values(klass, key)
13
+
14
+ if options.blank?
15
+ registry[klass.to_s][key] = name
16
+ else
17
+ registry[klass.to_s][key].merge!({ name => options }) unless options.blank?
18
+ end
19
+ end
20
+
21
+ def self.set_default_values(klass, key)
22
+ registry[klass.to_s] ||= {}
23
+ registry[klass.to_s][key] ||= {}
24
+ end
25
+
26
+ def self.add_swagger_atribute(klass, name, options = {})
27
+ options[:type] ||= :string
28
+ options[:type] ||= klass.columns.find { |c| c.name == name.to_s }.sql_type_metadata.type
29
+
30
+ add_to_registry(klass, :schema_attributes, name, options)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSwagger
4
+ module DSL
5
+ extend ActiveSupport::Concern
6
+
7
+ Error = Class.new(StandardError)
8
+
9
+ def swagger_attributes
10
+ output = {}
11
+ self.class.swagger_properties.each_pair do |key, value|
12
+ output[key] = send(key.to_sym)
13
+ output[key] = output[key].to_s if value[:type] == :string
14
+ end
15
+ output
16
+ end
17
+
18
+ class_methods do
19
+ def swagger_schema_name(name = nil)
20
+ Definitions.add_to_registry(self, :schema_name, name)
21
+ end
22
+
23
+ def swagger_attribute(name, options = {})
24
+ Definitions.add_swagger_atribute(self, name, options)
25
+ end
26
+
27
+ def swagger_strong_params
28
+ Params.new(swagger_properties).to_strong_params
29
+ end
30
+
31
+ def swagger_schema
32
+ { type: :object, properties: swagger_properties }
33
+ end
34
+
35
+ def swagger_properties
36
+ class_registry[:schema_attributes]
37
+ end
38
+
39
+ def class_registry
40
+ Definitions.registry[to_s]
41
+ end
42
+
43
+ def schema_name
44
+ class_registry[:schema_name]
45
+ end
46
+
47
+ def swagger_reference
48
+ { '$ref' => "#/components/schemas/#{schema_name}" }
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSwagger
4
+ class Params
5
+ attr_accessor :properties
6
+
7
+ def initialize(properties)
8
+ @properties = properties
9
+ end
10
+
11
+ def mapping(key, value)
12
+ case value[:type]
13
+ when :array
14
+ { key => [] }
15
+ when :object
16
+ { key => {} }
17
+ else
18
+ key
19
+ end
20
+ end
21
+
22
+ def to_strong_params
23
+ params = []
24
+ properties.each_pair do |key, value|
25
+ params.push(mapping(key, value))
26
+ end
27
+ params
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSwagger
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSwagger
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_swagger/version'
4
+ require 'active_swagger/railtie'
5
+ require 'active_swagger/definitions'
6
+ require 'active_swagger/dsl'
7
+ require 'active_swagger/params'
8
+
9
+ module ActiveSwagger; end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :active_swagger do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_swagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aladac
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.4.1
33
+ description: DSL to extend ActiveModel with swagger stuff
34
+ email:
35
+ - adam.ladachowski@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/active_swagger.rb
44
+ - lib/active_swagger/definitions.rb
45
+ - lib/active_swagger/dsl.rb
46
+ - lib/active_swagger/params.rb
47
+ - lib/active_swagger/railtie.rb
48
+ - lib/active_swagger/version.rb
49
+ - lib/tasks/active_swagger_tasks.rake
50
+ homepage: https://github.com/aladac/active_swagger
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ rubygems_mfa_required: 'true'
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '2.7'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.2.22
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: DSL to extend ActiveModel with swagger stuff
74
+ test_files: []