dot-what 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. checksums.yaml +15 -0
  2. data/History.txt +3 -0
  3. data/LICENSE +21 -0
  4. data/Manifest.txt +6 -0
  5. data/README.md +33 -0
  6. data/Rakefile +24 -0
  7. data/lib/what.rb +203 -0
  8. metadata +83 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjM2OTM1YWMxM2FhNDI3YTUzMGM0ZTI4ODc4MzRhN2NiNzRhNjI1OQ==
5
+ data.tar.gz: !binary |-
6
+ ZjQ0NDlhOTZhMzNjNjMwOTVjYzA0MGI5NWI2NTUyYjY3YWRlMzg4Zg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzUxYzY1NGM0NzI3YTg5NzlkOTljZDRmZTIwMDZkZGVhNWNlODI1MzU1MGMx
10
+ ZmY4NDVkYjM1NTEzODZiNjA2Yzg4ZGI3MDM2ZGI3MGJmNDkwZTE5OGVkNTIz
11
+ MWE2ZGYyZjUxMDE3MDdlMDExYzBlNDdjMTg0NjM2YWJmM2UyMDM=
12
+ data.tar.gz: !binary |-
13
+ OWY1YWNkNmYzYjkyMWEyYWNjMzZmMWMzNmFiNzNkM2RjODRmNjJiM2VhMDky
14
+ MjI3Njk1MDlmOWYzYmRhYTVmNTM1ODU2ODM2ZjZmZjYyZThlNTExMmQ1NzZk
15
+ YWEzMDNhMmFmY2E0Nzc2NjI5ODU0MmFkODkxOWRkYTE5YjBjNzg=
@@ -0,0 +1,3 @@
1
+ === 0.1.0 / 2016-02-12
2
+
3
+ * First version!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lars Christensen
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.md
4
+ LICENSE
5
+ Rakefile
6
+ lib/what.rb
@@ -0,0 +1,33 @@
1
+ # dot-what
2
+
3
+ Show ruby available methods on objects and classes
4
+
5
+ ## Synopsis
6
+
7
+ ```shell
8
+ c:\> irb
9
+ irb(main):001:0> require 'what'
10
+ => true
11
+ irb(main):002:0> String.what?
12
+ => nil
13
+ ```
14
+
15
+ ```shell
16
+ irb(main):001:0> require 'time'
17
+ => true
18
+ irb(main):002:0> Time.what?
19
+ => nil
20
+ ```
21
+
22
+ ```shell
23
+ irb(main):001:0> require 'time'
24
+ => true
25
+ irb(main):002:0> Time.instance_what?
26
+ => nil
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ ```shell
32
+ gem install dot-what
33
+ ```
@@ -0,0 +1,24 @@
1
+ # -*- ruby -*-
2
+
3
+ require "rubygems"
4
+ require "hoe"
5
+
6
+ # Hoe.plugin :compiler
7
+ # Hoe.plugin :gem_prelude_sucks
8
+ # Hoe.plugin :inline
9
+ # Hoe.plugin :minitest
10
+ # Hoe.plugin :racc
11
+ # Hoe.plugin :rcov
12
+ # Hoe.plugin :rdoc
13
+
14
+ Hoe.spec "dot-what" do |it|
15
+
16
+ developer "Lars Christensen", "larsch@belunktum.dk"
17
+ license "MIT"
18
+ it.readme_file = "README.md"
19
+ it.urls = [ "https://github.com/larsch/dot-what" ]
20
+ it.summary = "object.what?"
21
+ it.description = "Show ruby available methods on objects and classes"
22
+ end
23
+
24
+ # vim: syntax=ruby
@@ -0,0 +1,203 @@
1
+ module What
2
+
3
+ VERSION = "0.1.0"
4
+
5
+ module Colors
6
+ def black(str) "\e[30m#{str}\e[0m" end
7
+ def red(str) "\e[31m#{str}\e[0m" end
8
+ def green(str) "\e[32m#{str}\e[0m" end
9
+ def brown(str) "\e[33m#{str}\e[0m" end
10
+ def blue(str) "\e[34m#{str}\e[0m" end
11
+ def magenta(str) "\e[35m#{str}\e[0m" end
12
+ def cyan(str) "\e[36m#{str}\e[0m" end
13
+ def gray(str) "\e[37m#{str}\e[0m" end
14
+
15
+ def bg_black(str) "\e[40m#{str}\e[0m" end
16
+ def bg_red(str) "\e[41m#{str}\e[0m" end
17
+ def bg_green(str) "\e[42m#{str}\e[0m" end
18
+ def bg_brown(str) "\e[43m#{str}\e[0m" end
19
+ def bg_blue(str) "\e[44m#{str}\e[0m" end
20
+ def bg_magenta(str) "\e[45m#{str}\e[0m" end
21
+ def bg_cyan(str) "\e[46m#{str}\e[0m" end
22
+ def bg_gray(str) "\e[47m#{str}\e[0m" end
23
+
24
+ def bold(str) "\e[1m#{str}\e[21m" end
25
+ def italic(str) "\e[3m#{str}\e[23m" end
26
+ def underline(str) "\e[4m#{str}\e[24m" end
27
+ def blink(str) "\e[5m#{str}\e[25m" end
28
+ def reverse(str) "\e[7m#{str}\e[27m" end
29
+ end
30
+
31
+ module NoColors
32
+ def black(str) str end
33
+ def red(str) str end
34
+ def green(str) str end
35
+ def brown(str) str end
36
+ def blue(str) str end
37
+ def magenta(str) str end
38
+ def cyan(str) str end
39
+ def gray(str) str end
40
+
41
+ def bg_black(str) str end
42
+ def bg_red(str) str end
43
+ def bg_green(str) str end
44
+ def bg_brown(str) str end
45
+ def bg_blue(str) str end
46
+ def bg_magenta(str) str end
47
+ def bg_cyan(str) str end
48
+ def bg_gray(str) str end
49
+
50
+ def bold(str) str end
51
+ def italic(str) str end
52
+ def underline(str) str end
53
+ def blink(str) str end
54
+ def reverse(str) str end
55
+ end
56
+
57
+ def What.where(program)
58
+ if search_path = ENV["PATH"]
59
+ paths = search_path.split(File::PATH_SEPARATOR)
60
+ paths.each do |path|
61
+ path.tr!('\\','/')
62
+ candidate = File.join(path, program)
63
+ return candidate if File.executable?(candidate)
64
+ if pathext = ENV["PATHEXT"]
65
+ pathexts = pathext.split(File::PATH_SEPARATOR)
66
+ pathexts.each do |ext|
67
+ candidate = File.join(path, program + ext)
68
+ return candidate if File.executable?(candidate)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ nil
74
+ end
75
+
76
+ def What.pager
77
+ env_pager = ENV["PAGER"]
78
+ if pager = env_pager || where("less") || where("more")
79
+ pager.tr!('/','\\')
80
+ args = []
81
+ args << "-r" if pager =~ /\bless\b/
82
+ File.popen([pager, *args], "w") do |io|
83
+ if (pager =~ /more\.com/i && ENV["ConEmuANSI"] == "ON") || args.include?("-r")
84
+ io.extend(Colors)
85
+ else
86
+ io.extend(NoColors)
87
+ end
88
+ yield(io)
89
+ end
90
+ else
91
+ io = STDOUT.dup
92
+ io.extend(NoColors)
93
+ yield(io)
94
+ end
95
+ end
96
+
97
+ def What.instance_what?(klass, method = nil)
98
+ methods = []
99
+ if Symbol === method
100
+ methods = [klass.instance_method(method)].group_by { |m| m.owner }
101
+ else
102
+ methods = klass.instance_methods.map { |m| klass.instance_method(m) }.group_by { |m| m.owner }
103
+ rejected = [ Kernel, Object, BasicObject ]
104
+ if method == false || method == nil
105
+ methods.delete_if { |key| rejected.include?(key) }
106
+ end
107
+ end
108
+ What.pager { |io| describe(methods, io) }
109
+ end
110
+
111
+ def What.about(object, method = nil)
112
+ methods = []
113
+ if Symbol === method
114
+ methods = [object.method(method)].group_by { |m| m.owner }
115
+ else
116
+ methods = object.methods.map { |m| object.method(m) }.group_by { |m| m.owner }
117
+ rejected = [ Kernel, Object, BasicObject ]
118
+ if method == false || method == nil
119
+ methods.delete_if { |key| rejected.include?(key) }
120
+ end
121
+ end
122
+ What.pager { |io| describe(methods, io) }
123
+ end
124
+
125
+ def What.describe(methods, io)
126
+ first = true
127
+ methods.each do |k, ms|
128
+ ms = ms.sort_by { |m| m.name }
129
+ io.puts unless first
130
+ first = false
131
+ io.puts "#{io.green(io.bold(k.to_s))} (#{io.green(k.class.to_s)})"
132
+ ms.each do |m|
133
+ prefix = (UnboundMethod === m) ? "#" : ""
134
+ io.print " #{io.blue(io.bold(prefix + m.name.to_s))}"
135
+ if m.arity == -1 && m.parameters.empty?
136
+ io.print "(...)"
137
+ elsif m.arity != 0
138
+ param_name = "a"
139
+ parms = m.parameters.map do |type, name|
140
+ if name.nil?
141
+ name = param_name
142
+ param_name = param_name.next
143
+ end
144
+ case type
145
+ when :opt
146
+ "#{name} = ?"
147
+ when :rest
148
+ "*#{name}"
149
+ when :block
150
+ "&#{name}"
151
+ else
152
+ "name"
153
+ end
154
+ end
155
+ io.print "(" + parms.join(", ") + ((m.arity < 0) ? ", ...)" : ")")
156
+ end
157
+ if m.respond_to?(:super_method) && sm = m.super_method
158
+ io.puts ", overrides #{sm}"
159
+ end
160
+ line_feeded = false
161
+ if s = m.source_location
162
+ io.print " #{s[0]}:#{s[1]}"
163
+ begin
164
+ lns = IO.read(s[0]).lines.to_a
165
+ ln = s[1]-1
166
+ found = false
167
+ while ln >= 0 && ln >= s[1]-6 && !found
168
+ endline = ln
169
+ while lns[ln] =~ /^\s*#/
170
+ beginline = ln
171
+ found = true
172
+ ln -= 1
173
+ end
174
+ if found
175
+ doc = lns[beginline..endline]
176
+ indent = doc.map { |ln| ln[/^\s*/].size }.min
177
+ io.puts
178
+ io.puts doc.map { |ln| " " + io.green(ln[indent..-1].chomp) }
179
+ line_feeded = true
180
+ end
181
+ ln -= 1
182
+ end
183
+ rescue Errno::ENOENT
184
+ end
185
+ end
186
+ io.puts unless line_feeded
187
+ end
188
+ end
189
+ nil
190
+ end
191
+ end
192
+
193
+ module Kernel
194
+ def what?(method = false)
195
+ What.about(self, method)
196
+ end
197
+ end
198
+
199
+ class Module
200
+ def instance_what?(method = false)
201
+ What.instance_what?(self, method)
202
+ end
203
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dot-what
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lars Christensen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.14'
41
+ description: Show ruby available methods on objects and classes
42
+ email:
43
+ - larsch@belunktum.dk
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.md
50
+ files:
51
+ - History.txt
52
+ - LICENSE
53
+ - Manifest.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/what.rb
57
+ homepage: https://github.com/larsch/dot-what
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --main
64
+ - README.md
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.5.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: object.what?
83
+ test_files: []