Class: RubyLsp::Ree::ReeTemplateApplicator

Inherits:
Object
  • Object
show all
Includes:
ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb

Constant Summary collapse

TEMPLATES_FOLDER =
'.vscode-ree/templates'
DEFAULT_TEMPLATE_FILENAME =
'default.rb'
RSPEC_TEMPLATE_PATH =
'spec_template.rb'

Constants included from ReeLspUtils

RubyLsp::Ree::ReeLspUtils::Entry

Instance Method Summary collapse

Methods included from ReeLspUtils

#camelize, #find_local_file_path, #get_range_for_fn_insert, #get_ree_type, #get_uri_path, #package_name_from_spec_uri, #package_name_from_uri, #package_path_from_uri, #parameter_name, #path_from_package_folder, #signature_params_from_node, #spec_relative_file_path_from_uri, #underscore

Constructor Details

#initializeReeTemplateApplicator

Returns a new instance of ReeTemplateApplicator.



10
11
12
13
14
15
16
17
18
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 10

def initialize
  return unless template_dir_exists?

  @template_types = Dir
    .entries(TEMPLATES_FOLDER)
    .select{ |entry| 
      File.directory? File.join(TEMPLATES_FOLDER,entry) and !(entry =='.' || entry == '..')
    }
end

Instance Method Details

#apply(change_item) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 24

def apply(change_item)
  uri = change_item[:uri]
  path = get_uri_path(uri)

  return unless File.exist?(path)

  file_content = File.read(path)
  return if file_content.size > 0

  if path.end_with?('_spec.rb')
    template_str = fetch_rspec_template
    template_info = fetch_rspec_template_info(uri)
  else
    template_type = get_template_type_from_uri(uri)
    return unless template_type
  
    template_str = fetch_template(template_type)
    template_info = fetch_template_info(uri)
  end
  
  template_content = replace_placeholders(template_str, template_info)

  File.write(path, template_content)
end

#fetch_rspec_templateObject



59
60
61
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 59

def fetch_rspec_template
  File.read(File.join(TEMPLATES_FOLDER, RSPEC_TEMPLATE_PATH))
end

#fetch_rspec_template_info(uri) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 77

def fetch_rspec_template_info(uri)
  object_name = File.basename(uri, '.rb')
  object_class = object_name.split('_').collect(&:capitalize).join
  package_name = package_name_from_spec_uri(uri)
  package_class = package_name.split('_').collect(&:capitalize).join
  file_path = spec_relative_file_path_from_uri(URI(uri))&.delete_suffix('_spec')
  
  {
    'RELATIVE_FILE_PATH' => file_path,
    'MODULE_NAME' => package_class,
    'CLASS_NAME' => object_class,
    'OBJECT_NAME' => object_name, 
    'PACKAGE_NAME' => package_name,
  }
end

#fetch_template(template_type) ⇒ Object



55
56
57
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 55

def fetch_template(template_type)
  File.read(File.join(TEMPLATES_FOLDER, template_type, DEFAULT_TEMPLATE_FILENAME))
end

#fetch_template_info(uri) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 63

def fetch_template_info(uri)
  object_name = File.basename(uri, '.rb')
  object_class = object_name.split('_').collect(&:capitalize).join
  package_name = package_name_from_uri(uri)
  package_class = package_name.split('_').collect(&:capitalize).join
  
  {
    'PACKAGE_MODULE' => package_class,
    'PACKAGE_NAME' => package_name,
    'OBJECT_CLASS' => object_class,
    'OBJECT_NAME' => object_name,
  }
end

#get_template_type_from_uri(uri) ⇒ Object



49
50
51
52
53
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 49

def get_template_type_from_uri(uri)
  uri_parts = File.dirname(uri).split('/')

  uri_parts.reverse.detect{ @template_types.include?(_1) }
end

#replace_placeholders(template_str, template_info) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 93

def replace_placeholders(template_str, template_info)
  template_info.each do |k,v|
    template_str.gsub!(k, v)
  end

  template_str
end

#template_dir_exists?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_template_applicator.rb', line 20

def template_dir_exists?
  File.exist?(TEMPLATES_FOLDER)
end