yard-dry-initializer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 39a09f1722d473bcf5146568ceb2d575ed5927e5b05f4fc0255bda963beeab75
4
+ data.tar.gz: 9cffcac3f30db2d90be738c157c107608b846d4a5257993940a52e656c8563d1
5
+ SHA512:
6
+ metadata.gz: 6f969d2dbb74c05ba12061852684e50b4df64c9a392b768f0214365c46629dbd20e5d19345bbdf98a80a7888305cd37e2df6be317b825fd0db65320f48dfc632
7
+ data.tar.gz: 2749bb64f7e46d9af4d9f142168c7fd2040689e9b71ec39602c40183b1a04614392b1c877527e4dbddb7a9d6ca0c9bbe552687971e1b8a8cd9e7a9db99cb2f37
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayCopNames: true
4
+
5
+ Metrics/LineLength:
6
+ Max: 100
7
+ # To make it possible to copy or click on URIs in the code, we allow lines
8
+ # contaning a URI to be longer than Max.
9
+ AllowURI: true
10
+ URISchemes:
11
+ - http
12
+ - https
13
+ Enabled: true
14
+
15
+ Metrics/AbcSize:
16
+ Max: 20
17
+
18
+ Metrics/MethodLength:
19
+ Max: 25
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - "spec/**/*.*"
24
+ - "*.gemspec"
25
+
26
+ Style/TrailingCommaInArguments:
27
+ Description: 'Checks for trailing comma in argument lists.'
28
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-params-comma'
29
+ Enabled: true
30
+ EnforcedStyleForMultiline: consistent_comma
31
+
32
+ Style/TrailingCommaInArrayLiteral:
33
+ Description: 'Checks for trailing comma in array literals.'
34
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
35
+ Enabled: true
36
+ EnforcedStyleForMultiline: consistent_comma
37
+
38
+ Style/TrailingCommaInHashLiteral:
39
+ Description: 'Checks for trailing comma in hash literals.'
40
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
41
+ Enabled: true
42
+ EnforcedStyleForMultiline: consistent_comma
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ - 2.4.4
6
+ - 2.3.7
7
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in yard-dry-initializer.gemspec
8
+ gemspec
9
+
10
+ group :development do
11
+ gem 'pry'
12
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Andrey Novikov
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.
@@ -0,0 +1,68 @@
1
+ # YARD plugin for Dry::Initializer
2
+
3
+ The [dry-initializer] gem is awesome. It makes defining dependencies of your classes so convenient and declarative.
4
+
5
+ But when you're using these classses, how to get know which positional arguments does it expect and in which order and which keyword ones?
6
+
7
+ It's easy (but bother) to look into source code, but in case of using inheritance it will be very cumbersome.
8
+
9
+ This YARD plugin to the rescue!
10
+
11
+ <a href="https://evilmartians.com/?utm_source=yard-dry-initializer&utm_campaign=project_page"><img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
12
+
13
+ Write this:
14
+
15
+ ```
16
+ class Base
17
+ extend Dry::Initializer
18
+
19
+ param :account, comment: "End user account on behalf of which all actions are done"
20
+ option :force, default: -> { false }, comment: "Execute action regardless of pre-checks result"
21
+ end
22
+
23
+ class ProcessSubject < Base
24
+ param :subject
25
+ option :filter, comment: "Callable object, given +subject+ will allow processing if returns true"
26
+ option :force, default: -> { true }
27
+ end
28
+ ```
29
+
30
+ And you will get this:
31
+
32
+ ![Example of generated docs for ProcessSubject class](generated_docs_example.png)
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'yard-dry-initializer'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Add this line in your `.yardopts`:
47
+
48
+ --plugin dry-initializer
49
+
50
+ ## Usage
51
+
52
+ Use YARD as usual, enjoy new info in your docs.
53
+
54
+ If you're getting `[error]: Error loading plugin 'yard-dry-initializer'` error you may need to invoke `yard` via `bundle exec` or generate binstubs for it.
55
+
56
+ ## Development
57
+
58
+ 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.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Envek/yard-dry-initializer.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'yard/dry/initializer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -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
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard/dry/initializer'
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard'
4
+ require 'yard/dry/initializer/param_handler'
5
+ require 'yard/dry/initializer/option_handler'
6
+ require 'yard/dry/initializer/version'
7
+
8
+ module YARD
9
+ module Dry
10
+ module Initializer
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YARD
4
+ module Dry
5
+ module Initializer
6
+ module CommonHandler
7
+ def process
8
+ return unless reader?
9
+
10
+ # Define reader method
11
+ object = YARD::CodeObjects::MethodObject.new(namespace, reader_name)
12
+ register(object)
13
+ object.visibility = reader_visibility
14
+ object.dynamic = true
15
+ doc = "Reader method for the +#{definition_name}+ initializer parameter."
16
+ object.docstring = [comment, doc].compact.join("\n\n")
17
+
18
+ # Register as attribute unless it is already registered
19
+ return if namespace.attributes[:instance].key?(reader_name.to_sym)
20
+ namespace.attributes[:instance][reader_name.to_sym] = { read: object }
21
+ end
22
+
23
+ protected
24
+
25
+ def constructor
26
+ @constructor ||= begin
27
+ existing = namespace.meths.find(&:constructor?)
28
+ return create_constructor unless existing
29
+ return existing if existing.namespace == namespace
30
+ copy_parent_constructor(existing)
31
+ end
32
+ end
33
+
34
+ def create_constructor
35
+ YARD::CodeObjects::MethodObject.new(namespace, :initialize)
36
+ end
37
+
38
+ def copy_parent_constructor(existing_constructor)
39
+ YARD::CodeObjects::MethodObject.new(namespace, :initialize) do |new_constructor|
40
+ existing_constructor.copy_to(new_constructor)
41
+
42
+ # To allow replace arguments independent of parent
43
+ new_constructor.parameters = existing_constructor.parameters.map(&:dup)
44
+ # new_constructor.add_tag(*existing_constructor.tags.map(&:dup))
45
+ end
46
+ end
47
+
48
+ def definition_name
49
+ statement.parameters.first.jump(:tstring_content, :ident).source
50
+ end
51
+
52
+ def options
53
+ raw_options = statement.parameters[2] || statement.parameters[1]
54
+ return {} unless raw_options
55
+ raw_options.map do |o|
56
+ [o.first.jump(:tstring_content, :ident).source, o[1]]
57
+ end.to_h
58
+ end
59
+
60
+ def reader?
61
+ return true unless options.key?('reader:')
62
+ options['reader:'].jump(:tstring_content, :ident).source != 'false'
63
+ end
64
+
65
+ def reader_name
66
+ if options.key?('as:')
67
+ options['as:'].jump(:tstring_content, :ident).source
68
+ else
69
+ definition_name
70
+ end
71
+ end
72
+
73
+ def reader_visibility
74
+ return :public unless options.key?('reader:')
75
+ options['reader:'].jump(:tstring_content, :ident).source.to_sym
76
+ end
77
+
78
+ def comment
79
+ return nil unless options.key?('comment:')
80
+ options['comment:'].jump(:tstring_content, :ident).source
81
+ end
82
+
83
+ def default_string
84
+ return options['default:'][1].source if options.key?('default:')
85
+ 'nil' if options.key?('optional:') && options['optional:'].jump(:tstring_content, :ident).source == 'true'
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard/dry/initializer/common_handler'
4
+
5
+ module YARD
6
+ module Dry
7
+ module Initializer
8
+ class OptionHandler < ::YARD::Handlers::Ruby::Base
9
+ include CommonHandler
10
+
11
+ handles method_call(:option)
12
+ namespace_only
13
+
14
+ def process
15
+ super
16
+
17
+ unless constructor.tags('param').find { |t| t.name == 'options' }
18
+ constructor.parameters << ['**options', nil]
19
+ constructor.add_tag(YARD::Tags::Tag.new(:param, nil, ['Hash'], 'options'))
20
+ end
21
+
22
+ existing_tag = constructor.tags('option').find { |t| t.pair.name == definition_name }
23
+ comment = self.comment || existing_tag&.pair&.text
24
+ default = default_string && [default_string] || existing_tag&.pair&.defaults
25
+ option = YARD::Tags::DefaultTag.new(:option, comment, nil, definition_name, default)
26
+ if existing_tag
27
+ existing_tag.pair = option
28
+ else
29
+ constructor.add_tag(YARD::Tags::OptionTag.new(:option, 'options', option))
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard/dry/initializer/common_handler'
4
+
5
+ module YARD
6
+ module Dry
7
+ module Initializer
8
+ class ParamHandler < ::YARD::Handlers::Ruby::Base
9
+ include CommonHandler
10
+
11
+ handles method_call(:param)
12
+ namespace_only
13
+
14
+ def process
15
+ super
16
+
17
+ add_constructor_param
18
+ add_constructor_tag
19
+ end
20
+
21
+ protected
22
+
23
+ def add_constructor_param
24
+ existing_index = constructor.parameters.index do |name, _default|
25
+ name == definition_name
26
+ end
27
+ if existing_index
28
+ constructor.parameters[existing_index] = [definition_name, default_string]
29
+ else
30
+ constructor.parameters.insert(last_param_index, [definition_name, default_string])
31
+ end
32
+ end
33
+
34
+ def last_param_index
35
+ return -1 if constructor.parameters.empty?
36
+ constructor.parameters.last.first.start_with?('**') ? -2 : -1
37
+ end
38
+
39
+ def add_constructor_tag
40
+ if existing_tag
41
+ existing_tag.text = comment
42
+ existing_tag.instance_variable_set(:@defaults, default_string && [default_string])
43
+ else
44
+ param = YARD::Tags::DefaultTag.new(:param, comment, nil, definition_name, [default_string])
45
+ constructor.add_tag(param)
46
+ end
47
+ end
48
+
49
+ def existing_tag
50
+ @existing_tag ||=
51
+ constructor.tags.find do |tag|
52
+ tag.name == definition_name && tag.tag_name == 'param'
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YARD
4
+ module Dry
5
+ module Initializer
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'yard/dry/initializer/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'yard-dry-initializer'
9
+ spec.version = YARD::Dry::Initializer::VERSION
10
+ spec.authors = ['Andrey Novikov']
11
+ spec.email = ['envek@envek.name']
12
+
13
+ spec.summary = 'Generates documentation for your params and options'
14
+ spec.description = <<~MSG
15
+ Generates for you all YARD declarations of attributes
16
+ and initializer parameters created by dry-initializer gem.
17
+ MSG
18
+ spec.homepage = 'https://github.com/Envek/yard-dry-initializer'
19
+ spec.license = 'MIT'
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
+ f.match(%r{^(test|spec|features)/})
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'yard', '>= 0.9'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.16'
31
+ spec.add_development_dependency 'rake', '~> 12.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rubocop'
34
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yard-dry-initializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Novikov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: |
84
+ Generates for you all YARD declarations of attributes
85
+ and initializer parameters created by dry-initializer gem.
86
+ email:
87
+ - envek@envek.name
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - generated_docs_example.png
103
+ - lib/yard-dry-initializer.rb
104
+ - lib/yard/dry/initializer.rb
105
+ - lib/yard/dry/initializer/common_handler.rb
106
+ - lib/yard/dry/initializer/option_handler.rb
107
+ - lib/yard/dry/initializer/param_handler.rb
108
+ - lib/yard/dry/initializer/version.rb
109
+ - yard-dry-initializer.gemspec
110
+ homepage: https://github.com/Envek/yard-dry-initializer
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Generates documentation for your params and options
134
+ test_files: []