class Sirens::Method

Attributes

is_instance_method[R]

Accessing

method_filename[R]

Accessing

method_line_number[R]

Accessing

mod[R]

Accessing

name[R]

Accessing

visibility[R]

Accessing

Public Class Methods

new(mod:, name:, visibility:, instance_method:) click to toggle source

Initializing

# File lib/sirens/models/method.rb, line 5
def initialize(mod:, name:, visibility:, instance_method:)
    @mod = mod
    @name = name
    @visibility = visibility
    @is_instance_method = instance_method

    @ruby_method =  @is_instance_method === true ? 
        @mod.instance_method(@name) : @mod.method(@name)

    @method_filename, @method_line_number = @ruby_method.source_location

    @source_code = nil
end

Public Instance Methods

icon() click to toggle source

Querying

# File lib/sirens/models/method.rb, line 48
def icon()
    if is_public?
        filename = 'public-method.png'
    elsif is_protected?
        filename = 'protected-method.png'
    elsif is_private?
        filename = 'private-method.png'
    else
        raise RuntimeError.new('Uknown visibility type.')
    end

    Pathname.new(__FILE__).dirname + '../../../resources/icons/' + filename             
end
is_instance_method?() click to toggle source
# File lib/sirens/models/method.rb, line 42
def is_instance_method?()
    is_instance_method
end
is_private?() click to toggle source
# File lib/sirens/models/method.rb, line 38
def is_private?()
    visibility == :private
end
is_protected?() click to toggle source
# File lib/sirens/models/method.rb, line 34
def is_protected?()
    visibility == :protected
end
is_public?() click to toggle source

Asking

# File lib/sirens/models/method.rb, line 30
def is_public?()
    visibility == :public
end
remove_indentation(source_code) click to toggle source

Utility methods

# File lib/sirens/models/method.rb, line 78
def remove_indentation(source_code)
    lines = source_code.lines

    indentation = 0

    /^(\s*).*$/.match(lines.first) do |matches|
        indentation = matches[1].size
    end

    lines = lines.collect { |line|
        line.gsub!(/^\s{#{indentation}}/, '')
    }

    lines.join
end
source_code() click to toggle source
# File lib/sirens/models/method.rb, line 66
def source_code()
    begin
        @source_code ||= remove_indentation(@ruby_method.comment) +
            "\n" +
            remove_indentation(@ruby_method.source)                
    rescue ::MethodSource::SourceNotFoundError => e
        @source_code = "Could not locate source for #{@mod}::#{@name}"
    end
end
source_location() click to toggle source
# File lib/sirens/models/method.rb, line 62
def source_location()
    "#{method_filename}:#{method_line_number}"
end