Class: ShowCode::Code
- Inherits:
-
Object
- Object
- ShowCode::Code
- Defined in:
- lib/show_code/code.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
- #add_line_numbers(output) ⇒ Object
-
#expression_end?(codes) ⇒ Boolean
refer: gist.github.com/judofyr/1373383.
- #extract_method_code(related_lines) ⇒ Object
-
#greet ⇒ Object
Example: ShowCode ‘ShowCode::Code.new.greet’.
- #highlighted(opts = {}) ⇒ Object
-
#initialize(location) ⇒ Code
constructor
A new instance of Code.
- #real_start_line(file_lines, line) ⇒ Object
Constructor Details
#initialize(location) ⇒ Code
Returns a new instance of Code.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/show_code/code.rb', line 7 def initialize(location) @file = location.file file_lines = File.readlines(@file) if @file @line = real_start_line file_lines, location.line = file_lines[(@line - 1)..-1] || [] codes = extract_method_code @lines = codes.count @content = codes.join end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/show_code/code.rb', line 5 def content @content end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/show_code/code.rb', line 5 def file @file end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
5 6 7 |
# File 'lib/show_code/code.rb', line 5 def line @line end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
5 6 7 |
# File 'lib/show_code/code.rb', line 5 def lines @lines end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
5 6 7 |
# File 'lib/show_code/code.rb', line 5 def owner @owner end |
Instance Method Details
#add_line_numbers(output) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/show_code/code.rb', line 58 def add_line_numbers(output) line_number = line - 1 output.split("\n").map do |loc| line_number += 1 "\e[33m#{line_number.to_s.ljust(4, ' ')}\e[0m#{loc}" end.join("\n") end |
#expression_end?(codes) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/show_code/code.rb', line 38 def expression_end?(codes) str = codes.join catch(:valid) do eval("BEGIN{throw :valid}; #{str}") end # skip , or str !~ /[,\\]\s*\z/ rescue SyntaxException false end |
#extract_method_code(related_lines) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/show_code/code.rb', line 28 def extract_method_code() codes = [] .each do |loc| codes << loc return codes if expression_end?(codes) end raise SyntaxError, "unexpected $end" end |
#greet ⇒ Object
Example: ShowCode ‘ShowCode::Code.new.greet’
67 68 69 |
# File 'lib/show_code/code.rb', line 67 def greet puts 'Hello ShowCode!' end |
#highlighted(opts = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/show_code/code.rb', line 51 def highlighted(opts = {}) = {:line_numbers => true}.merge(opts) colorized = CodeRay.scan(content, :ruby).tty output = [:line_numbers] ? add_line_numbers(colorized) : colorized "\n%s\n\n" % output end |
#real_start_line(file_lines, line) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/show_code/code.rb', line 19 def real_start_line(file_lines, line) return line unless line.zero? file_lines.each do |loc| line += 1 return line if /^\s*(class|module)\s+[A-Z]/ === loc end end |