Class: Sirens::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/sirens/models/method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod:, name:, visibility:, instance_method:) ⇒ Method

Initializing



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#is_instance_methodObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def is_instance_method
  @is_instance_method
end

#method_filenameObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def method_filename
  @method_filename
end

#method_line_numberObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def method_line_number
  @method_line_number
end

#modObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def mod
  @mod
end

#nameObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def name
  @name
end

#visibilityObject (readonly)

Accessing



21
22
23
# File 'lib/sirens/models/method.rb', line 21

def visibility
  @visibility
end

Instance Method Details

#iconObject

Querying



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sirens/models/method.rb', line 42

def is_instance_method?()
    is_instance_method
end

#is_private?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sirens/models/method.rb', line 38

def is_private?()
    visibility == :private
end

#is_protected?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sirens/models/method.rb', line 34

def is_protected?()
    visibility == :protected
end

#is_public?Boolean

Asking

Returns:

  • (Boolean)


30
31
32
# File 'lib/sirens/models/method.rb', line 30

def is_public?()
    visibility == :public
end

#remove_indentation(source_code) ⇒ Object

Utility methods



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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_codeObject



66
67
68
69
70
71
72
73
74
# 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_locationObject



62
63
64
# File 'lib/sirens/models/method.rb', line 62

def source_location()
    "#{method_filename}:#{method_line_number}"
end