Class: Stitch::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/stitch/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ Source

Returns a new instance of Source.



69
70
71
72
# File 'lib/stitch/source.rb', line 69

def initialize(root, path)
  @root = Pathname.new(root).expand_path
  @path = Pathname.new(path).expand_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



67
68
69
# File 'lib/stitch/source.rb', line 67

def path
  @path
end

#rootObject (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.expand_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

#compileObject



84
85
86
# File 'lib/stitch/source.rb', line 84

def compile
  compiler.compile(path)
end

#eql?(source) ⇒ Boolean

Returns:

  • (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

#extObject



80
81
82
# File 'lib/stitch/source.rb', line 80

def ext
  path.extname
end

#hashObject



100
101
102
# File 'lib/stitch/source.rb', line 100

def hash
  self.path.hash
end

#nameObject



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

#requiresObject

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/stitch/source.rb', line 88

def valid?
  !!compiler
end