Module: Reflexive

Defined in:
lib/reflexive/helpers.rb,
lib/reflexive/methods.rb,
lib/reflexive/columnizer.rb,
lib/reflexive/application.rb,
lib/reflexive/constantize.rb,
lib/reflexive/descendants.rb,
lib/reflexive/method_lookup.rb,
lib/reflexive/routing_helpers.rb,
lib/reflexive/variables_scope.rb,
lib/reflexive/reflexive_ripper.rb,
lib/reflexive/coderay_html_encoder.rb,
lib/reflexive/coderay_ruby_scanner.rb,
lib/reflexive/parse_tree_top_down_walker.rb

Defined Under Namespace

Modules: Columnizer, Helpers, RoutingHelpers Classes: Application, CodeRayHtmlEncoder, CodeRayRubyScanner, MethodLookup, Methods, ParseTreeTopDownWalker, ReflexiveRipper, VariablesScope

Constant Summary collapse

FILE_EXT =
/\.\w+\z/
DL_EXT =
/\.s?o\z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_reloading_active?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
# File 'lib/reflexive/helpers.rb', line 6

def self.class_reloading_active?
  if defined?(Rails)
    !Rails.configuration.cache_classes rescue false
  else
    false
  end
end

.constant_lookup(name, scope) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/reflexive/constantize.rb', line 4

def constant_lookup(name, scope)
  if name =~ /^::/
    begin
      return Reflexive.constantize(name)
    rescue NameError, ArgumentError
      return nil
    end
  end

  scope_parts = scope.split("::")
  
  begin
    name_with_scope = "#{ scope_parts.join("::") }::#{ name }"
    return Reflexive.constantize(name_with_scope)
  rescue NameError, ArgumentError
    # For defined top-level module, when looked up from another class:
    # ArgumentError: Object is not missing constant TopLevelConst!
    #        from .../activesupport-2.3.5/lib/active_support/dependencies.rb:417:in `load_missing_constant'
    retry if scope_parts.pop
  end
  
  nil
end

.descendants(klass, generations = nil) ⇒ Object

List all descedents of this class.

class X ; end
class A < X; end
class B < X; end
X.descendents  #=> [A,B]

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

class X ; end
class A < X; end
class B < A; end
X.descendents    #=> [A, B]
X.descendents(1) #=> [A]

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reflexive/descendants.rb', line 25

def descendants(klass, generations=nil)
  subclass = []
  
  ObjectSpace.each_object(Class) do |c|
    next if c == klass
    
    ancestors = c.ancestors[0 .. (generations || -1)]
    subclass << c if ancestors.include?(klass)

    ancestors = c.singleton_class.ancestors[0 .. (generations || -1)]
    subclass << c if ancestors.include?(klass)
  end

  if klass.instance_of?(Module)
    # descendants of module are also modules which include the module
    ObjectSpace.each_object(Module) do |c|
      next if c == klass

      ancestors = c.ancestors[0 .. (generations || -1)]
      subclass << c if ancestors.include?(klass)
    end
  end

  subclass.uniq
end

.load_path_lookup(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/reflexive/helpers.rb', line 16

def self.load_path_lookup(path)
  path_with_rb_ext = path.sub(FILE_EXT, "") + ".rb"

  $LOAD_PATH.each do |load_path|
    if feature = File.join(load_path, path_with_rb_ext)
      return feature if File.exists?(feature)
    end
  end

  nil
end

.loaded_features_lookup(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/reflexive/helpers.rb', line 28

def self.loaded_features_lookup(path)
  path_without_ext = path.sub(FILE_EXT, "")

  $LOADED_FEATURES.reverse.reject do |feature|
    feature =~ DL_EXT
  end.detect do |feature|
    feature_without_ext = feature.sub(FILE_EXT, "")

    feature_without_ext =~ /\/#{ Regexp.escape(path_without_ext) }\z/
  end    
end

Instance Method Details

#constantize(camel_cased_word) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
56
57
58
# File 'lib/reflexive/constantize.rb', line 49

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end