Module: Bade::Where

Defined in:
lib/bade/runtime/utils/where.rb

Class Method Summary collapse

Class Method Details

.class?(klass) ⇒ [String, Integer], String

Parameters:

  • klass (Class)

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bade/runtime/utils/where.rb', line 38

def class?(klass)
  defined_methods(klass)
    .group_by { |sl| sl[0] }
    .map do |file, sls|
      lines = sls.map { |sl| sl[1] }

      {
        file: file,
        count: lines.size,
        line: lines.min,
      }
    end # rubocop:disable Style/MultilineBlockChain
    .sort_by { |fc| fc[:count] }
    .map { |fc| [fc[:file], fc[:line]] }
end

.class_primarily?(klass) ⇒ Boolean

Raises ArgumentError if klass does not have any Ruby methods defined in it.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bade/runtime/utils/where.rb', line 55

def class_primarily?(klass)
  source_locations = class?(klass)
  if source_locations.empty?
    methods = defined_methods(klass)
    msg = if methods.empty?
            "#{klass} has no methods"
          else
            "#{klass} only has built-in methods (#{methods.size} in total)"
          end

    raise ::ArgumentError, msg
  end
  source_locations[0]
end

.instance_method?(klass, method_name) ⇒ [String, Integer], String

Parameters:

Returns:



24
25
26
# File 'lib/bade/runtime/utils/where.rb', line 24

def instance_method?(klass, method_name)
  source_location(klass.instance_method(method_name))
end

.instance_methods?(klass, method_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bade/runtime/utils/where.rb', line 32

def instance_methods?(klass, method_name)
  via_extractor?(:method, klass, method_name)
end

.method?(klass, method_name) ⇒ [String, Integer], String

Parameters:

  • klass (Class)
  • method_name (Symbol, String)

Returns:



17
18
19
# File 'lib/bade/runtime/utils/where.rb', line 17

def method?(klass, method_name)
  source_location(klass.method(method_name))
end

.methods?(klass, method_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/bade/runtime/utils/where.rb', line 28

def methods?(klass, method_name)
  via_extractor?(:method, klass, method_name)
end

.proc?(proc) ⇒ [String, Integer], String

Parameters:

  • proc (Proc)

Returns:



10
11
12
# File 'lib/bade/runtime/utils/where.rb', line 10

def proc?(proc)
  source_location(proc)
end