Class: Jade::Source
- Inherits:
-
Data
- Object
- Data
- Jade::Source
- 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
-
#line_starts ⇒ Object
readonly
Returns the value of attribute line_starts.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
- .camelize(str) ⇒ Object
- .load(source_root, uri, overlays: {}) ⇒ Object
- .load_from_module_name(source_root, name, overlays: {}) ⇒ Object
- .resolve_root(source_root, uri, name) ⇒ Object
- .snake_case(str) ⇒ Object
Instance Method Summary collapse
-
#initialize(uri:, text:, line_starts: calculate_line_starts(text), root: nil) ⇒ Source
constructor
A new instance of Source.
- #to_module_name ⇒ Object
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_starts ⇒ Object (readonly)
Returns the value of attribute line_starts
6 7 8 |
# File 'lib/jade/source.rb', line 6 def line_starts @line_starts end |
#root ⇒ Object (readonly)
Returns the value of attribute root
6 7 8 |
# File 'lib/jade/source.rb', line 6 def root @root end |
#text ⇒ Object (readonly)
Returns the value of attribute text
6 7 8 |
# File 'lib/jade/source.rb', line 6 def text @text end |
#uri ⇒ Object (readonly)
Returns the value of attribute 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 = [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 |