class YARD::Handlers::Ruby::ConstantTransformHandler

There might be a nicer way to decorate this class but with my limited knowledge could only get this handler to be applied after the default constant handler by inheriting from the default constant handler. This is required so that the value assigned from the transform is not overridden in the registry by the default handler

Private Instance Methods

constants_from_value(data) click to toggle source
# File lib/yard/handlers/constant_transform_handler.rb, line 77
def constants_from_value(data)
  escape_pattern = /#\{\s*(\w+)\s*\}/
  data.scan(escape_pattern).flatten.collect { |value| value.strip }
end
convert_captures(regexp_source) click to toggle source

Cucumber's Transform object overrides the to_s function and strips the anchor tags ^$ and any captures so that id it is interpolated in a step definition the it can appear anywhere in the step without being effected by position or captures

# File lib/yard/handlers/constant_transform_handler.rb, line 57
def convert_captures(regexp_source)
  regexp_source
    .gsub(/(\()(?!\?[<:=!])/,'(?:')
    .gsub(/(\(\?<)(?![=!])/,'(?:<')
end
find(node, node_type, value) click to toggle source
# File lib/yard/handlers/constant_transform_handler.rb, line 49
def find(node, node_type, value)
  node.traverse { |child| return(child) if node_type == child.type && child.source == value }
  self
end
find_value_for_constant(name) click to toggle source
# File lib/yard/handlers/constant_transform_handler.rb, line 87
def find_value_for_constant(name)
  constant = YARD::Registry.all(:constant).find{|c| c.name == name.to_sym }
  log.warn "ConstantTransformHandler#find_value_for_constant : Could not find the CONSTANT [#{name}] using the string value." unless constant
  constant ? strip_regex_from(constant.value) : name
end
strip_anchors(regexp_source) click to toggle source
# File lib/yard/handlers/constant_transform_handler.rb, line 63
def strip_anchors(regexp_source)
  regexp_source.
    gsub(/(^\(\/|\/\)$)/, '').
    gsub(/(^\^|\$$)/, '')
end
strip_regex_from(value) click to toggle source

Step the regex starting / and ending / from the value

# File lib/yard/handlers/constant_transform_handler.rb, line 94
def strip_regex_from(value)
  value.gsub(/^\/|\/$/,'')
end
substitute(data) click to toggle source

Support for interpolation in the Transform's Regex

# File lib/yard/handlers/constant_transform_handler.rb, line 70
def substitute(data)
  until (nested = constants_from_value(data)).empty?
    nested.each {|n| data.gsub!(value_regex(n),find_value_for_constant(n)) }
  end
  data
end
value_regex(value) click to toggle source

Return a regex of the value

# File lib/yard/handlers/constant_transform_handler.rb, line 83
def value_regex(value)
  /#\{\s*#{value}\s*\}/
end