Class: Peeky::ClassInfo

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ ClassInfo

Peak into class information


16
17
18
# File 'lib/peeky/class_info.rb', line 16

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

#instanceObject (readonly)

Holds an instance to the class you are gathering information from


13
14
15
# File 'lib/peeky/class_info.rb', line 13

def instance
  @instance
end

Instance Method Details

#accessorsArray<AttrInfo>

Get a list of :attr_accessor on the class

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_accessor


55
56
57
# File 'lib/peeky/class_info.rb', line 55

def accessors
  @_accessors ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_accessor }
end

#accessors_source_orderArray<AttrInfo>

Get a list of :attr_accessors ordered the way they are in the source code

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_accessor


61
62
63
64
65
# File 'lib/peeky/class_info.rb', line 61

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_infosObject

Attribute infos


68
69
70
71
72
73
74
# File 'lib/peeky/class_info.rb', line 68

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_nameObject

Class full name includes the module namespace


38
39
40
# File 'lib/peeky/class_info.rb', line 38

def class_full_name
  instance.class.name
end

#class_nameObject

Class name


43
44
45
46
# File 'lib/peeky/class_info.rb', line 43

def class_name
  @_class_name ||= class_full_name.to_s.gsub(/^.*::/, '')
  # instance.class.name.split('::').last
end

#debug(format: [:signatures]) ⇒ Object

Debug

Refact: PATTERN: Come up it an debug inclusion system so that so that debug helpers can be included for development and excluded for production

Parameters:

  • format (String) (defaults to: [:signatures])

    format: <value for format> (optional)


170
171
172
173
174
175
176
177
178
179
# File 'lib/peeky/class_info.rb', line 170

def debug(format: [:signatures])
  debug_method_names if format.include?(:method_names)

  return unless format.include?(:signatures)

  puts '-' * 70
  puts 'Methods'
  puts '-' * 70
  signatures.each(&:debug)
end

#debug_method_namesObject


181
182
183
184
185
186
187
188
# File 'lib/peeky/class_info.rb', line 181

def debug_method_names
  puts '-' * 70
  puts 'Method Names'
  puts '-' * 70
  ruby_instance_method_names.each do |method_name|
    puts method_name
  end
end

#loadObject

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.


31
32
33
34
35
# File 'lib/peeky/class_info.rb', line 31

def load
  ruby_instance_methods
  ruby_instance_method_names
  signatures
end

#method_by_name(name) ⇒ Object

Method by name

Parameters:

  • name (String)

    name (required)


79
80
81
# File 'lib/peeky/class_info.rb', line 79

def method_by_name(name)
  signatures_by_name(name, filter_type: :method).first
end

#methodsArray<MethodInfo>

Get a list methods

Returns:

  • (Array<MethodInfo>)

    list of MethodInfo where type is :method


85
86
87
# File 'lib/peeky/class_info.rb', line 85

def methods
  @_methods ||= signatures.select { |signature| signature.implementation_type == :method }
end

#methods_source_orderArray<MethodInfo>

Get a list methods ordered the way they are in the source code

Returns:


91
92
93
94
95
# File 'lib/peeky/class_info.rb', line 91

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_nameObject

Module name


49
50
51
# File 'lib/peeky/class_info.rb', line 49

def module_name
  @_module_name ||= class_full_name.to_s.gsub(/(.*)::.*/, '\1')
end

#reader_by_name(name) ⇒ Object

Reader by name

Parameters:

  • name (String)

    name (required)


100
101
102
# File 'lib/peeky/class_info.rb', line 100

def reader_by_name(name)
  signatures_by_name(name, filter_type: :attr_reader).first
end

#readersArray<AttrInfo>

Get a list of :attr_reader on the class

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_accessor


106
107
108
# File 'lib/peeky/class_info.rb', line 106

def readers
  @_readers ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_reader }
end

#readers_source_orderArray<AttrInfo>

Get a list of :attr_reader ordered the way they are in the source code

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_reader


112
113
114
115
116
# File 'lib/peeky/class_info.rb', line 112

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

#signaturesObject

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

such as static, private vs public
deep, deep_to_level, this_instance.

143
144
145
# File 'lib/peeky/class_info.rb', line 143

def signatures
  @_signatures ||= ruby_instance_methods.map { |im| MethodInfo.new(im, @instance) }
end

#signatures_by_clean_name(clean_name) ⇒ Object

Signatures by clean name

Parameters:

  • clean_name (String)

    clean name (required)


150
151
152
# File 'lib/peeky/class_info.rb', line 150

def signatures_by_clean_name(clean_name)
  signatures.select { |im| im.clean_name == clean_name }
end

#signatures_by_name(name, filter_type: :all) ⇒ Object

Signatures by name

Parameters:

  • name (String)

    name (required)

  • filter_type (String) (defaults to: :all)

    filter_type: <value for filter type> (optional)


158
159
160
161
162
# File 'lib/peeky/class_info.rb', line 158

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_sObject


20
21
22
# File 'lib/peeky/class_info.rb', line 20

def to_s
  class_full_name
end

#writer_by_name(name) ⇒ Object

Writer by name

Parameters:

  • name (String)

    name (required)


135
136
137
# File 'lib/peeky/class_info.rb', line 135

def writer_by_name(name)
  signatures_by_name(name, filter_type: :attr_writer).first
end

#writersArray<AttrInfo>

Get a list of :attr_writer on the class

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_writer


120
121
122
# File 'lib/peeky/class_info.rb', line 120

def writers
  @_writers ||= attribute_infos.select { |attribute_info| attribute_info.type == :attr_writer }
end

#writers_source_orderArray<AttrInfo>

Get a list of :attr_writer ordered the way they are in the source code

Returns:

  • (Array<AttrInfo>)

    list of AttrInfo where type is :attr_writer


126
127
128
129
130
# File 'lib/peeky/class_info.rb', line 126

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