Class: Class

Inherits:
Object show all
Defined in:
lib/core/facets/class/pathize.rb,
lib/core/facets/class/to_proc.rb,
lib/core/facets/module/revise.rb,
lib/core/facets/class/methodize.rb,
lib/core/facets/class/descendants.rb,
lib/core/facets/class/preallocate.rb,
lib/core/facets/module/class_extend.rb

Instance Method Summary collapse

Instance Method Details

#class_extend(*mods, &block) ⇒ Object

For Class, Module#class_extend is similar to class_eval.

The alternative is to "undef_method :class_extend", but this seems uneccessarily limited.



84
85
86
87
88
89
90
# File 'lib/core/facets/module/class_extend.rb', line 84

def class_extend(*mods, &block)
  class_extension = Module.new
  class_extension.__send__(:include, *mods) if mods.any?
  class_extension.module_eval(&block) if block
  extend(class_extension)
  class_extensions << class_extension
end

#descendants(generations = -1)) ⇒ Object

List all descendants of this class. The order differs between Rubies, so these examples sort the list.

class A ; end
class B < A; end
class C < A; end
A.descendants.sort_by(&:name)  #=> [B,C]

You may also limit the generational distance the subclass may be from the parent class.

class X ; end
class Y < X; end
class Z < Y; end
X.descendants.sort_by(&:name)  #=> [Y,Z]
X.descendants(1)               #=> [Y]

Ruby 4.1 adds its own Module#descendants. Where Ruby has it, a call without a limit is passed to Ruby's, and Facets only handles the generations limit. Ruby doesn't define the order of its list.

On older Rubies, the list is in the same order as ActiveSupport's Class#descendants: the subclasses first, then each one's own descendants.

The generations walk goes through #subclasses only, never #descendants, because ActiveSupport wraps #descendants in a version that takes no argument.

NOTE: With ActiveSupport loaded, calling descendants with an argument raises ArgumentError, because of that wrapper.

NOTE: This is a intensive operation. Do not expect it to be very fast.

Author:

  • Roger Pack



38
39
40
41
42
43
44
45
46
# File 'lib/core/facets/class/descendants.rb', line 38

def descendants(generations=-1)
  return super() if generations < 1 && Module.method_defined?(:descendants)
  walk = lambda do |klass, gen|
    subs = klass.subclasses
    next subs if gen == 1
    subs + subs.flat_map { |k| walk.(k, gen - 1) }
  end
  walk.(self, generations)
end

#methodize ⇒ Object

Translate a class name to a suitable method name.

module ::Example
class MethodizeExample
end
end

Example::MethodizeExample.methodize  #=> "example__methodize_example"


14
15
16
# File 'lib/core/facets/class/methodize.rb', line 14

def methodize
  name.methodize
end

#pathize ⇒ Object

Converts a class name to a unix path.

module ::Example
class PathizeExample
end
end

Example::PathizeExample.pathize  #=> "example/pathize_example"


14
15
16
# File 'lib/core/facets/class/pathize.rb', line 14

def pathize
  name.pathize
end

#preallocate(aspect) ⇒ Object

Designate aspect modules to be added to a object at instantiation.

class Firetruck
def put_out_fire(option)
  "Put out #{option}"
end
end

module FastFiretruck
def put_out_fire(option)
  super("very #{option}!")
end
end

Firetruck.preallocate(FastFiretruck)

ft = Firetruck.new
ft.put_out_fire('fast') #=> "Put out very fast!"

This method is very similar to the idea of #prepend, but it has some limitations in that it works by overriding #new and #allocate and extends an object with the aspect modules on instantiation. A true #prepend implementation would not have to do this --but would be a natural part of the class heirarchy instead. For this reason, this method has been named #preallocate, rather than #prepend.

NOTE: This method is not a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: Trans



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/core/facets/class/preallocate.rb', line 37

def preallocate(aspect)
  _new      = method(:new)
  _allocate = method(:allocate)
  (class << self; self; end).class_eval do
    define_method(:new) do |*args|
      o = _new.call(*args)
      o.extend aspect
      o
    end
    define_method(:allocate) do |*args|
      o = _allocate.call(*args)
      o.extend aspect
      o
    end
  end
end

#to_proc ⇒ Object

Convert instatiation of a class into a Proc.

class Person
def initialize(name)
  @name = name
end

def inspect
  @name.to_str
end
end

persons = %w(john bob jane hans).map(&Person)

persons.map{ |p| p.inspect }  #=> ['john', 'bob', 'jane', 'hans']

CREDIT: Daniel Schierbeck



20
21
22
# File 'lib/core/facets/class/to_proc.rb', line 20

def to_proc
  proc{|*args| new(*args)}
end