Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cog/primitive.rb,
lib/cog/helpers/file_scanner.rb,
lib/cog/native_extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#cog_source_and_typeString

Returns source and type, where type is one of :project, :user, :built_in, :gem, or :unknown.

Returns:

  • (String, String)

    source and type, where type is one of :project, :user, :built_in, :gem, or :unknown



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_eolObject



2
3
4
# File 'lib/cog/helpers/file_scanner.rb', line 2

def normalize_eol
  gsub(/\r\n/, "\n").gsub(/\r/, "\n")
end

#relative_to(prefix) ⇒ String

Returns this string as a file system path relative to the prefix.

Parameters:

  • prefix (String)

    path prefix to strip from the beginning of this string

Returns:

  • (String)

    this string as a file system path relative to the prefix



16
17
18
19
20
21
22
# 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_rootString

Returns strips Cog::Config::ProjectConfig#project_root from the beginning of this string.

Returns:



5
6
7
8
9
10
11
# 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?Cog::Plugin?

Returns if this string can be interpretted as a path relative to one of the registered cog plugins, return that plugin, otherwise return nil.

Returns:

  • (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



26
27
28
29
30
31
# 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_identString

Returns a safe identifier name in the Cog.active_language so as not to conflict with any reserved words.

Examples:

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

# For C#
"boolean".to_ident # => 'boolean'
"bool".to_ident    # => 'bool_'

Returns:



76
77
78
# File 'lib/cog/primitive.rb', line 76

def to_ident
  Cog.active_language.to_ident(self)
end

#to_litString

Returns literal representation in the Cog.active_language.

Examples:

# 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"'

Returns:



63
64
65
# File 'lib/cog/primitive.rb', line 63

def to_lit
  Cog.active_language.to_char(self) || Cog.active_language.to_string(self)
end

#without_extension(ext) ⇒ String

Returns a copy of this string with the given extension removed. Does nothing if this string does not edit with the extension.

Parameters:

  • ext (String)

    file extension to remove from the end of this string

Returns:

  • (String)

    a copy of this string with the given extension removed. Does nothing if this string does not edit with the extension



36
37
38
39
40
41
# 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