Class: Jade::Source

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Source.



29
30
31
# File 'lib/jade/source.rb', line 29

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

Instance Attribute Details

#line_startsObject (readonly)

Returns the value of attribute line_starts

Returns:

  • (Object)

    the current value of line_starts



2
3
4
# File 'lib/jade/source.rb', line 2

def line_starts
  @line_starts
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



2
3
4
# File 'lib/jade/source.rb', line 2

def text
  @text
end

#uriObject (readonly)

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



2
3
4
# File 'lib/jade/source.rb', line 2

def uri
  @uri
end

Class Method Details

.camelize(str) ⇒ Object



51
52
53
# File 'lib/jade/source.rb', line 51

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

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



3
4
5
6
# File 'lib/jade/source.rb', line 3

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

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



8
9
10
11
12
13
14
15
# File 'lib/jade/source.rb', line 8

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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jade/source.rb', line 17

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



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

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



33
34
35
36
37
38
39
# File 'lib/jade/source.rb', line 33

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