Module: Motion::Require
- Defined in:
- lib/motion-require.rb,
lib/motion-require/ext.rb,
lib/motion-require/version.rb
Defined Under Namespace
Modules: Ext Classes: RequireBuilder
Constant Summary collapse
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.require_relative_enabled ⇒ Object
Returns the value of attribute require_relative_enabled.
Class Method Summary collapse
-
.all(files = nil) ⇒ Object
Scan specified files.
- .dependencies_for(files) ⇒ Object
-
.explicit_relative(path) ⇒ Object
RubyMotion prefers relative paths to be explicitly prefixed with ./.
- .ext_file ⇒ Object
- .requires_in(file) ⇒ Object
-
.resolve_path(source, required) ⇒ Object
Join ‘required` to directory containing `source`.
Class Attribute Details
.require_relative_enabled ⇒ Object
Returns the value of attribute require_relative_enabled.
9 10 11 |
# File 'lib/motion-require.rb', line 9 def require_relative_enabled @require_relative_enabled end |
Class Method Details
.all(files = nil) ⇒ Object
Scan specified files. When nil, fallback to RubyMotion’s default (app/*/.rb).
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/motion-require.rb', line 90 def all(files=nil) Motion::Project::App.setup do |app| if files.nil? || files.empty? app.files.push ext_file app.files_dependencies dependencies_for(app.files.flatten) else # Place files prior to those in ./app, otherwise at the end. preceding_app = app.files.index { |f| f =~ %r(^(?:\./)?app/) } || -1 required = Array(files).map { |f| explicit_relative(f) } app.files.insert(preceding_app, ext_file, *required) app.files.uniq! # Prevent redundancy app.files_dependencies dependencies_for(required) end end end |
.dependencies_for(files) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/motion-require.rb', line 53 def dependencies_for(files) dependencies = {} files.each do |file_path| requires = requires_in(file_path) if !requires.empty? dependencies[file_path] = requires.map { |required| required_path = resolve_path(file_path, required) if !File.exist?(required_path) && File.extname(required) != ".rb" required_path += ".rb" end if !File.exist?(required_path) # TODO: Report line number of failing require raise LoadError, "ERROR! In `#{file_path}', could not require `#{required}', file not found." end required_path } dependencies[file_path].unshift ext_file end end dependencies end |
.explicit_relative(path) ⇒ Object
RubyMotion prefers relative paths to be explicitly prefixed with ./
108 109 110 111 |
# File 'lib/motion-require.rb', line 108 def explicit_relative(path) # Paths that do not start with "/", "./", or "../" will be prefixed with ./ path.sub(%r(^(?!\.{0,2}/)), './') end |
.ext_file ⇒ Object
113 114 115 |
# File 'lib/motion-require.rb', line 113 def ext_file File.(File.join(File.dirname(__FILE__), "../motion/ext.rb")) end |
.requires_in(file) ⇒ Object
77 78 79 80 81 |
# File 'lib/motion-require.rb', line 77 def requires_in(file) parser = Motion::Require::RequireBuilder.new(File.read(file)) parser.parse parser.requires end |
.resolve_path(source, required) ⇒ Object
Join ‘required` to directory containing `source`. Preserves relative/absolute nature of source
85 86 87 |
# File 'lib/motion-require.rb', line 85 def resolve_path(source, required) Pathname.new(source).dirname.join(required.to_str).cleanpath.to_path end |