schemable 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -1
- data/Gemfile +5 -5
- data/Gemfile.lock +82 -50
- data/README.md +324 -100
- data/lib/generators/schemable/install_generator.rb +3 -13
- data/lib/generators/schemable/model_generator.rb +1 -16
- data/lib/schemable/attribute_schema_generator.rb +130 -0
- data/lib/schemable/configuration.rb +114 -0
- data/lib/schemable/definition.rb +322 -0
- data/lib/schemable/included_schema_generator.rb +107 -0
- data/lib/schemable/relationship_schema_generator.rb +119 -0
- data/lib/schemable/request_schema_generator.rb +88 -0
- data/lib/schemable/response_schema_generator.rb +124 -0
- data/lib/schemable/schema_modifier.rb +248 -0
- data/lib/schemable/version.rb +1 -1
- data/lib/schemable.rb +60 -927
- data/lib/templates/schemable.rb +76 -0
- data/schemable.gemspec +2 -5
- data/sig/schemable/attribute_schema_generator.rbs +13 -0
- data/sig/schemable/configuration.rbs +21 -0
- data/sig/schemable/definition.rbs +32 -0
- data/sig/schemable/included_schema_generator.rbs +11 -0
- data/sig/schemable/relationship_schema_generator.rbs +11 -0
- data/sig/schemable/request_schema_generator.rbs +10 -0
- data/sig/schemable/response_schema_generator.rbs +13 -0
- data/sig/schemable/schema_modifier.rbs +9 -0
- data/sig/schemable.rbs +4 -1
- metadata +23 -36
- data/lib/templates/common_definitions.rb +0 -13
- data/lib/templates/serializers_helper.rb +0 -7
@@ -0,0 +1,76 @@
|
|
1
|
+
Schemable.configure do |config|
|
2
|
+
# The following options are available for configuration.
|
3
|
+
# If you do not specify a configuration option, then its default value will be used.
|
4
|
+
# To configure them, uncomment them and set them to the desired value.
|
5
|
+
|
6
|
+
# The ORM options are :active_record, :mongoid
|
7
|
+
#
|
8
|
+
# config.orm = :active_record
|
9
|
+
|
10
|
+
# The gem uses `{ type: :number, format: :float }` for float attributes by default.
|
11
|
+
# If you want to use `{ type: :string }` instead, set this option to true.
|
12
|
+
#
|
13
|
+
# config.float_as_string = false
|
14
|
+
|
15
|
+
# The gem uses `{ type: :number, format: :decimal }` for decimal attributes by default.
|
16
|
+
# If you want to use `{ type: :string }` instead, set this option to true.
|
17
|
+
#
|
18
|
+
# config.decimal_as_string = false
|
19
|
+
|
20
|
+
# The gem by default sets the pagination_enabled option to true
|
21
|
+
# which means in the meta section of the response schema
|
22
|
+
# it will add the pagination links and the total count
|
23
|
+
# if you don't want to have the pagination links and the total count
|
24
|
+
# in the meta section of the response schema, set this option to false
|
25
|
+
# If you want to define your own meta schema, you can set the custom_meta_response_schema option
|
26
|
+
#
|
27
|
+
# config.pagination_enabled = true
|
28
|
+
#
|
29
|
+
# config.custom_meta_response_schema = nil
|
30
|
+
|
31
|
+
# The gem allows for custom defined schema for a specific type
|
32
|
+
# for example if you wish to have all your arrays have the schema
|
33
|
+
# { type: :array, items: { type: string } } then use the below method to add to custom_type_mappers
|
34
|
+
#
|
35
|
+
# config.add_custom_type_mapper(:array, { type: :array, items: { type: string } })
|
36
|
+
|
37
|
+
# If you have a custom enum method defined on all of your model, you can set it here
|
38
|
+
# for example if you have a method called `base_attributes` on all of your models
|
39
|
+
# and you use that method to return an array of symbols that are the attributes
|
40
|
+
# to be serialized then you can set the below to `base_attributes`
|
41
|
+
#
|
42
|
+
# config.infer_attributes_from_custom_method = nil
|
43
|
+
|
44
|
+
# If you want to get the list of attributes from the jsonapi-rails gem's
|
45
|
+
# JSONAPI::Serializable::Resource class, set this option to true.
|
46
|
+
# It uses the attribute_blocks method to get the list of attributes.
|
47
|
+
#
|
48
|
+
# config.infer_attributes_from_jsonapi_serializable = false
|
49
|
+
|
50
|
+
# Sometimes you may have virtual attributes that are not in the database
|
51
|
+
# Generating the schema for these attributes will fail, in that case you can
|
52
|
+
# add your logic to return an instance of the model that is serialized in
|
53
|
+
# jsonapi format and the gem will use that to generate the schema
|
54
|
+
# this is useful if you use factory_bot and jsonapi-rails to generate the instance
|
55
|
+
# check the commented out code in the definition template for an example
|
56
|
+
# Set this option to true to enable this feature
|
57
|
+
#
|
58
|
+
# config.use_serialized_instance = false
|
59
|
+
|
60
|
+
# By default the gem uses activerecord's defined_enums method to get the enums
|
61
|
+
# with their keys and values, if you don't have this method defined on your model
|
62
|
+
# then please set the below option to the name of the method that returns the
|
63
|
+
# enums with their keys and values as a hash. This will handle the auto generation
|
64
|
+
# of the enum schema for you, with correct values.
|
65
|
+
#
|
66
|
+
# config.custom_defined_enum_method = nil
|
67
|
+
|
68
|
+
# If you use mongoid and simple_enum gem, you can set the below options to the prefix and suffix
|
69
|
+
# Since simple_enum uses the prefix and suffix to generate the enum methods, and the fields' names
|
70
|
+
# are usually the enum name with the prefix and suffix, the gem will remove the prefix and suffix
|
71
|
+
# from the field name to get the enum name and then use that to get the enum values
|
72
|
+
#
|
73
|
+
# config.enum_prefix_for_simple_enum = nil
|
74
|
+
#
|
75
|
+
# config.enum_suffix_for_simple_enum = nil
|
76
|
+
end
|
data/schemable.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ['Muhammad Nawzad']
|
9
9
|
spec.email = ['hama127n@gmail.com']
|
10
10
|
|
11
|
-
spec.summary = 'An
|
12
|
-
spec.description = "The schemable gem is an
|
11
|
+
spec.summary = 'An opinionated Gem for Rails applications to auto generate schema in JSONAPI format.'
|
12
|
+
spec.description = "The schemable gem is an opinionated Gem for Rails applications to auto generate schema for models in JSONAPI format. It is designed to work with rswag's swagger documentation since it can generate the schemas for it."
|
13
13
|
spec.homepage = 'https://github.com/muhammadnawzad/schemable'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
spec.required_ruby_version = '>= 3.1.2'
|
@@ -29,8 +29,5 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ['lib']
|
31
31
|
|
32
|
-
spec.add_dependency 'factory_bot_rails', '~> 6.2.0'
|
33
|
-
spec.add_dependency 'jsonapi-rails', '~> 0.4.1'
|
34
|
-
|
35
32
|
spec.metadata['rubygems_mfa_required'] = 'true'
|
36
33
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Schemable
|
2
|
+
class AttributeSchemaGenerator
|
3
|
+
attr_reader model: Class
|
4
|
+
attr_reader model_definition: Definition
|
5
|
+
attr_reader configuration: Configuration
|
6
|
+
attr_reader response: Hash[Symbol, any]?
|
7
|
+
attr_reader schema_modifier: SchemaModifier
|
8
|
+
|
9
|
+
def initialize: (Definition) -> void
|
10
|
+
def generate: -> (Hash[Symbol, any] | Array[any])
|
11
|
+
def generate_attribute_schema: (Symbol) -> Hash[Symbol, any]
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Schemable
|
2
|
+
class Configuration
|
3
|
+
attr_accessor orm: Symbol
|
4
|
+
attr_accessor float_as_string: bool
|
5
|
+
attr_accessor decimal_as_string: bool
|
6
|
+
attr_accessor pagination_enabled: bool
|
7
|
+
attr_accessor use_serialized_instance: bool
|
8
|
+
attr_accessor custom_defined_enum_method: Symbol?
|
9
|
+
attr_accessor enum_prefix_for_simple_enum: String?
|
10
|
+
attr_accessor enum_suffix_for_simple_enum: String?
|
11
|
+
attr_accessor custom_type_mappers: Hash[Symbol, any]
|
12
|
+
attr_accessor infer_attributes_from_custom_method: Symbol?
|
13
|
+
attr_accessor custom_meta_response_schema: Hash[Symbol, any]?
|
14
|
+
attr_accessor infer_attributes_from_jsonapi_serializable: bool
|
15
|
+
|
16
|
+
|
17
|
+
def initialize: -> void
|
18
|
+
def type_mapper: (Symbol) -> Hash[Symbol, any]
|
19
|
+
def add_custom_type_mapper: (Symbol, Hash[Symbol, any]) -> void
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Schemable
|
2
|
+
class Definition
|
3
|
+
attr_reader configuration: Configuration
|
4
|
+
attr_writer relationships: Hash[Symbol, any]
|
5
|
+
attr_writer additional_create_request_attributes: Hash[Symbol, any]
|
6
|
+
attr_writer additional_update_request_attributes: Hash[Symbol, any]
|
7
|
+
|
8
|
+
def model: -> Class
|
9
|
+
def initialize: -> void
|
10
|
+
def serializer: -> Class?
|
11
|
+
def model_name: -> String
|
12
|
+
def attributes: -> Array[Symbol]
|
13
|
+
def array_types: -> Hash[Symbol, any]
|
14
|
+
def relationships: -> Hash[Symbol, any]
|
15
|
+
def nullable_attributes: -> Array[Symbol]
|
16
|
+
def serialized_instance: -> Hash[Symbol, any]
|
17
|
+
def self.generate: -> Array[Hash[Symbol, any]]
|
18
|
+
def excluded_response_included: -> Array[Symbol]
|
19
|
+
def excluded_response_relations: -> Array[Symbol]
|
20
|
+
def excluded_response_attributes: -> Array[Symbol]
|
21
|
+
def additional_response_included: -> Hash[Symbol, any]
|
22
|
+
def additional_response_relations: -> Hash[Symbol, any]
|
23
|
+
def additional_response_attributes: -> Hash[Symbol, any]
|
24
|
+
def excluded_create_request_attributes: -> Array[Symbol]
|
25
|
+
def excluded_update_request_attributes: -> Array[Symbol]
|
26
|
+
def optional_create_request_attributes: -> Array[Symbol]
|
27
|
+
def optional_update_request_attributes: -> Array[Symbol]
|
28
|
+
def additional_create_request_attributes: -> Hash[Symbol, any]
|
29
|
+
def additional_update_request_attributes: -> Hash[Symbol, any]
|
30
|
+
def camelize_keys: (Hash[Symbol, any]) -> (Array[Hash[Symbol, any]] | Hash[Symbol, any])
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Schemable
|
2
|
+
class IncludedSchemaGenerator
|
3
|
+
attr_reader model_definition: Definition
|
4
|
+
attr_reader schema_modifier: SchemaModifier
|
5
|
+
attr_reader relationships: Hash[Symbol, any]
|
6
|
+
|
7
|
+
def initialize: (Definition) -> void
|
8
|
+
def generate: (?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> (Hash[Symbol, any])
|
9
|
+
def prepare_schema_for_included: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> Hash[Symbol, any]
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Schemable
|
2
|
+
class RelationshipSchemaGenerator
|
3
|
+
attr_reader model_definition: Definition
|
4
|
+
attr_reader schema_modifier: SchemaModifier
|
5
|
+
attr_reader relationships: Hash[Symbol, any]
|
6
|
+
|
7
|
+
def initialize: (Definition) -> void
|
8
|
+
def generate_schema: (String, ?collection: bool) -> Hash[Symbol, any]
|
9
|
+
def generate: (?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> (Hash[Symbol, any])
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Schemable
|
2
|
+
class RequestSchemaGenerator
|
3
|
+
attr_reader model_definition: Definition
|
4
|
+
attr_reader schema_modifier: SchemaModifier
|
5
|
+
|
6
|
+
def initialize: (Definition) -> void
|
7
|
+
def generate_for_create: () -> (Hash[Symbol, any])
|
8
|
+
def generate_for_update: () -> (Hash[Symbol, any])
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Schemable
|
2
|
+
class ResponseSchemaGenerator
|
3
|
+
attr_reader model: Class
|
4
|
+
attr_reader model_definition: Definition
|
5
|
+
attr_reader schema_modifier: SchemaModifier
|
6
|
+
attr_reader configuration: Configuration
|
7
|
+
|
8
|
+
def initialize: (Definition) -> void
|
9
|
+
def meta: -> Hash[Symbol, any]
|
10
|
+
def jsonapi: -> Hash[Symbol, any]
|
11
|
+
def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool) -> Hash[Symbol, any]
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Schemable
|
2
|
+
class SchemaModifier
|
3
|
+
def parse_path: (path: String) -> Array[Symbol]
|
4
|
+
def path_exists?: (schema: Hash[Symbol, any], path: String) -> bool
|
5
|
+
def deep_merge_hashes: (destination: Hash[Symbol, any], new_data: Hash[Symbol, any]) -> (Hash[Symbol, any])
|
6
|
+
def add_properties: (original_schema: (Hash[Symbol, any]), new_schema: Hash[Symbol, any], path: String) -> (Hash[Symbol, any])
|
7
|
+
def delete_properties: (original_schema: (Hash[Symbol, any]), path: String) -> (Hash[Symbol, any])
|
8
|
+
end
|
9
|
+
end
|
data/sig/schemable.rbs
CHANGED
metadata
CHANGED
@@ -1,44 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schemable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Muhammad Nawzad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
name: factory_bot_rails
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 6.2.0
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 6.2.0
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: jsonapi-rails
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.4.1
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 0.4.1
|
41
|
-
description: The schemable gem is an opiniated Gem for Rails applications to auto
|
11
|
+
date: 2023-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The schemable gem is an opinionated Gem for Rails applications to auto
|
42
14
|
generate schema for models in JSONAPI format. It is designed to work with rswag's
|
43
15
|
swagger documentation since it can generate the schemas for it.
|
44
16
|
email:
|
@@ -59,11 +31,26 @@ files:
|
|
59
31
|
- lib/generators/schemable/install_generator.rb
|
60
32
|
- lib/generators/schemable/model_generator.rb
|
61
33
|
- lib/schemable.rb
|
34
|
+
- lib/schemable/attribute_schema_generator.rb
|
35
|
+
- lib/schemable/configuration.rb
|
36
|
+
- lib/schemable/definition.rb
|
37
|
+
- lib/schemable/included_schema_generator.rb
|
38
|
+
- lib/schemable/relationship_schema_generator.rb
|
39
|
+
- lib/schemable/request_schema_generator.rb
|
40
|
+
- lib/schemable/response_schema_generator.rb
|
41
|
+
- lib/schemable/schema_modifier.rb
|
62
42
|
- lib/schemable/version.rb
|
63
|
-
- lib/templates/
|
64
|
-
- lib/templates/serializers_helper.rb
|
43
|
+
- lib/templates/schemable.rb
|
65
44
|
- schemable.gemspec
|
66
45
|
- sig/schemable.rbs
|
46
|
+
- sig/schemable/attribute_schema_generator.rbs
|
47
|
+
- sig/schemable/configuration.rbs
|
48
|
+
- sig/schemable/definition.rbs
|
49
|
+
- sig/schemable/included_schema_generator.rbs
|
50
|
+
- sig/schemable/relationship_schema_generator.rbs
|
51
|
+
- sig/schemable/request_schema_generator.rbs
|
52
|
+
- sig/schemable/response_schema_generator.rbs
|
53
|
+
- sig/schemable/schema_modifier.rbs
|
67
54
|
homepage: https://github.com/muhammadnawzad/schemable
|
68
55
|
licenses:
|
69
56
|
- MIT
|
@@ -88,9 +75,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
75
|
- !ruby/object:Gem::Version
|
89
76
|
version: '0'
|
90
77
|
requirements: []
|
91
|
-
rubygems_version: 3.4.
|
78
|
+
rubygems_version: 3.4.10
|
92
79
|
signing_key:
|
93
80
|
specification_version: 4
|
94
|
-
summary: An
|
81
|
+
summary: An opinionated Gem for Rails applications to auto generate schema in JSONAPI
|
95
82
|
format.
|
96
83
|
test_files: []
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module SwaggerDefinitions
|
2
|
-
module CommonDefinitions
|
3
|
-
def self.aggregate
|
4
|
-
[
|
5
|
-
# Import definitions like this:
|
6
|
-
# Swagger::Definitions::Model.definitions
|
7
|
-
|
8
|
-
# Make sure in swagger_helper.rb's components section you have:
|
9
|
-
# schemas: SwaggerDefinitions::CommonDefinitions.aggregate
|
10
|
-
].flatten.reduce({}, :merge)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|