Sha256: abbb8c85d2e8de9974fb9ab399fc9bd4e9527bd8b915f8c67fad0bd110b8408d

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

class YARD::Handlers::Ruby::ClassConditionHandler < YARD::Handlers::Ruby::Base
  namespace_only
  handles meta_type(:condition)
  
  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
  
  protected
  
  # Parses the condition part of the if/unless statement
  # 
  # @return [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)
  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
  
  def parse_then_block
    parse_block(statement.then_block)
  end
  
  def parse_else_block
    parse_block(statement.else_block) if statement.else_block
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
yard-0.5.3 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.5.2 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.5.1p1 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.5.1 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.5.0 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.4.0 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.2.3.5 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.2.3.4 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.2.3.2 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.2.3.3 lib/yard/handlers/ruby/class_condition_handler.rb
yard-0.2.3 lib/yard/handlers/ruby/class_condition_handler.rb