Class: AddedMethods::AddedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/added_methods/added_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ AddedMethod

Returns a new instance of AddedMethod.



33
34
35
36
37
38
39
# File 'lib/added_methods/added_method.rb', line 33

def initialize(args = {})
  self.time = Time.now

  args.each { |key, value|
    send("#{key}=", value)
  }
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def base
  @base
end

#defObject

Returns the value of attribute def.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def def
  @def
end

#fileObject

Returns the value of attribute file.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def file
  @file
end

#klassObject

Returns the value of attribute klass.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def klass
  @klass
end

#lineObject

Returns the value of attribute line.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def line
  @line
end

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def name
  @name
end

#singletonObject Also known as: singleton?

Returns the value of attribute singleton.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def singleton
  @singleton
end

#timeObject

Returns the value of attribute time.



31
32
33
# File 'lib/added_methods/added_method.rb', line 31

def time
  @time
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
# File 'lib/added_methods/added_method.rb', line 44

def [](key)
  send(key.to_sym == :class ? :klass : key)
end

#extract_source(num_lines = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/added_methods/added_method.rb', line 56

def extract_source(num_lines = nil)
  lines = extract_source_from_script_lines(num_lines)

  # try to make sure we correctly extracted the method
  # definition, otherwise try to get it from Ruby2Ruby
  if lines && lines.first =~ /\b#{name}\b/
    lines
  else
    extract_source_from_r2r || lines
  end
end

#r2r_sourceObject



52
53
54
# File 'lib/added_methods/added_method.rb', line 52

def r2r_source
  @r2r_source ||= extract_source_from_r2r
end

#sourceObject



48
49
50
# File 'lib/added_methods/added_method.rb', line 48

def source
  @source ||= extract_source
end

#to_sObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/added_methods/added_method.rb', line 68

def to_s
  str = "# File #{file}, line #{line}"

  case lines = source
    when Array
      num   = line - 1
      width = (num + lines.size).to_s.length

      lines.map! { |l| "%0#{width}d: %s" % [num += 1, l] }

      "#{' ' * width}  #{str}\n#{lines}"
    when String
      "#{str}#{lines}"
    else
      str
  end
end