show_code 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a58a7d68906360e8984066796e0b05ad3ac687ab
4
- data.tar.gz: a2eace5e41bcc43550529d35cafe9a69209c0116
3
+ metadata.gz: 5ab123ab2b788e63a6f7c1395bae7876797af614
4
+ data.tar.gz: 7aa5dc0ba82d9beee57702ffd5d9d601ab3a1797
5
5
  SHA512:
6
- metadata.gz: de1028e128e1da58548ac64cbcde0251e2109bec25e02f4486b8bf7c72388fc971963953fb9bc6e44e90e7bc17fdd6171c534a9da19d11832abb38d1dc6686ad
7
- data.tar.gz: 21b85e098479ebcef206a37eaa595557b12ddf7a694ed042d3dbd5f9e5a241080eb6f54de4b5105f566ee55dd0669022b01f871b18eab5037ba2e2982857c9ef
6
+ metadata.gz: 0276b4538244c23480e8548572281facde845301f88eeaca0388af490a25a3a8fb65fe499d93294cbb4faf274647582f6ad04fde0cc16f41de6c40ba7dca9926
7
+ data.tar.gz: 2ce50ad3e766ea265b474be62d537a7a47025127f02842d2f40f850953cf031403a81c7bb014cfa2994b80eba1feb0a895f99783d8b33e7b0e112a7367c4ed40
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ # Default
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /Gemfile.lock
@@ -10,3 +11,38 @@
10
11
 
11
12
  # rspec failure tracking
12
13
  .rspec_status
14
+
15
+ ## MAC OS
16
+ .DS_Store
17
+
18
+ ## TEXTMATE
19
+ *.tmproj
20
+ tmtags
21
+
22
+ ## EMACS
23
+ *~
24
+ \#*
25
+ .\#*
26
+
27
+ ## VIM
28
+ *.swp
29
+
30
+ ## RUBYMINE
31
+ .idea
32
+
33
+ ## PROJECT::GENERAL
34
+ coverage
35
+ rdoc
36
+ pkg
37
+ .ruby-version
38
+ gemfiles/*.gemfile.lock
39
+ Gemfile.lock
40
+ /tmp
41
+ /.bundle
42
+ /gemfiles/.bundle
43
+
44
+ ## PROJECT::RVM
45
+ .rvmrc
46
+
47
+ # PROJECT::RBENV
48
+ .ruby-gemset
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## Code Show ##
1
+ ## Show Code ##
2
2
 
3
3
  show_code provides a quick way to show ruby method source codes in terminal.
4
4
 
@@ -15,7 +15,7 @@ __NOTE__: show_code current version *require* Ruby v1.9.0 or later.
15
15
  You can use show_code in `console c` or `irb`
16
16
 
17
17
  ```ruby
18
- require "show_code" # just when irb
18
+ require "show_code" # just when use irb
19
19
  ShowCode method_object
20
20
  # or
21
21
  ShowCode method_string
@@ -37,7 +37,7 @@ ShowCode ShowCode::Code.instance_method(:greet)
37
37
 
38
38
  ```
39
39
  #### open resource file
40
- Sometimes, we wanna open the resource file to edit, as default `geidt` will open the file.
40
+ Sometimes, we wanna open the resource file to edit, as default `gedit` will open the file.
41
41
 
42
42
  ```ruby
43
43
  ShowCode.open 'ShowCode::Code.new.greet'
@@ -0,0 +1,45 @@
1
+ module ShowCode
2
+ class Code
3
+
4
+ attr :content, :owner, :file, :lines
5
+
6
+ def initialize(location)
7
+ @file = location.file
8
+ file_lines = File.readlines(@file) if @file
9
+
10
+ related_lines = file_lines[(location.line - 1)..-1] || []
11
+ codes = extract_method_code related_lines
12
+ @lines = codes.count
13
+ @content = codes.join
14
+ end
15
+
16
+ def extract_method_code(related_lines)
17
+ codes = []
18
+ related_lines.each do |value|
19
+ codes << value
20
+ return codes if expression_end?(codes)
21
+ end
22
+ raise SyntaxError, "unexpected $end"
23
+ end
24
+
25
+ # refer: https://gist.github.com/judofyr/1373383
26
+ def expression_end?(codes)
27
+ str = codes.join
28
+
29
+ catch(:valid) do
30
+ eval("BEGIN{throw :valid}; #{str}")
31
+ end
32
+
33
+ # skip , or \
34
+ str !~ /[,\\]\s*\z/
35
+ rescue SyntaxException
36
+ false
37
+ end
38
+
39
+ # Example: ShowCode 'ShowCode::Code.new.greet'
40
+ def greet
41
+ puts 'Hello ShowCode!'
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,30 @@
1
+ module ShowCode
2
+ class SourceLocation
3
+
4
+ attr :method, :file, :line
5
+
6
+ def initialize(target)
7
+ if target.is_a?(String)
8
+
9
+ target = target.gsub! '.new', '.allocate'
10
+ klass = target.split('.')[0..-2].join('.')
11
+ method = target.split('.').last
12
+
13
+ # refer:
14
+ # Object.instance_method(:method).bind(User).call(:current).source_location
15
+ # Object.instance_method(:method).bind(User.allocate).call(:name).source_location
16
+ # Object.instance_method(:method).bind(User.second.school.leader).call(:name).source_location
17
+
18
+ @method = Object.instance_method(:method).bind(eval(klass)).call(method)
19
+ elsif target.is_a?(Method) || target.is_a?(UnboundMethod)
20
+ @method = target
21
+ else
22
+ raise ArgumentError, 'bad argument (expected Method object or String)'
23
+ end
24
+
25
+ @file, @line = @method.source_location
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module ShowCode
2
+ module SyntaxException
3
+
4
+ GENERIC_REGEXPS = [
5
+ /unexpected (\$end|end-of-file|end-of-input|END_OF_FILE)/, # mri, jruby, ruby-2.0, ironruby
6
+ /embedded document meets end of file/, # =begin
7
+ /unterminated (quoted string|string|regexp) meets end of file/, # "quoted string" is ironruby
8
+ /can't find string ".*" anywhere before EOF/, # rbx and jruby
9
+ /missing 'end' for/, /expecting kWHEN/ # rbx
10
+ ]
11
+
12
+ RBX_ONLY_REGEXPS = [
13
+ /expecting '[})\]]'(?:$|:)/, /expecting keyword_end/
14
+ ]
15
+
16
+ def self.===(ex)
17
+ return false unless SyntaxError === ex
18
+ case ex.message
19
+ when *GENERIC_REGEXPS
20
+ true
21
+ when *RBX_ONLY_REGEXPS
22
+ rbx?
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ def self.rbx?
29
+ RbConfig::CONFIG['ruby_install_name'] == 'rbx'
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+
@@ -1,3 +1,3 @@
1
1
  module ShowCode
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - cenxky
@@ -62,7 +62,6 @@ files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
64
  - ".travis.yml"
65
- - CODE_OF_CONDUCT.md
66
65
  - Gemfile
67
66
  - LICENSE.txt
68
67
  - README.md
@@ -70,6 +69,9 @@ files:
70
69
  - bin/console
71
70
  - bin/setup
72
71
  - lib/show_code.rb
72
+ - lib/show_code/code.rb
73
+ - lib/show_code/source_location.rb
74
+ - lib/show_code/syntax_exception.rb
73
75
  - lib/show_code/version.rb
74
76
  - show_code.gemspec
75
77
  homepage: https://github.com/cenxky/show_code
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at dfzy5566@qq.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/