Class: Jade::Source

Inherits:
Data
  • Object
show all
Defined in:
lib/jade/source.rb

Overview

root is the directory uri is relative to — the app's source root for its own modules, an extension gem's for the modules it ships. It is what makes "app or gem?" structural instead of a name list. nil for sources that never came off disk: buffers, stdin, the stdlib.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, text:, line_starts: calculate_line_starts(text), root: nil) ⇒ Source

Returns a new instance of Source.



33
34
35
# File 'lib/jade/source.rb', line 33

def initialize(uri:, text:, line_starts: calculate_line_starts(text), root: nil)
  super
end

Instance Attribute Details

#line_startsObject (readonly)

Returns the value of attribute line_starts

Returns:

  • (Object)

    the current value of line_starts



6
7
8
# File 'lib/jade/source.rb', line 6

def line_starts
  @line_starts
end

#rootObject (readonly)

Returns the value of attribute root

Returns:

  • (Object)

    the current value of root



6
7
8
# File 'lib/jade/source.rb', line 6

def root
  @root
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



6
7
8
# File 'lib/jade/source.rb', line 6

def text
  @text
end

#uriObject (readonly)

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



6
7
8
# File 'lib/jade/source.rb', line 6

def uri
  @uri
end

Class Method Details

.camelize(str) ⇒ Object



55
56
57
# File 'lib/jade/source.rb', line 55

def self.camelize(str)
  str.split('_').map { |part| part.capitalize }.join
end

.load(source_root, uri, overlays: {}) ⇒ Object



7
8
9
10
# File 'lib/jade/source.rb', line 7

def self.load(source_root, uri, overlays: {})
  text = overlays[uri] || File.read(File.join(source_root, uri))
  new(uri:, text:, root: source_root)
end

.load_from_module_name(source_root, name, overlays: {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/jade/source.rb', line 12

def self.load_from_module_name(source_root, name, overlays: {})
  name.split('.')
    .compact
    .map { snake_case(it) }
    .then { |(*rest, last)| rest + [last + '.jd'] }
    .then { File.join(*it) }
    .then { load(resolve_root(source_root, it, name), it, overlays:) }
end

.resolve_root(source_root, uri, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jade/source.rb', line 21

def self.resolve_root(source_root, uri, name)
  return source_root if File.exist?(File.join(source_root, uri))

  candidates = Jade.extensions.select { |r| File.exist?(File.join(r, uri)) }

  case candidates.size
  when 0 then source_root
  when 1 then candidates.first
  else raise "Module #{name} is provided by multiple extensions: #{candidates.join(', ')}"
  end
end

.snake_case(str) ⇒ Object



59
60
61
62
63
64
# File 'lib/jade/source.rb', line 59

def self.snake_case(str)
  str
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')  # ABCXyz -> ABC_Xyz
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')      # abcXyz -> abc_Xyz
    .downcase
end

Instance Method Details

#to_module_nameObject



37
38
39
40
41
42
43
# File 'lib/jade/source.rb', line 37

def to_module_name
  uri
    .delete_suffix('.jd')
    .split('/')
    .map { Source.camelize(it) }
    .join('.')
end