Class: Opal::BuilderProcessors::Processor

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/builder_processors.rb

Direct Known Subclasses

ERBProcessor, JsProcessor, RubyProcessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename, options = {}) ⇒ Processor

Returns a new instance of Processor



14
15
16
17
18
# File 'opal/lib/opal/builder_processors.rb', line 14

def initialize(source, filename, options = {})
  @source, @filename, @options = source, filename, options
  @requires = []
  @required_trees = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename



19
20
21
# File 'opal/lib/opal/builder_processors.rb', line 19

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options



19
20
21
# File 'opal/lib/opal/builder_processors.rb', line 19

def options
  @options
end

#required_treesObject (readonly)

Returns the value of attribute required_trees



19
20
21
# File 'opal/lib/opal/builder_processors.rb', line 19

def required_trees
  @required_trees
end

#requiresObject (readonly)

Returns the value of attribute requires



19
20
21
# File 'opal/lib/opal/builder_processors.rb', line 19

def requires
  @requires
end

#sourceObject (readonly)

Returns the value of attribute source



19
20
21
# File 'opal/lib/opal/builder_processors.rb', line 19

def source
  @source
end

Class Method Details

.extensionsObject



33
34
35
# File 'opal/lib/opal/builder_processors.rb', line 33

def self.extensions
  @extensions
end

.handles(*extensions) ⇒ Object



25
26
27
28
29
30
31
# File 'opal/lib/opal/builder_processors.rb', line 25

def self.handles(*extensions)
  @extensions = extensions
  matches = extensions.join('|')
  matches = "(#{matches})" if extensions.size == 1

  @match_regexp = Regexp.new "\\.#{matches}#{REGEXP_END}"
end

.inherited(processor) ⇒ Object



10
11
12
# File 'opal/lib/opal/builder_processors.rb', line 10

def self.inherited(processor)
  DEFAULT_PROCESSORS << processor
end

.match?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'opal/lib/opal/builder_processors.rb', line 37

def self.match? other
  (other.is_a?(String) and other.match(match_regexp))
end

.match_regexpObject



41
42
43
# File 'opal/lib/opal/builder_processors.rb', line 41

def self.match_regexp
  @match_regexp or raise NotImplementedError
end

Instance Method Details

#mark_as_required(filename) ⇒ Object



64
65
66
# File 'opal/lib/opal/builder_processors.rb', line 64

def mark_as_required(filename)
  "Opal.mark_as_loaded(Opal.normalize_loadable_path(#{filename.to_s.inspect}));"
end

#source_mapObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'opal/lib/opal/builder_processors.rb', line 45

def source_map
  @source_map ||= begin
    mappings = []
    source_file = filename+'.js'
    line = source.count("\n")
    column = source.scan("\n[^\n]*$").size
    offset = ::SourceMap::Offset.new(line, column)
    mappings << ::SourceMap::Mapping.new(source_file, offset, offset)

    # Ensure mappings isn't empty: https://github.com/maccman/sourcemap/issues/11
    unless mappings.any?
      zero_offset = ::SourceMap::Offset.new(0,0)
      mappings = [::SourceMap::Mapping.new(source_file,zero_offset,zero_offset)]
    end

    ::SourceMap::Map.new(mappings)
  end
end

#to_sObject



21
22
23
# File 'opal/lib/opal/builder_processors.rb', line 21

def to_s
  source.to_s
end