dry-doc 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aee851ee5134074ba0f9278e9dca6df0cdeb4efb
4
+ data.tar.gz: 4a8e6e5a36521355828fd9b096dea9b320bb0c20
5
+ SHA512:
6
+ metadata.gz: 9e9ce1e2dabd2da2c3dd8353ebf7aaa854c33a0450a9594cd623750ba4ab7379cc60532436d69d519f71055aab4e5d945c55bd2a20cd528176a77a80c8bf7fa2
7
+ data.tar.gz: ecda373413a557e0c343bd9a6dce2efe9b95a6cf7edbc7a0f49e4b5a47aaf8395898b0de68095696a969cbb5c158d12cdb2abf73594295b21bcbb59698666073
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jamesdabbs@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dry-doc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 James Dabbs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Dry::Doc
2
+
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/dry/doc`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dry-doc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dry-doc
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jamesdabbs/dry-doc. 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
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dry/doc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dry-doc.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dry/doc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dry-doc"
8
+ spec.version = Dry::Doc::VERSION
9
+ spec.authors = ["James Dabbs"]
10
+ spec.email = ["jamesdabbs@gmail.com"]
11
+
12
+ spec.summary = %q{Dry::Structs with swagger}
13
+ spec.homepage = 'https://github.com/jamesdabbs/dry-doc'
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.14'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_development_dependency 'pry', '~> 0.11'
36
+ spec.add_development_dependency 'simplecov', '~> 0.16'
37
+
38
+ spec.add_runtime_dependency 'dry-struct', '~> 0.3'
39
+ end
@@ -0,0 +1,35 @@
1
+ module Dry::Doc
2
+ module Namespace
3
+ def definitions
4
+ @_definitions ||= []
5
+ end
6
+
7
+ def define name, &config
8
+ klass = Class.new ::Dry::Doc::Value do |c|
9
+ @ref = name
10
+ class_exec &config
11
+ end
12
+ klass.define_singleton_method(:name) { name }
13
+
14
+ const_set name, klass
15
+ definitions.push klass
16
+ klass
17
+ end
18
+
19
+ def types
20
+ ::Dry::Doc::Value::Types
21
+ end
22
+
23
+ def as_open_api
24
+ definitions.freeze
25
+
26
+ defs = definitions.each_with_object({}) do |d, h|
27
+ h[d.name] = d.as_open_api
28
+ end
29
+
30
+ {
31
+ definitions: defs
32
+ }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,116 @@
1
+ class Dry::Doc::Schema
2
+ ::Dry::Doc::UnknownPrimitive = Class.new ::Dry::Doc::NotImplemented
3
+
4
+ Nullable = :"x-nullable"
5
+ T = ::Dry::Doc::Value::Types
6
+
7
+ class Field < ::Dry::Struct
8
+ attribute :type, Object
9
+ attribute :description, T::String.optional
10
+
11
+ def as_json
12
+ ast = type.respond_to?(:to_ast) ? type.to_ast : type
13
+ base = walk_ast(ast, {})
14
+ base[:description] = description if description
15
+ base
16
+ end
17
+
18
+ private
19
+
20
+ BOOL_AST = ::Dry::Doc::Value::Types::Bool.to_ast[1][0 .. -2]
21
+
22
+ def walk_ast ast, acc
23
+ kind, data = ast
24
+
25
+ # TODO: compare with
26
+ # https://github.com/dry-rb/dry-types/blob/d6e50bf54c42dd54ffc0e813738978f58bbfbfa7/lib/dry/types/compiler.rb
27
+ # and see if we can clean this up and cover all the possibilities
28
+ case kind
29
+ when :array
30
+ type, _meta = data
31
+ acc[:type] = 'array'
32
+ acc[:items] = walk_ast type, {}
33
+ return acc
34
+
35
+ when :constrained
36
+ type, *constraints, _meta = data
37
+ # FIXME: note `is?` constraints
38
+ return walk_ast type, acc
39
+
40
+ when :constructor
41
+ definition, _cons, _meta = data
42
+ return walk_ast definition, acc
43
+
44
+ when :definition
45
+ primitive, _meta = data
46
+
47
+ if T.inline? primitive
48
+ return acc.merge primitive.as_open_api
49
+ elsif primitive.respond_to?(:ref) && primitive.ref
50
+ return acc.merge ref: primitive.ref
51
+ elsif primitive == Integer
52
+ return acc.merge type: :integer
53
+ elsif primitive == String
54
+ return acc.merge type: :string
55
+ elsif primitive == NilClass
56
+ return acc.merge type: nil
57
+ elsif primitive == DateTime
58
+ return acc.merge type: :string, format: :'date-time'
59
+ elsif primitive == Date
60
+ return acc.merge type: :string, format: :date
61
+ else
62
+ # TODO: allow plugins for handling custom types
63
+ # :nocov:
64
+ raise ::Dry::Doc::UnknownPrimitive, primitive
65
+ # :nocov:
66
+ end
67
+
68
+ when :sum
69
+ *nodes, _meta = data
70
+
71
+ if nodes == BOOL_AST
72
+ return acc.merge type: :boolean
73
+ end
74
+
75
+ types = nodes.map { |i| walk_ast i, {} }
76
+ nils, non_nils = types.partition { |t| t.key?(:type) && t[:type].nil? }
77
+ if nils.length == 1 && non_nils.length == 1
78
+ acc[:'x-nullable'] = true
79
+ acc = acc.merge non_nils.first
80
+ else
81
+ acc = acc.merge oneOf: non_nils
82
+ end
83
+ return acc
84
+
85
+ when :enum
86
+ inner, _meta = data
87
+ acc = walk_ast inner, acc
88
+ acc[:values] = self.type.values # FIXME: this needs to look at the AST
89
+ return acc
90
+ end
91
+
92
+ # :nocov:
93
+ raise ::Dry::Doc::NotImplemented, "AST kind: #{kind} | #{data}"
94
+ # :nocov:
95
+ end
96
+ end
97
+
98
+ attr_reader :klass
99
+
100
+ def initialize klass
101
+ @klass, @properties = klass, {}
102
+ end
103
+
104
+ def register name, type, description:
105
+ @properties[name] = Field.new \
106
+ type: type,
107
+ description: description
108
+ end
109
+
110
+ def as_json
111
+ {
112
+ type: :object,
113
+ properties: @properties.transform_values(&:as_json)
114
+ }
115
+ end
116
+ end
@@ -0,0 +1,79 @@
1
+ class Dry::Doc::Value < ::Dry::Struct
2
+ T = ::Dry::Types.module
3
+ Types = ::Dry::Doc::Value::T::Strict
4
+
5
+ module Types
6
+ Inlines = Set.new
7
+
8
+ def self.instance klass
9
+ ::Dry::Doc::Value::T::Constructor klass
10
+ end
11
+
12
+ def self.constant value
13
+ ::Dry::Doc::Value::T::Constant value
14
+ end
15
+
16
+ def self.sum left, right
17
+ left = instance left unless left.respond_to? :to_ast
18
+ right = instance right unless right.respond_to? :to_ast
19
+ ::Dry::Types::Sum.new left, right
20
+ end
21
+
22
+ def self.[] type
23
+ ::Dry::Doc::Value::Types::Array.member type
24
+ end
25
+
26
+ def self.inline? klass
27
+ ::Dry::Doc::Value::Types::Inlines.include? klass
28
+ end
29
+ end
30
+
31
+ class << self
32
+ def doc
33
+ @_doc ||= ::Dry::Doc::Schema.new self
34
+ end
35
+
36
+ def types
37
+ ::Dry::Doc::Value::Types
38
+ end
39
+
40
+ def as_open_api
41
+ doc.as_json
42
+ end
43
+
44
+ def ref
45
+ "#/definitions/#{@ref}" if @ref
46
+ end
47
+
48
+ def attribute name, type=nil, opts={}, &nested
49
+ if nested
50
+ opts = type || {}
51
+ inline_class = build_type :"#{self.name}::#{name}", &nested
52
+ ::Dry::Doc::Value::Types::Inlines.add inline_class
53
+ type = types.instance inline_class
54
+ type = type.optional if opts[:optional]
55
+ end
56
+
57
+ doc.register name, type,
58
+ description: opts.delete(:description)
59
+ super name, type
60
+ end
61
+
62
+ def inspect
63
+ "<#{name}>"
64
+ end
65
+
66
+ private
67
+
68
+ def build_type name, &config
69
+ Class.new ::Dry::Doc::Value do |klass|
70
+ class_exec &config
71
+ define_singleton_method(:name) { name }
72
+ end
73
+ end
74
+ end
75
+
76
+ def as_json *_
77
+ to_h
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module Dry
2
+ module Doc
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
data/lib/dry/doc.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'dry-struct'
2
+
3
+ require 'dry/doc/version'
4
+
5
+ module Dry
6
+ module Doc
7
+ NotImplemented = Class.new ::NotImplementedError
8
+ end
9
+ end
10
+
11
+ require 'dry/doc/value'
12
+ require 'dry/doc/schema'
13
+ require 'dry/doc/namespace'
14
+
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Dabbs
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dry-struct
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ description:
98
+ email:
99
+ - jamesdabbs@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - dry-doc.gemspec
115
+ - lib/dry/doc.rb
116
+ - lib/dry/doc/namespace.rb
117
+ - lib/dry/doc/schema.rb
118
+ - lib/dry/doc/value.rb
119
+ - lib/dry/doc/version.rb
120
+ homepage: https://github.com/jamesdabbs/dry-doc
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.8
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Dry::Structs with swagger
144
+ test_files: []