class Peeky::ClassInfo

Class Info stores information about the class instance that is provided.

The information is collected into MethodInfo objects that live within the signatures accessor.

This information is then separated out into :methods, :attr_accessors, :attr_readers and :attr_writers

Attributes

instance[R]

Holds an instance to the class you are gathering information from

Public Class Methods

new(instance) click to toggle source

Peak into class information

# File lib/peeky/class_info.rb, line 16
def initialize(instance)
  @instance = instance
end

Public Instance Methods

accessors() click to toggle source

Get a list of :attr_accessor on the class @return [Array<AttrInfo>] list of AttrInfo where type is :attr_accessor

# File lib/peeky/class_info.rb, line 75
def accessors
  @_accessors ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_accessor }
end
accessors_source_order() click to toggle source

Get a list of :attr_accessors ordered the way they are in the source code @return [Array<AttrInfo>] list of AttrInfo where type is :attr_accessor

# File lib/peeky/class_info.rb, line 81
def accessors_source_order
  # TODO: This feature is required
  # May be best to have a sort object that can be created for each type of ordering that is needed
  accessors
end
attribute_infos() click to toggle source

Attribute infos

# File lib/peeky/class_info.rb, line 88
def attribute_infos
  @_attribute_infos ||= begin
    grouped_method_infos = signatures.select { |signature| signature.readable? || signature.writable? }.group_by(&:clean_name)

    grouped_method_infos.keys.map { |key| AttrInfo.create(*grouped_method_infos[key]) }
  end
end
class_full_name() click to toggle source

Class full name includes the module namespace

# File lib/peeky/class_info.rb, line 58
def class_full_name
  instance.class.name
end
class_name() click to toggle source

Class name

# File lib/peeky/class_info.rb, line 63
def class_name
  @_class_name ||= class_full_name.to_s.gsub(/^.*::/, '')
  # instance.class.name.split('::').last
end
load() click to toggle source

Load class_info

Accessing information about methods and parameters is currently lazy-loaded via memoization.

At times during debug or other edge cases, it may be useful to pre-load this information early.

# File lib/peeky/class_info.rb, line 51
def load
  ruby_instance_methods
  ruby_instance_method_names
  signatures
end
method_by_name(name) click to toggle source

Method by name

@param name [String] name (required)

# File lib/peeky/class_info.rb, line 99
def method_by_name(name)
  signatures_by_name(name, filter_type: :method).first
end
methods() click to toggle source

Get a list methods @return [Array<MethodInfo>] list of MethodInfo where type is :method

# File lib/peeky/class_info.rb, line 105
def methods
  @_methods ||= signatures.select { |signature| signature.implementation_type == :method }
end
methods_source_order() click to toggle source

Get a list methods ordered the way they are in the source code @return [Array<MethodInfo>] list of MethodInfo

# File lib/peeky/class_info.rb, line 111
def methods_source_order
  # TODO: This feature is required
  # May be best to have a sort object that can be created for each type of ordering that is needed
  methods
end
module_name() click to toggle source

Module name

# File lib/peeky/class_info.rb, line 69
def module_name
  @_module_name ||= class_full_name.to_s.gsub(/(.*)::.*/, '\1')
end
reader_by_name(name) click to toggle source

Reader by name

@param name [String] name (required)

# File lib/peeky/class_info.rb, line 120
def reader_by_name(name)
  signatures_by_name(name, filter_type: :attr_reader).first
end
readers() click to toggle source

Get a list of :attr_reader on the class @return [Array<AttrInfo>] list of AttrInfo where type is :attr_accessor

# File lib/peeky/class_info.rb, line 126
def readers
  @_readers ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_reader }
end
readers_source_order() click to toggle source

Get a list of :attr_reader ordered the way they are in the source code @return [Array<AttrInfo>] list of AttrInfo where type is :attr_reader

# File lib/peeky/class_info.rb, line 132
def readers_source_order
  # TODO: This feature is required
  # May be best to have a sort object that can be created for each type of ordering that is needed
  readers
end
signatures() click to toggle source

def signatures(types = [:instance]) REFACT: Support different types

such as static, private vs public
deep, deep_to_level, this_instance.
# File lib/peeky/class_info.rb, line 163
def signatures
  @_signatures ||= ruby_instance_methods.map { |im| MethodInfo.new(im, @instance) }
end
signatures_by_clean_name(clean_name) click to toggle source

Signatures by clean name

@param clean_name [String] clean name (required)

# File lib/peeky/class_info.rb, line 170
def signatures_by_clean_name(clean_name)
  signatures.select { |im| im.clean_name == clean_name }
end
signatures_by_name(name, filter_type: :all) click to toggle source

Signatures by name

@param name [String] name (required) @param filter_type [String] filter_type: <value for filter type> (optional)

# File lib/peeky/class_info.rb, line 178
def signatures_by_name(name, filter_type: :all)
  return signatures.select { |im| im.name == name } if filter_type == :all

  signatures.select { |im| im.name == name && im.implementation_type == filter_type }
end
to_s() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/peeky/class_info.rb, line 21
def to_s
  result = []
  result.push kv('class', class_full_name)
  if defined?(@_ruby_instance_method_names)
    result.push kv('# of instance methods', @_ruby_instance_method_names.join(', '))
  else
    result.push kv('# of instance methods', '')
  end
  if defined?(@_signatures)
    result.push kv('# of accessors', accessors.length)
    result.push kv('# of readers', readers.length)
    result.push kv('# of writers', writers.length)
    result.push kv('# of methods', methods.length)
  else
    result.push kv('# of accessors', '')
    result.push kv('# of readers', '')
    result.push kv('# of writers', '')
    result.push kv('# of methods', '')
  end
  result.join("\n")
end
writer_by_name(name) click to toggle source

Writer by name

@param name [String] name (required)

# File lib/peeky/class_info.rb, line 155
def writer_by_name(name)
  signatures_by_name(name, filter_type: :attr_writer).first
end
writers() click to toggle source

Get a list of :attr_writer on the class @return [Array<AttrInfo>] list of AttrInfo where type is :attr_writer

# File lib/peeky/class_info.rb, line 140
def writers
  @_writers ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_writer }
end
writers_source_order() click to toggle source

Get a list of :attr_writer ordered the way they are in the source code @return [Array<AttrInfo>] list of AttrInfo where type is :attr_writer

# File lib/peeky/class_info.rb, line 146
def writers_source_order
  # TODO: This feature is required
  # May be best to have a sort object that can be created for each type of ordering that is needed
  writers
end

Private Instance Methods

kv(key, value) click to toggle source
# File lib/peeky/class_info.rb, line 186
def kv(key, value)
  "#{key.to_s.ljust(25)}: #{value}"
end
ruby_instance_method_names() click to toggle source
# File lib/peeky/class_info.rb, line 190
def ruby_instance_method_names
  @_ruby_instance_method_names ||= instance.class.instance_methods(false).sort
end
ruby_instance_methods() click to toggle source
# File lib/peeky/class_info.rb, line 194
def ruby_instance_methods
  @_ruby_instance_methods ||= ruby_instance_method_names.map { |method_name| instance.method(method_name) }
rescue StandardError => e
  puts e
end