docstrings 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in docstrings.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jack Chen (chendo)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # DocStrings
2
+
3
+ DocStrings allows you to define and access Python-like docstrings in Ruby.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class Dog
9
+ def bark
10
+ """Tell the dog to bark."""
11
+ puts "Woof!"
12
+ end
13
+
14
+ def sit
15
+ """
16
+ Makes the dog sit.
17
+ """
18
+ @state = :sitting
19
+ end
20
+ end
21
+
22
+ Dog.instance_method(:bark).docstring # => "Tell the dog to bark."
23
+ Dog.new.method(:sit).docstring # => "Makes the dog sit.
24
+ ```
25
+
26
+ ## How does it work?
27
+
28
+ Ruby has this odd syntax which allows you to concatenate two strings by simply having them side by side:
29
+
30
+ ```ruby
31
+ "foo" "bar" # => "foobar"
32
+ ```
33
+
34
+ So `"""Foo"""` simply evaluates to `"Foo"`, and all this gem does is define a method to retrieve this value by using `Method#source_location`.
35
+
36
+ ## Wouldn't this cause a performance hit every time the method is called?
37
+
38
+ Nope! At least, not in CRuby. Ruby is smart enough to recognise when string literals aren't actually used, and will skip generating bytecode for it.
39
+
40
+ ```ruby
41
+ def no_docstring
42
+ nil
43
+ end
44
+
45
+ def with_docstring
46
+ """Foo"""
47
+ nil
48
+ end
49
+
50
+ puts RubyVM::InstructionSequence.of(method(:no_docstring)).disasm
51
+ puts "---"
52
+ puts RubyVM::InstructionSequence.of(method(:with_docstring)).disasm
53
+
54
+ # Output:
55
+ # == disasm: <RubyVM::InstructionSequence:no_docstring@/tmp/execpad-ecb5745fbd46/source-ecb5745fbd46>
56
+ # 0000 trace 8 ( 1)
57
+ # 0002 putnil
58
+ # 0003 trace 16 ( 3)
59
+ # 0005 leave
60
+ # ---
61
+ # == disasm: <RubyVM::InstructionSequence:with_docstring@/tmp/execpad-ecb5745fbd46/source-ecb5745fbd46>
62
+ # 0000 trace 8 ( 5)
63
+ # 0002 putnil
64
+ # 0003 trace 16 ( 8)
65
+ # 0005 leave
66
+ ```
67
+
68
+ See https://eval.in/36676
69
+
70
+ ## Installation
71
+
72
+ Add this line to your application's Gemfile:
73
+
74
+ gem 'docstrings'
75
+
76
+ And then execute:
77
+
78
+ $ bundle
79
+
80
+ Or install it yourself as:
81
+
82
+ $ gem install docstrings
83
+
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create new Pull Request
92
+
93
+ ## License
94
+
95
+ See `LICENSE.txt`. It's MIT.
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
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'docstrings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docstrings"
8
+ spec.version = DocStrings::VERSION
9
+ spec.authors = ["Jack Chen (chendo)"]
10
+ spec.email = ["gems+docstrings@chen.do"]
11
+ spec.description = %q{docstrings is a gem that adds the ability to use Python-like docstrings}
12
+ spec.summary = %q{docstrings allows you to define and access docstrings on methods in Ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/lib/docstrings.rb ADDED
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
2
+
3
+ require "docstrings/version"
4
+ require "docstrings/extensions/base"
5
+
6
+ module DocStrings
7
+ module_function
8
+
9
+ def init
10
+ Method.send(:include, DocStrings::Extensions::Base::MethodExtension)
11
+ UnboundMethod.send(:include, DocStrings::Extensions::Base::MethodExtension)
12
+ end
13
+ end
14
+
15
+ DocStrings.init
@@ -0,0 +1,20 @@
1
+ module DocStrings
2
+ module Extensions
3
+ module Base
4
+ module MethodExtension
5
+ def docstring
6
+ path, line_number = source_location
7
+
8
+ if path && File.exists?(path)
9
+ file = File.read(path)
10
+
11
+ # FIXME: This will work terribly for large files
12
+ if file =~ Regexp.new(%Q{\\A(?:.*?\n){#{line_number}}\s*?"""([\\s\\S]+?)"""})
13
+ $1.gsub(/\n\s+/, "\n").strip
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module DocStrings
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocStrings do
4
+ class TestClass
5
+ def no_docstring
6
+ end
7
+
8
+ def simple
9
+ """Simple"""
10
+ end
11
+
12
+ def multi_line
13
+ """
14
+ Line 1
15
+ Line 2
16
+ """
17
+ end
18
+ end
19
+
20
+ def docstring_for_method(method_name)
21
+ TestClass.instance_method(method_name).docstring
22
+ end
23
+
24
+ it "works with simple docstrings" do
25
+ docstring_for_method(:simple).should == "Simple"
26
+ end
27
+
28
+ it "returns nil when no docstring" do
29
+ docstring_for_method(:no_docstring).should be_nil
30
+ end
31
+
32
+ it "works with multiline docstrings" do
33
+ docstring_for_method(:multi_line).should == "Line 1\nLine 2"
34
+ end
35
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'docstrings'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docstrings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jack Chen (chendo)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: docstrings is a gem that adds the ability to use Python-like docstrings
63
+ email:
64
+ - gems+docstrings@chen.do
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - docstrings.gemspec
77
+ - lib/docstrings.rb
78
+ - lib/docstrings/extensions/base.rb
79
+ - lib/docstrings/version.rb
80
+ - spec/docstrings_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: ''
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.23
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: docstrings allows you to define and access docstrings on methods in Ruby
107
+ test_files:
108
+ - spec/docstrings_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: