class String

Public Instance Methods

cog_source_and_type() click to toggle source

@api developer @return [String, String] source and type, where type is one of :project, :user, :built_in, :gem, or :unknown

# File lib/cog/native_extensions/string.rb, line 45
def cog_source_and_type
  if ((Cog.project_root && start_with?(Cog.project_root)) ||
      (Cog.project_template_path && start_with?(Cog.project_template_path)) || 
      (Cog.project_generator_path && start_with?(Cog.project_generator_path)) ||
      (Cog.project_plugin_path && start_with?(Cog.project_plugin_path)))
    [File.basename(Cog.project_root), :project]
  elsif start_with? Cog.user_dir
    [File.basename(ENV['HOME']), :user]
  elsif start_with? Cog.gem_dir
    ['cog', :built_in]
  elsif start_with? File.expand_path(File.join(Cog.gem_dir, '..'))
    ['gem', :gem]
  else
    ['unknown', :unknown]
  end
end
normalize_eol() click to toggle source
# File lib/cog/helpers/file_scanner.rb, line 2
def normalize_eol
  gsub(/\r\n/, "\n").gsub(/\r/, "\n")
end
relative_to(prefix) click to toggle source

@api developer @param prefix [String] path prefix to strip from the beginning of this string @return [String] this string as a file system path relative to the prefix

# File lib/cog/native_extensions/string.rb, line 16
def relative_to(prefix)
  if Cog.show_fullpaths?
    File.expand_path self
  else
    prefix && start_with?(prefix.to_s) ? slice(prefix.to_s.length+1..-1) : dup
  end
end
relative_to_project_root() click to toggle source

@api developer @return [String] strips {Cog::Config::ProjectConfig#project_root} from the beginning of this string

# File lib/cog/native_extensions/string.rb, line 5
def relative_to_project_root
  if Cog.show_fullpaths?
    File.expand_path self
  else
    Cog.project? ? relative_to(Cog.project_root) : dup
  end
end
relative_to_which_plugin?() click to toggle source

@api developer @return [Cog::Plugin,nil] if this string can be interpretted as a path relative to one of the registered cog plugins, return that plugin, otherwise return nil

# File lib/cog/native_extensions/string.rb, line 26
def relative_to_which_plugin?
  Cog.plugins.each do |plugin|
    return plugin if start_with?(plugin.path)
  end
  nil
end
to_ident() click to toggle source

@return [String] a safe identifier name in the {Cog::Config::LanguageConfig#active_language Cog.active_language} so as not to conflict with any {Cog::DSL::LanguageDSL#reserved reserved words} @example

# For Java
"boolean".to_ident # => 'boolean_'
"bool".to_ident    # => 'bool'

# For C#
"boolean".to_ident # => 'boolean'
"bool".to_ident    # => 'bool_'
# File lib/cog/primitive.rb, line 72
def to_ident
  Cog.active_language.to_ident(self)
end
to_lit() click to toggle source

@return [String] literal representation in the {Cog::Config::LanguageConfig#active_language Cog.active_language} @example

# For Objective-C
"my cat walks over the keyboard".to_lit # => '@"my cat walks over the keyboard"'

# For Java
"my cat walks over the keyboard".to_lit # => '"my cat walks over the keyboard"'
# File lib/cog/primitive.rb, line 59
def to_lit
  Cog.active_language.to_char(self) || Cog.active_language.to_string(self)
end
without_extension(ext) click to toggle source

@api developer @param ext [String] file extension to remove from the end of this string @return [String] a copy of this string with the given extension removed. Does nothing if this string does not edit with the extension

# File lib/cog/native_extensions/string.rb, line 36
def without_extension(ext)
  return dup if ext.nil?
  ext = ext.to_s
  ext = '.' + ext unless ext.start_with? '.'
  end_with?(ext) ? slice(0..(-ext.length - 1)) : dup
end