Class: ImportJS::JSModule
- Inherits:
-
Object
- Object
- ImportJS::JSModule
- Defined in:
- lib/import_js/js_module.rb
Overview
Class that represents a js module found in the file system
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#import_path ⇒ Object
readonly
Returns the value of attribute import_path.
-
#is_destructured ⇒ Object
Returns the value of attribute is_destructured.
-
#lookup_path ⇒ Object
readonly
Returns the value of attribute lookup_path.
-
#main_file ⇒ Object
readonly
Returns the value of attribute main_file.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
Instance Method Summary collapse
-
#display_name ⇒ String
A readable description of the module.
-
#initialize(lookup_path, relative_file_path, strip_file_extensions) ⇒ JSModule
constructor
A new instance of JSModule.
- #to_import_statement(variable_name) ⇒ ImportJS::ImportStatement
Constructor Details
#initialize(lookup_path, relative_file_path, strip_file_extensions) ⇒ JSModule
Returns a new instance of JSModule.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/import_js/js_module.rb', line 16 def initialize(lookup_path, relative_file_path, strip_file_extensions) @lookup_path = lookup_path @file_path = relative_file_path if relative_file_path.end_with? '/package.json' @main_file = JSON.parse(File.read(relative_file_path))['main'] match = relative_file_path.match(/(.*)\/package\.json/) @import_path = match[1] @skip = !@main_file elsif relative_file_path.match(/\/index\.js.*$/) match = relative_file_path.match(/(.*)\/(index\.js.*)/) @main_file = match[2] @import_path = match[1] else @import_path = relative_file_path strip_file_extensions.each do |ext| if @import_path.end_with?(ext) @import_path = @import_path[0...-ext.length] break end end end if lookup_path @import_path = @import_path.sub("#{@lookup_path}\/", '') # remove path prefix end end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
6 7 8 |
# File 'lib/import_js/js_module.rb', line 6 def file_path @file_path end |
#import_path ⇒ Object (readonly)
Returns the value of attribute import_path.
4 5 6 |
# File 'lib/import_js/js_module.rb', line 4 def import_path @import_path end |
#is_destructured ⇒ Object
Returns the value of attribute is_destructured.
9 10 11 |
# File 'lib/import_js/js_module.rb', line 9 def is_destructured @is_destructured end |
#lookup_path ⇒ Object (readonly)
Returns the value of attribute lookup_path.
5 6 7 |
# File 'lib/import_js/js_module.rb', line 5 def lookup_path @lookup_path end |
#main_file ⇒ Object (readonly)
Returns the value of attribute main_file.
7 8 9 |
# File 'lib/import_js/js_module.rb', line 7 def main_file @main_file end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
8 9 10 |
# File 'lib/import_js/js_module.rb', line 8 def skip @skip end |
Instance Method Details
#display_name ⇒ String
Returns a readable description of the module.
44 45 46 47 48 |
# File 'lib/import_js/js_module.rb', line 44 def display_name parts = [import_path] parts << " (main: #{@main_file})" if @main_file parts.join('') end |
#to_import_statement(variable_name) ⇒ ImportJS::ImportStatement
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/import_js/js_module.rb', line 52 def to_import_statement(variable_name) ImportJS::ImportStatement.new.tap do |statement| if is_destructured statement.inject_destructured_variable(variable_name) else statement.default_variable = variable_name end statement.path = import_path end end |