Module: RiderServer::Inspect

Defined in:
lib/rider_server/inspect.rb

Class Method Summary collapse

Class Method Details

.describe_method(method) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rider_server/inspect.rb', line 136

def self.describe_method(method)
  loc = method.source_location
  method_location = if !loc.nil? && loc.length == 2 && loc[0].is_a?(String) && loc[1].is_a?(Integer)
    loc
  else
    []
  end

  {
    "name" => method.name.to_s,
    "value" => Utils.rider_display(method),
    "visibility" => if method.private?
                      ":private"
                    elsif method.protected?
                      ":protected"
                    elsif method.public?
                      ":public"
                    else
                      ":unknown"
                    end,
    "owner" => method.owner.rider_inspect,
    "source-location" => method_location,
    "parameters" => inspect_parameters(method.parameters)
  }
end

.inspect_ancestors(obj) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rider_server/inspect.rb', line 59

def self.inspect_ancestors(obj)
  if obj.respond_to?(:ancestors)
    obj.ancestors.map do |item|
      if item.instance_of? ::Module
        {
          "name" => item.to_s,
          "value" => Utils.rider_display(item),
          "inspect-location" => "ancestor_find:#{item}",
          "source-location" => safely_location("Object.const_source_location('#{item}')")
        }
      else
        {
          "name" => item.to_s,
          "value" => Utils.rider_display(item),
          "inspect-location" => "ancestor_find:#{item}",
          "source-location" => []
        }
      end
    end
  else
    []
  end
end

.inspect_class(obj) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rider_server/inspect.rb', line 34

def self.inspect_class(obj)
  klass = obj.class
  {
    "name" => klass.to_s,
    "value" => Utils.rider_display(safely_eval("Object.const_get('#{klass}')", __FILE__, __LINE__)),
    "inspect-location" => "toplevel_const_get:#{klass.inspect}",
    "source-location" => safely_eval("Object.const_source_location('#{klass}')", __FILE__, __LINE__) || []
  }
end

.inspect_class_variables(obj) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rider_server/inspect.rb', line 201

def self.inspect_class_variables(obj)
  if obj.respond_to?(:class_variables)
    obj.class_variables.map do |item|
      [
        {
          "name" => item.to_s
        },
        {
          "name" => obj.class_variable_get(item).to_s,
          "value" => Utils.rider_display(obj.class_variable_get(item)),
          "inspect-location" => "class_variable_get:#{item}"
        }
      ]
    end
  else
    []
  end
end

.inspect_constants(obj) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rider_server/inspect.rb', line 83

def self.inspect_constants(obj)
  if obj.respond_to?(:constants)
    constants = safely_eval("#{obj.rider_inspect}.constants", __FILE__, __LINE__)
    return [] if constants.nil?  # Eigen classes have no constants
    constants.map do |item|
      [
        {
          "name" => item.to_s
        },
        {
          "name" => item.to_s,
          "value" => Utils.rider_display(safely_eval("#{obj.rider_inspect}.const_get('#{item}')", __FILE__, __LINE__)),
          "inspect-location" => "const_get:#{obj.rider_inspect}::#{item}",
          "source-location" => safely_location("#{obj.rider_inspect}.const_source_location('#{item}')")
        }
      ]
    end.sort_by { |hash| hash[0]["name"] }
  else
    []
  end
end

.inspect_instance_methods(obj) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/rider_server/inspect.rb', line 172

def self.inspect_instance_methods(obj)
  if obj.respond_to?(:rider_instance_methods)
    obj.rider_instance_methods.map do |item|
      describe_method(obj.instance_method(item))
    end
  else
    []
  end
end

.inspect_instance_variables(obj) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rider_server/inspect.rb', line 182

def self.inspect_instance_variables(obj)
  if obj.respond_to?(:instance_variables)
    obj.instance_variables.map do |item|
      [
        {
          "name" => item.to_s
        },
        {
          "name" => obj.instance_variable_get(item).to_s,
          "value" => Utils.rider_display(obj.instance_variable_get(item)),
          "inspect-location" => "instance_variable_get:#{item}"
        }
      ]
    end
  else
    []
  end
end

.inspect_methods(obj) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/rider_server/inspect.rb', line 162

def self.inspect_methods(obj)
  if obj.respond_to?(:rider_methods)
    obj.rider_methods.map do |name|
      describe_method(obj.method(name))
    end
  else
    []
  end
end

.inspect_parameters(parameters) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rider_server/inspect.rb', line 105

def self.inspect_parameters(parameters)
  alpha = Enumerator.new do |y|
    ("a".."z").each { |char| y << char }
  end

  parameters.map do |params|
    arg, name = params
    if arg == :rest
      "*#{name || "args"}"
    elsif arg == :keyrest
      if name.nil? || name == "**"
        "**kwargs"
      else
        name
      end
    elsif arg == :req
      name || alpha.next
    elsif arg == :keyreq
      "#{name || alpha.next}: ..."
    elsif arg == :key
      "#{name || alpha.next}:"
    elsif arg == :opt
      "#{name || alpha.next}=..."
    elsif arg == :block
      "&block"
    else
      raise "Unknown prameter format #{arg}"
    end.to_s
  end
end

.inspect_singleton_class(obj) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rider_server/inspect.rb', line 44

def self.inspect_singleton_class(obj)
  klass = obj.singleton_class
  {
    "name" => klass.to_s,
    "value" => Utils.rider_display(klass),
    "inspect-location" => "singleton_class",
    "source-location" => []
  }
rescue => e  # integers have no singleton
  {
    "name" => e.to_s,
    "value" => Utils.rider_display(e)
  }
end

.safely_eval(string, file, line) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rider_server/inspect.rb', line 14

def self.safely_eval(string, file, line)
  code = <<~HEREDOC
    begin
      #{string}
    rescue StandardError => e
      e
    end
  HEREDOC
  eval(code, TOPLEVEL_BINDING, file, line) # rubocop:disable Security/Eval
end

.safely_location(src) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rider_server/inspect.rb', line 25

def self.safely_location(src)
  loc = safely_eval(src, __FILE__, __LINE__)
  if loc.length == 2 && loc[0].is_a?(String) && loc[1].is_a?(Integer)
    loc
  else
    []
  end
end