Class: Javascripto::File
Constant Summary collapse
- JS =
Define Constants
'.js'- @@files =
File Repository
{}
- @@path_lookup_cache =
{}
Instance Attribute Summary collapse
-
#package ⇒ Object
Returns the value of attribute package.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.get_file(file_path) ⇒ Object
Return the file object instance.
- .js_root=(root) ⇒ Object
Instance Method Summary collapse
-
#cache ⇒ Object
Override Package Methods.
- #file_dependencies ⇒ Object
-
#initialize(path) ⇒ File
constructor
A new instance of File.
- #package_dependencies ⇒ Object
- #package_files ⇒ Object
- #package_name ⇒ Object
- #resource_path ⇒ Object
Methods inherited from Package
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/javascripto/file.rb', line 24 def initialize(path) @path = path # Handle remote packages. first_line = ::File.open(@@js_root.join(@path + JS), &:gets) if first_line @remote_url = /\s*^\/\/\s*use_remote_package(.*)/.match(first_line) if @remote_url @remote_url = @remote_url.captures[0].strip @package = self end end end |
Instance Attribute Details
#package ⇒ Object
Returns the value of attribute package.
18 19 20 |
# File 'lib/javascripto/file.rb', line 18 def package @package end |
#path ⇒ Object
Returns the value of attribute path.
18 19 20 |
# File 'lib/javascripto/file.rb', line 18 def path @path end |
Class Method Details
.get_file(file_path) ⇒ Object
Return the file object instance.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/javascripto/file.rb', line 68 def self.get_file(file_path) # Expand the file path. file_path = (file_path) # Look for the file object. if @@files[file_path] return @@files[file_path] end # Otherwise create a new one. @@files[file_path] = File.new(file_path) end |
.js_root=(root) ⇒ Object
13 14 15 16 |
# File 'lib/javascripto/file.rb', line 13 def self.js_root=(root) @@js_root = root @@load_path = ['lib', *Dir.glob(::File.join(@@js_root, "vendor/*")).map{|entry| "vendor/#{::File.basename(entry)}" } ] end |
Instance Method Details
#cache ⇒ Object
Override Package Methods
84 85 86 87 |
# File 'lib/javascripto/file.rb', line 84 def cache # Disable rails caching for single file packages nil end |
#file_dependencies ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/javascripto/file.rb', line 38 def file_dependencies if @file_dependencies return @file_dependencies end # Only scan up to 15 lines. lines = [] ::File.open(@@js_root.join(@path + JS), "r") do |file| 15.times do if line = file.gets lines << line else break end end end # Indentify required files required_files = lines.map do |line| match = /\s*^\/\/\s*require(.*)/.match(line) if match # Remove whitespace and break on comma for multiple files match.captures[0].strip.split(/\s*,\s*/) end end.flatten.compact.uniq @file_dependencies = required_files.map{ |required_file| File.get_file(required_file) } end |
#package_dependencies ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/javascripto/file.rb', line 97 def package_dependencies unless @package_dependencies @package_dependencies = [] # For each file dependency file_dependencies.each do |dependency| # It it's not already in a package... unless dependency.package # ...Make it it's own package. dependency.package = dependency end # Add files_dependency package as a package dependency. @package_dependencies << dependency.package end # Remove duplicates. @package_dependencies.uniq! end @package_dependencies end |
#package_files ⇒ Object
89 90 91 |
# File 'lib/javascripto/file.rb', line 89 def package_files [self] end |
#package_name ⇒ Object
93 94 95 |
# File 'lib/javascripto/file.rb', line 93 def package_name @path end |
#resource_path ⇒ Object
20 21 22 |
# File 'lib/javascripto/file.rb', line 20 def resource_path @remote_url || @path end |