Module: ReadSource::ReadSource

Defined in:
lib/read_source/read_source.rb

Instance Method Summary collapse

Instance Method Details

#attr?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/read_source/read_source.rb', line 32

def attr?
  (file, line_num = send :source_location) || return

  if file == "(irb)"
    warn "This feature doesn't work for code written in IRB!"
    return
  end

  def_header = IO.readlines(file)[line_num-1]
  return unless def_header =~ /\A[[:space:]]*attr[\w]*/
  def_header[/attr[\w]*/].to_sym rescue nil
end

#read_sourceObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/read_source/read_source.rb', line 3

def read_source
  (file, line_num = send :source_location) || return

  if file == "(irb)"
    warn "This feature doesn't work for code written in IRB!"
    return
  end

  is_inline_method = ->str{
    defs = str.scan(/def(?:\b|ine_method.*do)/).count
    ends = str.scan(/end/).count
    def_method_regex = /define_method[ (]+(?::|\w)+[ ),]+(?:{.*}|(?:instance_)?method\((?::|\w)+\)|&\w+)\)?/
    defs == ends || str =~ def_method_regex
  }

  readlines = IO.readlines(file)
  source = readlines[line_num-1]
  indent = /\A[[:space:]]*/.match(source).to_s.length
  source = source[indent..-1]
  return source if source.=~(/\A[[:space:]]*attr[\w]*/) || is_inline_method.(source)
  readlines[line_num..-1].each do |line|
    add_line = line =~ /\A[[:space:]]*\n\z/ ? "\n" : line[indent..-1]
    source += add_line
    if indent == /\A[[:space:]]*/.match(line).to_s.length
      break source
    end
  end
end