Class: Util::AddedMethods::AddedMethod

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ AddedMethod

Returns a new instance of AddedMethod.



79
80
81
82
83
# File 'lib/nuggets/util/added_methods.rb', line 79

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

Instance Attribute Details

#baseObject

Returns the value of attribute base.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def base
  @base
end

#defObject

Returns the value of attribute def.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def def
  @def
end

#fileObject

Returns the value of attribute file.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def file
  @file
end

#klassObject

Returns the value of attribute klass.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def klass
  @klass
end

#lineObject

Returns the value of attribute line.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def line
  @line
end

#nameObject

Returns the value of attribute name.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def name
  @name
end

#singletonObject Also known as: singleton?

Returns the value of attribute singleton.



77
78
79
# File 'lib/nuggets/util/added_methods.rb', line 77

def singleton
  @singleton
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
# File 'lib/nuggets/util/added_methods.rb', line 88

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

#extract_source(num_lines = nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/nuggets/util/added_methods.rb', line 96

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
  lines.first =~ /\b#{name}\b/ ? lines : extract_source_from_r2r || lines
end

#sourceObject



92
93
94
# File 'lib/nuggets/util/added_methods.rb', line 92

def source
  @source ||= extract_source
end

#to_sObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/nuggets/util/added_methods.rb', line 104

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