Class: Sirens::Method
- Inherits:
-
Object
- Object
- Sirens::Method
- Defined in:
- lib/sirens/models/method.rb
Instance Attribute Summary collapse
-
#is_instance_method ⇒ Object
readonly
Accessing.
-
#method_filename ⇒ Object
readonly
Accessing.
-
#method_line_number ⇒ Object
readonly
Accessing.
-
#mod ⇒ Object
readonly
Accessing.
-
#name ⇒ Object
readonly
Accessing.
-
#visibility ⇒ Object
readonly
Accessing.
Instance Method Summary collapse
-
#icon ⇒ Object
Querying.
-
#initialize(mod:, name:, visibility:, instance_method:) ⇒ Method
constructor
Initializing.
- #is_instance_method? ⇒ Boolean
- #is_private? ⇒ Boolean
- #is_protected? ⇒ Boolean
-
#is_public? ⇒ Boolean
Asking.
-
#remove_indentation(source_code) ⇒ Object
Utility methods.
- #source_code ⇒ Object
- #source_location ⇒ Object
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_method ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def is_instance_method @is_instance_method end |
#method_filename ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def method_filename @method_filename end |
#method_line_number ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def method_line_number @method_line_number end |
#mod ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def mod @mod end |
#name ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def name @name end |
#visibility ⇒ Object (readonly)
Accessing
21 22 23 |
# File 'lib/sirens/models/method.rb', line 21 def visibility @visibility end |
Instance Method Details
#icon ⇒ Object
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
42 43 44 |
# File 'lib/sirens/models/method.rb', line 42 def is_instance_method?() is_instance_method end |
#is_private? ⇒ Boolean
38 39 40 |
# File 'lib/sirens/models/method.rb', line 38 def is_private?() visibility == :private end |
#is_protected? ⇒ Boolean
34 35 36 |
# File 'lib/sirens/models/method.rb', line 34 def is_protected?() visibility == :protected end |
#is_public? ⇒ Boolean
Asking
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_code ⇒ Object
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_location ⇒ Object
62 63 64 |
# File 'lib/sirens/models/method.rb', line 62 def source_location() "#{method_filename}:#{method_line_number}" end |