Class: Modulr::JSModule
- Inherits:
-
Object
- Object
- Modulr::JSModule
- Includes:
- Comparable
- Defined in:
- lib/modulr/js_module.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other_module) ⇒ Object
- #dependencies ⇒ Object
- #dependency_array ⇒ Object
- #ensure(buffer = '') ⇒ Object
- #escaped_src ⇒ Object
- #factory ⇒ Object
- #id ⇒ Object
- #identifier_valid? ⇒ Boolean
-
#initialize(identifier, root, file = nil, line = nil) ⇒ JSModule
constructor
A new instance of JSModule.
- #inspect ⇒ Object
- #path ⇒ Object
- #relative? ⇒ Boolean
- #src ⇒ Object
- #top_level? ⇒ Boolean
Constructor Details
#initialize(identifier, root, file = nil, line = nil) ⇒ JSModule
Returns a new instance of JSModule.
37 38 39 40 41 42 43 44 |
# File 'lib/modulr/js_module.rb', line 37 def initialize(identifier, root, file=nil, line=nil) @identifier = identifier @root = root @file = file @line = line @terms = identifier.split('/').reject { |term| term == '' } raise ModuleIdentifierError.new(self) unless identifier_valid? end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
35 36 37 |
# File 'lib/modulr/js_module.rb', line 35 def file @file end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
35 36 37 |
# File 'lib/modulr/js_module.rb', line 35 def identifier @identifier end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
35 36 37 |
# File 'lib/modulr/js_module.rb', line 35 def line @line end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
35 36 37 |
# File 'lib/modulr/js_module.rb', line 35 def root @root end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
35 36 37 |
# File 'lib/modulr/js_module.rb', line 35 def terms @terms end |
Class Method Details
.find_dependencies(js_module) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/modulr/js_module.rb', line 20 def self.find_dependencies(js_module) begin expressions = parser.get_require_expressions(js_module.src) rescue ParserError raise JavaScriptSyntaxError, js_module end expressions.map do |exp| if exp[:identifier] new(exp[:identifier], js_module.root, js_module.path, exp[:line]) else raise DynamicModuleIdentifierError.new(exp[:src_code], js_module.path, exp[:line]) end end end |
.parser ⇒ Object
16 17 18 |
# File 'lib/modulr/js_module.rb', line 16 def self.parser @dependency_finder ||= Parser.new end |
Instance Method Details
#<=>(other_module) ⇒ Object
46 47 48 |
# File 'lib/modulr/js_module.rb', line 46 def <=> (other_module) id <=> other_module.id end |
#dependencies ⇒ Object
99 100 101 |
# File 'lib/modulr/js_module.rb', line 99 def dependencies @dependencies ||= self.class.find_dependencies(self) end |
#dependency_array ⇒ Object
103 104 105 |
# File 'lib/modulr/js_module.rb', line 103 def dependency_array '[' << dependencies.map { |d| "'#{d.id}'" }.join(', ') << ']' end |
#ensure(buffer = '') ⇒ Object
107 108 109 110 |
# File 'lib/modulr/js_module.rb', line 107 def ensure(buffer = '') fn = "function() {\n#{src}\n}" buffer << "\nrequire.ensure(#{dependency_array}, #{fn});\n" end |
#escaped_src ⇒ Object
89 90 91 92 93 |
# File 'lib/modulr/js_module.rb', line 89 def escaped_src @escaped_src ||= src.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } end |
#factory ⇒ Object
95 96 97 |
# File 'lib/modulr/js_module.rb', line 95 def factory "function(require, exports, module) {\n#{src}\n}" end |
#id ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/modulr/js_module.rb', line 58 def id return @id if @id if top_level? @id = identifier else @id = File.(partial_path, directory) @id.sub!("#{File.expand_path(root)}/", '') end end |
#identifier_valid? ⇒ Boolean
54 55 56 |
# File 'lib/modulr/js_module.rb', line 54 def identifier_valid? @valid ||= terms.all? { |t| t =~ /^([a-zA-Z]+|\.\.?)$/ } end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/modulr/js_module.rb', line 50 def inspect "#<#{self.class.name} \"#{identifier}\">" end |
#path ⇒ Object
76 77 78 |
# File 'lib/modulr/js_module.rb', line 76 def path @path ||= File.(partial_path, directory) + '.js' end |
#relative? ⇒ Boolean
68 69 70 |
# File 'lib/modulr/js_module.rb', line 68 def relative? @relative ||= terms.first =~ /^\.\.?$/ end |
#src ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/modulr/js_module.rb', line 80 def src return @src if @src if File.exist?(path) @src = File.read(path) else raise LoadModuleError.new(self) end end |
#top_level? ⇒ Boolean
72 73 74 |
# File 'lib/modulr/js_module.rb', line 72 def top_level? !relative? end |