Class: Jsus::SourceFile

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ SourceFile

Basic constructor.

Initializes a file from source.

Parameters:

  • source (String)

    original source for the file

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :namespace (String)

    source file namespace



42
43
44
45
46
47
48
49
# File 'lib/jsus/source_file.rb', line 42

def initialize(source, options = {})
  @namespace       = options[:namespace]
  @original_source = source.dup
  prepare_original_source
  @source          = @original_source.dup
  parse_header
  @original_source.freeze
end

Instance Attribute Details

#filenameObject 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

#namespaceObject (readonly)

Default namespace for source



30
31
32
# File 'lib/jsus/source_file.rb', line 30

def namespace
  @namespace
end

#original_filenameObject

Original filename (immutable)



16
17
18
# File 'lib/jsus/source_file.rb', line 16

def original_filename
  @original_filename
end

#original_sourceObject (readonly)

Original source code (immutable)



24
25
26
# File 'lib/jsus/source_file.rb', line 24

def original_source
  @original_source
end

#packageObject

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

#sourceObject

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

Parameters:

  • filename (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :namespace (String)

    namespace to which the source file by default belongs

Returns:

Raises:



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, options = {})
  filename = File.expand_path(filename)
  raise BadSourceFileException, "File does not exist." unless File.exists?(filename)
  source = Jsus::Util.read_file(filename)
  source_file = new(source, options)
  source_file.filename = source_file.original_filename = filename
  source_file.original_filename.freeze
  source_file
rescue Exception => e
  e.message.sub! /^/, "Unexpected exception happened while processing #{filename}: "
  Jsus.logger.error e.message
  raise e
end

Instance Method Details

#==(other) ⇒ Object



173
174
175
# File 'lib/jsus/source_file.rb', line 173

def ==(other)
  eql?(other)
end

#authorsArray

Returns list of authors.

Returns:

  • (Array)

    list of authors



93
94
95
# File 'lib/jsus/source_file.rb', line 93

def authors
  @authors
end

#descriptionString

Returns description of the source file.

Returns:

  • (String)

    description of the source file.



82
83
84
# File 'lib/jsus/source_file.rb', line 82

def description
  header["description"]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/jsus/source_file.rb', line 178

def eql?(other)
  other.kind_of?(SourceFile) && filename == other.filename
end

#extendsObject

Bar.js in package Bar extends 'Core/Class'. That means its contents would be appended to the Foo.js when compiling the result.

Examples:

file Foo.js in package Core provides ['Class', 'Hash']. File



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.

Returns:

  • (Boolean)

    whether the source file is an extension.



130
131
132
# File 'lib/jsus/source_file.rb', line 130

def extension?
  extends && !extends.empty?
end

#hashObject



183
184
185
# File 'lib/jsus/source_file.rb', line 183

def hash
  [self.class, filename].hash
end

#headerHash

Returns a header parsed from YAML-formatted source file first comment.

Returns:

  • (Hash)

    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

#inspectString

Human readable description of source file.

Returns:

  • (String)


168
169
170
# File 'lib/jsus/source_file.rb', line 168

def inspect
  self.to_hash.merge("namespace" => namespace).inspect
end

#licenseString

Returns license of source file.

Returns:

  • (String)

    license of source file



87
88
89
# File 'lib/jsus/source_file.rb', line 87

def license
  header["license"]
end

#providesArray Also known as: provisions

Returns array with provides tags.

Returns:

  • (Array)

    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.

Returns:

  • (Boolean)

    whether the source file is an extension.



136
137
138
# File 'lib/jsus/source_file.rb', line 136

def replacement?
  replaces && !replaces.empty?
end

#replacesJsus::Tag

Returns tag for replaced file, if any.

Returns:

  • (Jsus::Tag)

    tag for replaced file, if any



115
116
117
# File 'lib/jsus/source_file.rb', line 115

def replaces
  @replaces
end

#required_filesArray

Returns array of files required by this files including all the filenames for extensions. SourceFile filename always goes first, all the extensions are unordered.

Returns:

  • (Array)

    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

#requiresArray Also known as: dependencies, requirements

Returns list of dependencies for given file.

Returns:

  • (Array)

    list of dependencies for given file



99
100
101
# File 'lib/jsus/source_file.rb', line 99

def requires
  @requires
end

#resetObject

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_hashHash

Returns hash containing basic info with dependencies/provides tags' names and description for source file.

Returns:

  • (Hash)

    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