Method: YARD::Handlers::Ruby::Legacy::ClassConditionHandler#parse_condition

Defined in:
lib/yard/handlers/ruby/legacy/class_condition_handler.rb

#parse_conditiontrue, ... (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)

Since:

  • 0.5.5



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
62
63
64
65
# File 'lib/yard/handlers/ruby/legacy/class_condition_handler.rb', line 29

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.tokens[1..-1].to_s.strip
  when /^(\d+)$/
    condition = $1 != "0"
  when /^defined\?\s*\(?\s*([A-Za-z0-9:_]+?)\s*\)?$/
    # 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 = $1
    obj = YARD::Registry.resolve(namespace, name, true)
    begin
      condition = true if obj || Object.instance_eval("defined? #{name}")
    rescue SyntaxError, NameError
      condition = false
    end
  when "true"
    condition = true
  when "false"
    condition = false
  end

  if TkUNLESS === statement.tokens.first
    condition = !condition unless condition.nil?
  end
  condition
end