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



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

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



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

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

#required_treesObject (readonly)

Returns the value of attribute required_trees



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

def required_trees
  @required_trees
end

#requiresObject (readonly)

Returns the value of attribute requires



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

def requires
  @requires
end

#sourceObject (readonly)

Returns the value of attribute source



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

def source
  @source
end

Class Method Details

.extensionsObject



30
31
32
# File 'opal/lib/opal/builder_processors.rb', line 30

def self.extensions
  @extensions
end

.handles(*extensions) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'opal/lib/opal/builder_processors.rb', line 20

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

  ::Opal::Builder.register_processor(self, extensions)
  nil
end

.match?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.match_regexpObject



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

def self.match_regexp
  @match_regexp or raise NotImplementedError
end

Instance Method Details

#mark_as_required(filename) ⇒ Object



61
62
63
# File 'opal/lib/opal/builder_processors.rb', line 61

def mark_as_required(filename)
  "Opal.loaded([#{filename.to_s.inspect}]);"
end

#source_mapObject



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

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



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

def to_s
  source.to_s
end