Class: Peeky::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/peeky/api.rb

Overview

API has factory and creational patterns for easy usage of the Peeky reflection system

Instance Method Summary collapse

Instance Method Details

#build_class_info(instance, lazy: true) ⇒ Object

Build a ClassInfo structure based around a ruby class instance.

ClassInfo stores information about the instance of a class that is passed in including methods, attr_accessors attr_readers and attr_writers.


19
20
21
22
23
# File 'lib/peeky/api.rb', line 19

def build_class_info(instance, lazy: true)
  ci = Peeky::ClassInfo.new(instance)
  ci.load unless lazy
  ci
end

#class_renderer(key) ⇒ Object

Get a class renderer by :key

TODO: Refactor to a configurable system


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/peeky/api.rb', line 56

def class_renderer(key)
  case key
  when :class_debug
    Peeky::Renderer::ClassDebugRender
  when :class_interface
    Peeky::Renderer::ClassInterfaceRender
  when :class_interface_yard
    Peeky::Renderer::ClassInterfaceYardRender
  else
    raise "Unknown class renderer: #{key}"
  end
end

#method_renderer(key) ⇒ Object

Get a method renderer by :key

TODO: Refactor to a configurable system


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/peeky/api.rb', line 40

def method_renderer(key)
  case key
  when :signature
    Peeky::Renderer::MethodSignatureRender
  when :signature_with_debug
    Peeky::Renderer::MethodSignatureWithDebugRender
  when :call_minimum_params
    Peeky::Renderer::MethodCallMinimumParamsRender
  else
    raise "Unknown method renderer: #{key}"
  end
end

#render_class(render_key, class_info: nil, instance: nil, **_opts) ⇒ Object

Render a class using a predefined class renderer


26
27
28
29
30
31
32
33
34
35
# File 'lib/peeky/api.rb', line 26

def render_class(render_key, class_info: nil, instance: nil, **_opts)
  raise 'Call render_class with class_info OR instance.' if class_info.nil? && instance.nil?
  raise 'Call render_class with class_info OR instance, these parameters are mutually exclusive' if !class_info.nil? && !instance.nil?

  renderer = class_renderer(render_key)

  class_info = Peeky::ClassInfo.new(instance) if class_info.nil?

  renderer.new(class_info).render
end