Module: GrapeDoc::Helpers
- Defined in:
- lib/grape/doc/helper.rb
Class Method Summary collapse
-
.camelize(term) ⇒ Object
By default,
camelizeconverts strings to UpperCamelCase. -
.constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string.
- .doc_folder_path ⇒ Object
- .each_grape_class ⇒ Object
- .poc_data ⇒ Object
- .poc_file_path ⇒ Object
Class Method Details
.camelize(term) ⇒ Object
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
'active_model'.camelize # => "ActiveModel"
'active_model'.camelize(:lower) # => "activeModel"
'active_model/errors'.camelize # => "ActiveModel::Errors"
'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:
'SSLError'.underscore.camelize # => "SslError"
99 100 101 102 103 104 105 106 |
# File 'lib/grape/doc/helper.rb', line 99 def camelize(term) string = term.to_s string.capitalize! string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } string.gsub!('/', '::') string end |
.constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string.
'Module'.constantize # => Module
'Test::Unit'.constantize # => Test::Unit
The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:
C = 'outside'
module M
C = 'inside'
C # => 'inside'
'C'.constantize # => 'outside', same as ::C
end
NameError is raised when the name is not in CamelCase or the constant is unknown.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/grape/doc/helper.rb', line 51 def constantize(camel_cased_word) names = camel_cased_word.split('::') # Trigger a builtin NameError exception including the ill-formed constant in the message. Object.const_get(camel_cased_word) if names.empty? # Remove the first blank element in case of '::ClassName' notation. names.shift if names.size > 1 && names.first.empty? names.inject(Object) do |constant, name| if constant == Object constant.const_get(name) else candidate = constant.const_get(name) next candidate if constant.const_defined?(name, false) next candidate unless Object.const_defined?(name) # Go down the ancestors to check it it's owned # directly before we reach Object or the end of ancestors. constant = constant.ancestors.inject do |const, ancestor| break const if ancestor == Object break ancestor if ancestor.const_defined?(name, false) const end # owner is in Object, so raise constant.const_get(name, false) end end end |
.doc_folder_path ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/grape/doc/helper.rb', line 12 def doc_folder_path @doc_folder_path ||= -> { doc_folder_path = File.join(RackTestPoc.root,'doc') Dir.mkdir doc_folder_path unless File.exist?(doc_folder_path) return doc_folder_path }.call end |
.each_grape_class ⇒ Object
5 6 7 8 9 10 |
# File 'lib/grape/doc/helper.rb', line 5 def each_grape_class ::ObjectSpace.each_object(::Class) do |api_class| next unless -> { api_class < Grape::API rescue false }.call yield(api_class) end if block_given? end |
.poc_data ⇒ Object
27 28 29 30 31 |
# File 'lib/grape/doc/helper.rb', line 27 def poc_data require 'yaml' YAML.load(File.read(poc_file_path)) rescue;{} end |
.poc_file_path ⇒ Object
21 22 23 24 25 |
# File 'lib/grape/doc/helper.rb', line 21 def poc_file_path return Dir.glob( File.join(RackTestPoc.root,'test','poc','*.{yml,yaml}') ).max_by(&File.method(:ctime)) end |