Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/cog/primitive.rb,
lib/cog/helpers/file_scanner.rb,
lib/cog/native_extensions/string.rb
Instance Method Summary collapse
-
#cog_source_and_type ⇒ String
Source and type, where type is one of
:project
,:user
,:built_in
,:gem
, or:unknown
. - #normalize_eol ⇒ Object
-
#relative_to(prefix) ⇒ String
This string as a file system path relative to the
prefix
. -
#relative_to_project_root ⇒ String
Strips Cog::Config::ProjectConfig#project_root from the beginning of this string.
-
#relative_to_which_plugin? ⇒ Cog::Plugin?
If this string can be interpretted as a path relative to one of the registered cog plugins, return that plugin, otherwise return
nil
. -
#to_ident ⇒ String
A safe identifier name in the Cog.active_language so as not to conflict with any reserved words.
-
#to_lit ⇒ String
Literal representation in the Cog.active_language.
-
#without_extension(ext) ⇒ String
A copy of this string with the given extension removed.
Instance Method Details
#cog_source_and_type ⇒ String
Returns 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.(File.join(Cog.gem_dir, '..')) ['gem', :gem] else ['unknown', :unknown] end end |
#normalize_eol ⇒ Object
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
.
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. self else prefix && start_with?(prefix.to_s) ? slice(prefix.to_s.length+1..-1) : dup end end |
#relative_to_project_root ⇒ String
Returns strips Cog::Config::ProjectConfig#project_root from the beginning of this string.
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. 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
.
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_ident ⇒ String
Returns a safe identifier name in the Cog.active_language so as not to conflict with any reserved words.
76 77 78 |
# File 'lib/cog/primitive.rb', line 76 def to_ident Cog.active_language.to_ident(self) end |
#to_lit ⇒ String
Returns literal representation in the Cog.active_language.
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.
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 |