Class: YARD::Handlers::Ruby::ClassConditionHandler

Inherits:
Base show all
Defined in:
lib/yard/handlers/ruby/class_condition_handler.rb

Constant Summary

Constants included from YARD::CodeObjects

BUILTIN_ALL, BUILTIN_CLASSES, BUILTIN_EXCEPTIONS, BUILTIN_EXCEPTIONS_HASH, BUILTIN_MODULES, CONSTANTMATCH, CSEP, CSEPQ, ISEP, ISEPQ, METHODMATCH, METHODNAMEMATCH, NAMESPACEMATCH, NSEP, NSEPQ

Instance Method Summary

Methods inherited from Base

handles?, meta_type, method_call, #parse_block

Methods included from YARD::Parser::Ruby

#s

Methods inherited from YARD::Handlers::Base

clear_subclasses, #ensure_loaded!, handlers, handles, handles?, #initialize, namespace_only, namespace_only?, #parse_block, #push_state, #register, subclasses

Constructor Details

This class inherits a constructor from YARD::Handlers::Base

Instance Method Details

- (true, ...) parse_condition (protected)

Parses the condition part of the if/unless statement

Returns:

  • (true, false, nil) — true if the condition can be definitely parsed to true, false if not, and nil if the condition cannot be parsed with certainty (it’s dynamic)


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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yard/handlers/ruby/class_condition_handler.rb', line 25

def parse_condition
  condition = nil
  
  # Right now we can handle very simple unary conditions like:
  #   if true
  #   if false
  #   if 0
  #   if 100 (not 0)
  #   if defined? SOME_CONSTANT
  # 
  # The last case will do a lookup in the registry and then one
  # in the Ruby world (using eval).
  case statement.condition.type
  when :int
    condition = statement.condition[0] != "0"
  when :defined
    # defined? keyword used, let's see if we can look up the name
    # in the registry, then we'll try using Ruby's powers. eval() is not
    # *too* dangerous here since code is not actually executed.
    name = statement.condition[0].source
    obj = YARD::Registry.resolve(namespace, name, true)
    condition = true if obj || Object.instance_eval("defined? #{name}")
  when :var_ref
    var = statement.condition[0]
    if var == s(:kw, "true")
      condition = true
    elsif var == s(:kw, "false")
      condition = false
    end
  end
  
  # Invert an unless condition
  if statement.type == :unless || statement.type == :unless_mod
    condition = !condition if condition != nil
  end
  condition
end

- (Object) parse_else_block (protected)



67
68
69
# File 'lib/yard/handlers/ruby/class_condition_handler.rb', line 67

def parse_else_block
  parse_block(statement.else_block) if statement.else_block
end

- (Object) parse_then_block (protected)



63
64
65
# File 'lib/yard/handlers/ruby/class_condition_handler.rb', line 63

def parse_then_block
  parse_block(statement.then_block)
end

- (Object) process



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/yard/handlers/ruby/class_condition_handler.rb', line 5

def process
  condition = parse_condition
  if condition == nil
    # Parse both blocks if we're unsure of the condition
    parse_then_block
    parse_else_block
  elsif condition
    parse_then_block
  else
    parse_else_block
  end
end