You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

constantize.rb 1.1 kB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. require 'cucumber/platform'
  3. module Cucumber
  4. module Constantize # :nodoc:
  5. def constantize(camel_cased_word)
  6. try = 0
  7. begin
  8. try += 1
  9. names = camel_cased_word.split('::')
  10. names.shift if names.empty? || names.first.empty?
  11. constant = ::Object
  12. names.each do |name|
  13. constant = constantize_name(constant, name)
  14. end
  15. constant
  16. rescue NameError => e
  17. require underscore(camel_cased_word)
  18. retry if try < 2
  19. raise e
  20. end
  21. end
  22. # Snagged from active_support
  23. def underscore(camel_cased_word)
  24. camel_cased_word.to_s.gsub(/::/, '/')
  25. .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  26. .gsub(/([a-z\d])([A-Z])/, '\1_\2')
  27. .tr('-', '_')
  28. .downcase
  29. end
  30. private
  31. def constantize_name(constant, name)
  32. if constant.const_defined?(name, false)
  33. constant.const_get(name, false)
  34. else
  35. constant.const_missing(name)
  36. end
  37. end
  38. end
  39. end

No Description

Contributors (1)