rd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +42 -0
  2. data/bin/rd +12 -0
  3. data/lib/rklass.rb +124 -0
  4. metadata +79 -0
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ RD cli tool
2
+ ===========
3
+
4
+ The rd is tiny cli tool for ruby 1.9 kernel classes documenation.
5
+
6
+ Uses http://ruby-doc.org/ as data source.
7
+
8
+ Usage
9
+ -----
10
+
11
+ $ rd list
12
+ Will output all the kernel classes data retrieving is available for.
13
+
14
+ $ rd :class_name
15
+ Will output all the class methods and class includes such as additional modules.
16
+ Example:
17
+ $ rd exception
18
+ will output:
19
+ Exception
20
+ methods
21
+ • ==
22
+ • backtrace
23
+ • exception
24
+ • inspect
25
+ • message
26
+ • new
27
+ • set_backtrace
28
+ • to_s
29
+
30
+ $ rd :class_name :method
31
+ Will output method interface definition and method description (if available).
32
+ Example:
33
+ $ rd exception to_s
34
+ will output:
35
+ Exception
36
+ method
37
+ to_s
38
+ interface
39
+ • exception.to_s → string
40
+ description
41
+
42
+ Returns exception‘s message (or the name of the exception if no message is set).
data/bin/rd ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rklass')
4
+
5
+ begin
6
+ raise "No search data given. Skiped." if ARGV.size == 0
7
+ rk = RKlass.new(ARGV)
8
+ puts rk.cout
9
+ rescue => e
10
+ puts "Execution error: #{e.message}"
11
+ exit 1
12
+ end
data/lib/rklass.rb ADDED
@@ -0,0 +1,124 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'net/http'
5
+ require 'nokogiri'
6
+ require 'open-uri'
7
+ require 'rainbow'
8
+
9
+ class RKlass
10
+
11
+ KLASSES = [
12
+ "ARGF", "ArgumentError", "Array", "BasicObject", "Bignum", "Binding", "Class", "Comparable",
13
+ "Complex", "Continuation", "Data", "Dir", "EOFError", "Encoding", "Encoding/Converter", "Encoding/ConverterNotFoundError",
14
+ "Encoding/InvalidByteSequenceError", "Encoding/UndefinedConversionError", "EncodingError", "Enumerable",
15
+ "Enumerator", "Enumerator/Generator", "Enumerator/Yielder", "Errno", "Exception", "FalseClass", "Fiber",
16
+ "FiberError", "File", "File/Constants", "File/Stat", "FileTest", "Fixnum", "Float", "FloatDomainError",
17
+ "GC", "GC/Profiler", "Hash", "IO", "IO/WaitReadable", "IO/WaitWritable", "IOError", "IndexError", "Integer",
18
+ "Interrupt", "Kernel", "KeyError", "LoadError", "LocalJumpError", "Marshal", "MatchData", "Math", "Math/DomainError", "Method",
19
+ "Module", "Mutex", "NameError", "NameError/message", "NilClass", "NoMemoryError", "NoMethodError", "NotImplementedError",
20
+ "Numeric", "Object", "ObjectSpace", "Proc", "Process", "Process/GID", "Process/Status", "Process/Sys",
21
+ "Process/UID", "Random", "Range", "RangeError", "Rational", "Regexp", "RegexpError", "RubyVM", "RubyVM/Env",
22
+ "RuntimeError", "ScriptError", "SecurityError", "Signal", "Signal", "SignalException", "StandardError",
23
+ "StopIteration", "String", "Struct", "Symbol", "SyntaxError", "SystemCallError", "SystemExit", "SystemStackError",
24
+ "Thread", "ThreadError", "ThreadGroup", "Time", "TrueClass", "TypeError", "UnboundMethod", "ZeroDivisionError", "fatal"
25
+ ]
26
+
27
+ class Formatter
28
+
29
+ class << self
30
+
31
+ def cout(data)
32
+ format_branch(data).join("\n")
33
+ end
34
+
35
+ def inherit(str)
36
+ " #{str}"
37
+ end
38
+
39
+ def point(str)
40
+ "#{'•'.color(:red)} #{str.color(:cyan)}"
41
+ end
42
+
43
+ def format_branch(branch)
44
+ lines = []
45
+ if branch.is_a? Hash
46
+ branch.each_pair do |k, v|
47
+ lines << k.to_s.color(:magenta)
48
+ format_branch(v).each do |l|
49
+ lines << inherit(l)
50
+ end
51
+ end
52
+ elsif branch.is_a? Array
53
+ branch.each do |e|
54
+ lines << point(e)
55
+ end
56
+ else
57
+ lines << branch
58
+ end
59
+ lines
60
+ end
61
+
62
+ end
63
+ end
64
+
65
+ def initialize(data)
66
+ @search = data.first
67
+ raise "No class entry found: #{@search}." if !KLASSES.include?(@search) && !KLASSES.include?(@search.capitalize) && !self.respond_to?(@search.to_sym)
68
+ @method = data[1] if data.size > 1
69
+ end
70
+
71
+ def cout
72
+ if self.respond_to?(@search.to_sym)
73
+ Formatter.cout(self.send(@search.to_sym))
74
+ else
75
+ klass = @search
76
+ klass = klass.capitalize if !KLASSES.include?(klass)
77
+ doc = Nokogiri::HTML(open("http://ruby-doc.org/core/classes/#{klass}.html"))
78
+ methods = {}
79
+ includes = []
80
+ doc.css('#method-list div.name-list a').each do |method_link|
81
+ method_name = method_link.content.to_s
82
+ href = method_link['href'].sub('#', '')
83
+ method_heading = ''
84
+ method_descr = ''
85
+ method_examples = ''
86
+ doc.css('#method-' + href).each do |l|
87
+ l.css('div.method-heading span.method-name').each do |span|
88
+ method_heading = span.content.to_s.split(/<br>|\n/)
89
+ end
90
+ l.css('div.method-description p, div.method-description pre').each do |p|
91
+ method_descr += p.content.to_s.gsub(/<.+?>(.+)?<\/.+?>/, '\1')
92
+ end
93
+ end
94
+ methods[method_name] = {
95
+ :interface => method_heading,
96
+ :description => method_descr
97
+ # :examples => method_examples
98
+ }
99
+ end
100
+ doc.css('#includes-list a').each do |incl|
101
+ includes << incl.content.to_s
102
+ end
103
+
104
+ data = {}
105
+
106
+ if (@method.nil?)
107
+ data[:methods] = methods.keys if methods.size > 0
108
+ data[:includes] = includes if includes.size > 0
109
+ else
110
+ raise "No method named '#{@method}' found for class #{klass}." if methods[@method].nil?
111
+ data[:method] = @method
112
+ [:interface, :description].each do |sub|
113
+ data[sub] = methods[@method][sub]
114
+ end
115
+ end
116
+
117
+ Formatter.cout({ klass => data })
118
+ end
119
+ end
120
+
121
+ def list
122
+ { :available_classes => KLASSES }
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rd
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - 4pcbr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-07 00:00:00 +04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: nokogiri
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rainbow
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: ruby kernel classes documentation cli tool
39
+ email: i4pcbr@gmail.com
40
+ executables:
41
+ - rd
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.md
46
+ files:
47
+ - README.md
48
+ - bin/rd
49
+ - lib/rklass.rb
50
+ has_rdoc: true
51
+ homepage: https://github.com/4pcbr/rd
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.6.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: ruby kernel classes documentation cli tool
78
+ test_files: []
79
+