Module: Omakase::Doc

Defined in:
lib/omakase/doc.rb

Overview

What an object offers, for a model that has never seen its type: what the object's own classes define, not what Ruby gives everything.

Constant Summary collapse

CORE =
[Object, Kernel, BasicObject, Struct, Data, Enumerable, Comparable].freeze

Class Method Summary collapse

Class Method Details

.boundaryObject

A framework's base class defines hundreds of methods the model has no use for. What it wants is what this class adds — its columns, its associations, and the methods you wrote.



14
# File 'lib/omakase/doc.rb', line 14

def boundary = defined?(ActiveRecord::Base) ? [*CORE, ActiveRecord::Base] : CORE

.ivars(object) ⇒ Object



25
# File 'lib/omakase/doc.rb', line 25

def ivars(object) = object.instance_variables.to_h { |name| [name, object.instance_variable_get(name)] }

.of(object) ⇒ Object



16
# File 'lib/omakase/doc.rb', line 16

def of(object) = [object.class.to_s, *signatures(object), *state(object)].join("\n")

.signatures(object) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/omakase/doc.rb', line 27

def signatures(object)
  object.class.ancestors
    .take_while { |mod| !boundary.include?(mod) }
    .reject { |mod| mod.to_s.end_with?("GeneratedAttributeMethods") }
    .flat_map { |mod| mod.public_instance_methods(false) }
    .uniq.sort
    .reject { |name| name.match?(/\A_|_associated_records_for_/) }
    .map { |name| "  #{name}(#{Capabilities.parameters(object.method(name))})" }
end

.state(object) ⇒ Object

An object that answers attributes says what it holds better than its instance variables do — and a record's columns are state, not API.



20
21
22
23
# File 'lib/omakase/doc.rb', line 20

def state(object)
  values = object.respond_to?(:attributes) ? object.attributes : ivars(object)
  values.map { |name, value| "  #{name} = #{value.inspect}" }
end