rd 0.0.1 → 0.0.2

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 +1 -1
  2. data/bin/rd +6 -3
  3. data/lib/rklass.rb +78 -40
  4. metadata +2 -2
data/README.md CHANGED
@@ -9,7 +9,7 @@ Usage
9
9
  -----
10
10
 
11
11
  $ rd list
12
- Will output all the kernel classes data retrieving is available for.
12
+ Will output all the core classes data retrieving is available for.
13
13
 
14
14
  $ rd :class_name
15
15
  Will output all the class methods and class includes such as additional modules.
data/bin/rd CHANGED
@@ -3,9 +3,12 @@
3
3
  require File.join(File.dirname(__FILE__), '..', 'lib', 'rklass')
4
4
 
5
5
  begin
6
- raise "No search data given. Skiped." if ARGV.size == 0
7
- rk = RKlass.new(ARGV)
8
- puts rk.cout
6
+ data = ARGV
7
+ data = ['tutorial'] if !data.any?
8
+ rk = RKlass.new(data)
9
+ rk.cout do |content|
10
+ puts content
11
+ end
9
12
  rescue => e
10
13
  puts "Execution error: #{e.message}"
11
14
  exit 1
data/lib/rklass.rb CHANGED
@@ -64,61 +64,99 @@ class RKlass
64
64
 
65
65
  def initialize(data)
66
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)
67
+ raise "No class entry found: #{@search}." if (
68
+ !KLASSES.include?(@search) &&
69
+ !KLASSES.include?(@search.capitalize) &&
70
+ !self.respond_to?(@search.to_sym)
71
+ )
68
72
  @method = data[1] if data.size > 1
69
73
  end
70
74
 
71
- def cout
75
+ def self.load_class_data(klass)
76
+ data = Nokogiri::HTML(open("http://ruby-doc.org/core/classes/#{klass}.html"))
77
+ yield data if block_given?
78
+ data
79
+ end
80
+
81
+ def self.parse_class_data(doc)
82
+ methods = {}
83
+ includes = []
84
+ doc.css('#method-list div.name-list a').each do |method_link|
85
+ method_name = method_link.content.to_s
86
+ href = method_link['href'].sub('#', '')
87
+ method_heading = ''
88
+ method_descr = ''
89
+ method_examples = ''
90
+ doc.css('#method-' + href).each do |l|
91
+ l.css('div.method-heading span.method-name').each do |span|
92
+ method_heading = span.content.to_s.split(/<br>|\n/)
93
+ end
94
+ l.css('div.method-description p, div.method-description pre').each do |p|
95
+ method_descr += p.content.to_s.gsub(/<.+?>(.+)?<\/.+?>/, '\1')
96
+ end
97
+ end
98
+ methods[method_name] = {
99
+ :interface => method_heading,
100
+ :description => method_descr
101
+ }
102
+ end
103
+ doc.css('#includes-list a').each do |incl|
104
+ includes << incl.content.to_s
105
+ end
106
+ data = { :methods => methods, :includes => includes }
107
+ yield data if block_given?
108
+ data
109
+ end
110
+
111
+ def cout(&blk)
72
112
  if self.respond_to?(@search.to_sym)
73
- Formatter.cout(self.send(@search.to_sym))
113
+ Formatter.cout(self.send(@search.to_sym, &blk))
74
114
  else
75
115
  klass = @search
76
116
  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')
117
+ # class_data = parse_class_data(load_class_data(klass))
118
+ RKlass::load_class_data(klass) do |klass_doc|
119
+ RKlass::parse_class_data(klass_doc) do |class_data|
120
+ data = {}
121
+ if (@method.nil?)
122
+ data[:methods] = class_data[:methods].keys if class_data[:methods].size > 0
123
+ data[:includes] = class_data[:includes] if class_data[:includes].size > 0
124
+ else
125
+ raise "No method named '#{@method}' found for class #{klass}." if class_data[:methods][@method].nil?
126
+ data[:method] = @method
127
+ [:interface, :description].each do |sub|
128
+ data[sub] = class_data[:methods][@method][sub]
129
+ end
92
130
  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
131
 
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]
132
+ yield Formatter.cout({ klass => data }) if block_given?
114
133
  end
115
134
  end
116
-
117
- Formatter.cout({ klass => data })
118
135
  end
119
136
  end
120
137
 
121
138
  def list
122
139
  { :available_classes => KLASSES }
123
140
  end
141
+
142
+ def tutorial(&blk)
143
+ klass = KLASSES[Random.new.rand(KLASSES.size)]
144
+ RKlass::load_class_data(klass) do |klass_doc|
145
+ RKlass::parse_class_data(klass_doc) do |klass_data|
146
+ data = {}
147
+ if klass_data[:methods].any?
148
+ method = klass_data[:methods].keys[Random.new.rand(klass_data[:methods].keys.size)]
149
+ method_data = klass_data[:methods][method]
150
+ data[:method] = method
151
+ [:interface, :description].each do |sub|
152
+ data[sub] = method_data[sub]
153
+ end
154
+
155
+ yield Formatter.cout({ 'Tutorial mode' => ['on'], klass => data }) if block_given?
156
+ else
157
+ self.tutorial(&blk)
158
+ end
159
+ end
160
+ end
161
+ end
124
162
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rd
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - 4pcbr
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-07 00:00:00 +04:00
13
+ date: 2011-05-11 00:00:00 +04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency