Class: Stitch::Source
- Inherits:
-
Object
- Object
- Stitch::Source
- Defined in:
- lib/stitch/source.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
-
.from_file(root, path = nil, result = []) ⇒ Object
Recursively resolve sources from a given file, dynamically resolving its dependencies Usage: sources = Source.from_file(“./app/index.js”).
-
.from_path(root, path = nil, result = []) ⇒ Object
Recursively load all the sources from a given directory Usage: sources = Source.from_path(“./app”).
-
.resolve(path, relative_to) ⇒ Object
Resolve a require call to an absolute path Usage: path = Source.resolve(“../index.js”, “/my/file.js”).
Instance Method Summary collapse
- #compile ⇒ Object
- #eql?(source) ⇒ Boolean
- #ext ⇒ Object
- #hash ⇒ Object
-
#initialize(root, path) ⇒ Source
constructor
A new instance of Source.
- #name ⇒ Object
-
#requires ⇒ Object
Return an array of resolved paths specifying this source’s dependencies.
- #valid? ⇒ Boolean
Constructor Details
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
67 68 69 |
# File 'lib/stitch/source.rb', line 67 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
67 68 69 |
# File 'lib/stitch/source.rb', line 67 def root @root end |
Class Method Details
.from_file(root, path = nil, result = []) ⇒ Object
Recursively resolve sources from a given file, dynamically resolving its dependencies Usage:
sources = Source.from_file("./app/index.js")
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/stitch/source.rb', line 29 def self.from_file(root, path = nil, result = []) root = Pathname.new(root) unless path path = root root = root.dirname end source = self.new(root, path) source.requires.each do |child| from_file(root, child, result) end result << source end |
.from_path(root, path = nil, result = []) ⇒ Object
Recursively load all the sources from a given directory Usage:
sources = Source.from_path("./app")
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stitch/source.rb', line 9 def self.from_path(root, path = nil, result = []) path ||= root path = Pathname.new(path) if path.directory? path.children.each do |child| from_path(root, child, result) end else source = self.new(root, path) result << source if source.valid? end result end |
.resolve(path, relative_to) ⇒ Object
Resolve a require call to an absolute path Usage:
path = Source.resolve("../index.js", "/my/file.js")
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stitch/source.rb', line 49 def self.resolve(path, relative_to) path = Pathname.new(path) relative_to = Pathname.new(relative_to) unless path.absolute? path = path.(relative_to) end return path if path.exist? Compiler.all_extensions.each do |ext| candidate = Pathname.new(path.to_s + "." + ext) return candidate if candidate.exist? end raise "#{path} not found" end |
Instance Method Details
#compile ⇒ Object
84 85 86 |
# File 'lib/stitch/source.rb', line 84 def compile compiler.compile(path) end |
#eql?(source) ⇒ Boolean
104 105 106 107 |
# File 'lib/stitch/source.rb', line 104 def eql?(source) source.is_a?(Source) && source.path.to_s == self.path.to_s end |
#ext ⇒ Object
80 81 82 |
# File 'lib/stitch/source.rb', line 80 def ext path.extname end |
#hash ⇒ Object
100 101 102 |
# File 'lib/stitch/source.rb', line 100 def hash self.path.hash end |
#name ⇒ Object
74 75 76 77 78 |
# File 'lib/stitch/source.rb', line 74 def name name = path.relative_path_from(root) name = name.dirname + name.basename(".*") name.to_s end |
#requires ⇒ Object
Return an array of resolved paths specifying this source’s dependencies
94 95 96 97 98 |
# File 'lib/stitch/source.rb', line 94 def requires return [] unless source? requires = path.read.scan(/require\(("|')(.+)\1\)/) requires.map {|(_, pn)| self.class.resolve(pn, root) } end |
#valid? ⇒ Boolean
88 89 90 |
# File 'lib/stitch/source.rb', line 88 def valid? !!compiler end |