
module YARD::CodeObjects

  class StepTransformerObject < Base

    include Cucumber::LocationHelper

    attr_reader :constants, :keyword, :source, :value
    attr_accessor :steps

    ESCAPE_PATTERN = /#\{\s*(\w+)\s*\}/

    def value=(value)
      @value = format_source(value)

      until (nested = constants_from_value).empty?
        nested.each {|n| @value.gsub!(value_regex(n),find_value_for_constant(n)) }
      end
      
      @steps = []
    end

    def regex
      @regex ||= /#{strip_regex_from(@value)}/
    end

    def constants_from_value(data=@value)
      data.scan(ESCAPE_PATTERN).flatten.collect { |value| value.strip }
    end

    protected

    def find_value_for_constant(name)
      @@constants ||= YARD::Registry.all(:constant)
      constant = @@constants.find{|constant| constant.name == name.to_sym }
      strip_regex_from(constant.value) if constant
    end
  
    def value_regex(value)
      /#\{\s*#{value}\s*\}/
    end

    def strip_regex_from(value)
      value.gsub(/^\/|\/$/,'')
    end


  end

end

module YARD::CodeObjects

  class StepTransformerObject < Base

    include Cucumber::LocationHelper

    attr_reader :constants, :keyword, :source, :value
    attr_accessor :steps

    ESCAPE_PATTERN = /#\{\s*(\w+)\s*\}/

    def value=(value)
      @value = format_source(value)

      until (nested = constants_from_value).empty?
        nested.each {|n| @value.gsub!(value_regex(n),find_value_for_constant(n)) }
      end
      
      @steps = []
    end

    def regex
      @regex ||= /#{strip_regex_from(@value)}/
    end

    def constants_from_value(data=@value)
      data.scan(ESCAPE_PATTERN).flatten.collect { |value| value.strip }
    end

    protected

    def find_value_for_constant(name)
      constant = YARD::Registry.all(:constant).find{|c| c.name == name.to_sym }
      log.warn "StepTransformer#find_value_for_constant : Could not find the CONSTANT #{name} using an empty string for value." unless constant
      constant ? strip_regex_from(constant.value) : ""
    end
  
    def value_regex(value)
      /#\{\s*#{value}\s*\}/
    end

    def strip_regex_from(value)
      value.gsub(/^\/|\/$/,'')
    end


  end

end
Fix Step Tranformer Caching Issue
Fix Step Tranformer Caching Issue

- Thought that I could cache the Constants Object from the Registry
  but it turns out that it cannot be done and calls to the Registry
  must be made each time; otherwise the cache of Constants is out
  of date after the first call by a StepTransformer.
