Class: MoCo::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/moco/compiler.rb

Direct Known Subclasses

CssCompiler, HtmlCompiler, JsCompiler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file, compiled_file = nil, compiled_dir = nil) ⇒ Compiler

Returns a new instance of Compiler.



34
35
36
37
38
39
# File 'lib/moco/compiler.rb', line 34

def initialize(source_file, compiled_file = nil, compiled_dir = nil)
  @source_file = source_file
  @compiled_file = compiled_file || compiled_filename(compiled_dir)
  validate_filenames
  require_libraries
end

Instance Attribute Details

#compiled_fileObject (readonly)

Returns the value of attribute compiled_file.



32
33
34
# File 'lib/moco/compiler.rb', line 32

def compiled_file
  @compiled_file
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



31
32
33
# File 'lib/moco/compiler.rb', line 31

def source_file
  @source_file
end

Class Method Details

.compiled_extensionObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/moco/compiler.rb', line 27

def self.compiled_extension
  raise NotImplementedError
end

.convert_option(key, value) ⇒ Object



23
24
25
# File 'lib/moco/compiler.rb', line 23

def self.convert_option(key, value)
  [key.to_sym, CompilerOption.convert(value)]
end

.optionsObject



19
20
21
# File 'lib/moco/compiler.rb', line 19

def self.options
  (@options || {}).dup
end

.register(source_extension) ⇒ Object



5
6
7
# File 'lib/moco/compiler.rb', line 5

def self.register(source_extension)
  MoCo.register(self, source_extension)
end

.require_library(lib, gem_name = lib, version = nil) ⇒ Object



9
10
11
12
# File 'lib/moco/compiler.rb', line 9

def self.require_library(lib, gem_name = lib, version = nil)
  @libraries ||= []
  @libraries << [lib, gem_name, version]
end

.set_option(key, value = true) ⇒ Object



14
15
16
17
# File 'lib/moco/compiler.rb', line 14

def self.set_option(key, value = true)
  @options ||= {}
  @options[key] = value
end

Instance Method Details

#compileObject



45
46
47
48
49
50
51
52
# File 'lib/moco/compiler.rb', line 45

def compile
  write_compiled(compiled_text)
rescue SyntaxError, StandardError => e
  error = CompileError.new(e, @source_file)
  error.set_backtrace(e.backtrace)
  write_compiled(error_text(error))
  raise error
end

#compiled_textObject

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/moco/compiler.rb', line 62

def compiled_text
  raise NotImplementedError
end

#optionsObject



54
55
56
# File 'lib/moco/compiler.rb', line 54

def options
  self.class.options
end

#should_compile?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/moco/compiler.rb', line 41

def should_compile?
  ! FileUtil.up_to_date?(@compiled_file, @source_file)
end

#source_textObject



58
59
60
# File 'lib/moco/compiler.rb', line 58

def source_text
  File.read(@source_file)
end