Class: Jsus::SourceFile
- Inherits:
-
Object
- Object
- Jsus::SourceFile
- Defined in:
- lib/jsus/source_file.rb
Overview
SourceFile is a base for any Jsus operation.
It contains general info about source as well as file content.
Instance Attribute Summary collapse
-
#filename ⇒ Object
(also: #path)
Full filename (when initialized from file).
-
#namespace ⇒ Object
readonly
Default namespace for source.
-
#original_filename ⇒ Object
Original filename (immutable).
-
#original_source ⇒ Object
readonly
Original source code (immutable).
-
#package ⇒ Object
Package owning the sourcefile Is not directly used in SourceFile, but might be useful for introspection.
-
#source ⇒ Object
Source code (mutable).
Class Method Summary collapse
-
.from_file(filename, options = {}) ⇒ Jsus::SourceFile
Initializes a SourceFile given the filename and options.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#authors ⇒ Array
List of authors.
-
#description ⇒ String
Description of the source file.
- #eql?(other) ⇒ Boolean
-
#extends ⇒ Object
Bar.js in package Bar extends 'Core/Class'.
-
#extension? ⇒ Boolean
Whether the source file is an extension.
- #hash ⇒ Object
-
#header ⇒ Hash
A header parsed from YAML-formatted source file first comment.
-
#initialize(source, options = {}) ⇒ SourceFile
constructor
Basic constructor.
-
#inspect ⇒ String
Human readable description of source file.
-
#license ⇒ String
License of source file.
-
#provides ⇒ Array
(also: #provisions)
Array with provides tags.
-
#replacement? ⇒ Boolean
Whether the source file is an extension.
-
#replaces ⇒ Jsus::Tag
Tag for replaced file, if any.
-
#required_files ⇒ Array
Array of files required by this files including all the filenames for extensions.
-
#requires ⇒ Array
(also: #dependencies, #requirements)
List of dependencies for given file.
- #reset ⇒ Object private
-
#to_hash ⇒ Hash
Hash containing basic info with dependencies/provides tags' names and description for source file.
Constructor Details
#initialize(source, options = {}) ⇒ SourceFile
Basic constructor.
Initializes a file from source.
42 43 44 45 46 47 48 49 |
# File 'lib/jsus/source_file.rb', line 42 def initialize(source, = {}) @namespace = [:namespace] @original_source = source.dup prepare_original_source @source = @original_source.dup parse_header @original_source.freeze end |
Instance Attribute Details
#filename ⇒ Object Also known as: path
Full filename (when initialized from file)
19 20 21 |
# File 'lib/jsus/source_file.rb', line 19 def filename @filename end |
#namespace ⇒ Object (readonly)
Default namespace for source
30 31 32 |
# File 'lib/jsus/source_file.rb', line 30 def namespace @namespace end |
#original_filename ⇒ Object
Original filename (immutable)
16 17 18 |
# File 'lib/jsus/source_file.rb', line 16 def original_filename @original_filename end |
#original_source ⇒ Object (readonly)
Original source code (immutable)
24 25 26 |
# File 'lib/jsus/source_file.rb', line 24 def original_source @original_source end |
#package ⇒ Object
Package owning the sourcefile Is not directly used in SourceFile, but might be useful for introspection.
13 14 15 |
# File 'lib/jsus/source_file.rb', line 13 def package @package end |
#source ⇒ Object
Source code (mutable)
27 28 29 |
# File 'lib/jsus/source_file.rb', line 27 def source @source end |
Class Method Details
.from_file(filename, options = {}) ⇒ Jsus::SourceFile
Initializes a SourceFile given the filename and options
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jsus/source_file.rb', line 60 def self.from_file(filename, = {}) filename = File.(filename) raise BadSourceFileException, "File does not exist." unless File.exists?(filename) source = Jsus::Util.read_file(filename) source_file = new(source, ) source_file.filename = source_file.original_filename = filename source_file.original_filename.freeze source_file rescue Exception => e e..sub! /^/, "Unexpected exception happened while processing #{filename}: " Jsus.logger.error e. raise e end |
Instance Method Details
#==(other) ⇒ Object
173 174 175 |
# File 'lib/jsus/source_file.rb', line 173 def ==(other) eql?(other) end |
#authors ⇒ Array
Returns list of authors.
93 94 95 |
# File 'lib/jsus/source_file.rb', line 93 def @authors end |
#description ⇒ String
Returns description of the source file.
82 83 84 |
# File 'lib/jsus/source_file.rb', line 82 def description header["description"] end |
#eql?(other) ⇒ Boolean
178 179 180 |
# File 'lib/jsus/source_file.rb', line 178 def eql?(other) other.kind_of?(SourceFile) && filename == other.filename end |
#extends ⇒ Object
Bar.js in package Bar extends 'Core/Class'. That means its contents would be appended to the Foo.js when compiling the result.
124 125 126 |
# File 'lib/jsus/source_file.rb', line 124 def extends @extends end |
#extension? ⇒ Boolean
Returns whether the source file is an extension.
130 131 132 |
# File 'lib/jsus/source_file.rb', line 130 def extension? extends && !extends.empty? end |
#hash ⇒ Object
183 184 185 |
# File 'lib/jsus/source_file.rb', line 183 def hash [self.class, filename].hash end |
#header ⇒ Hash
Returns a header parsed from YAML-formatted source file first comment.
76 77 78 |
# File 'lib/jsus/source_file.rb', line 76 def header @header ||= {} end |
#inspect ⇒ String
Human readable description of source file.
168 169 170 |
# File 'lib/jsus/source_file.rb', line 168 def inspect self.to_hash.merge("namespace" => namespace).inspect end |
#license ⇒ String
Returns license of source file.
87 88 89 |
# File 'lib/jsus/source_file.rb', line 87 def license header["license"] end |
#provides ⇒ Array Also known as: provisions
Returns array with provides tags.
107 108 109 |
# File 'lib/jsus/source_file.rb', line 107 def provides @provides end |
#replacement? ⇒ Boolean
Returns whether the source file is an extension.
136 137 138 |
# File 'lib/jsus/source_file.rb', line 136 def replacement? replaces && !replaces.empty? end |
#replaces ⇒ Jsus::Tag
Returns tag for replaced file, if any.
115 116 117 |
# File 'lib/jsus/source_file.rb', line 115 def replaces @replaces end |
#required_files ⇒ Array
Returns array of files required by this files including all the filenames for extensions. SourceFile filename always goes first, all the extensions are unordered.
149 150 151 |
# File 'lib/jsus/source_file.rb', line 149 def required_files [filename].flatten end |
#requires ⇒ Array Also known as: dependencies, requirements
Returns list of dependencies for given file.
99 100 101 |
# File 'lib/jsus/source_file.rb', line 99 def requires @requires end |
#reset ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
141 142 143 144 |
# File 'lib/jsus/source_file.rb', line 141 def reset @source = @original_source.dup @filename = @original_filename.dup if @original_filename end |
#to_hash ⇒ Hash
Returns hash containing basic info with dependencies/provides tags' names and description for source file.
157 158 159 160 161 162 163 |
# File 'lib/jsus/source_file.rb', line 157 def to_hash { "desc" => description, "requires" => requires.map {|tag| tag.namespace == namespace ? tag.name : tag.full_name}, "provides" => provides.map {|tag| tag.name} } end |