Class: Jade::Source
- Inherits:
-
Data
- Object
- Data
- Jade::Source
- Defined in:
- lib/jade/source.rb
Instance Attribute Summary collapse
-
#line_starts ⇒ Object
readonly
Returns the value of attribute line_starts.
-
#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)) ⇒ Source
constructor
A new instance of Source.
- #to_module_name ⇒ Object
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_starts ⇒ Object (readonly)
Returns the value of attribute line_starts
2 3 4 |
# File 'lib/jade/source.rb', line 2 def line_starts @line_starts end |
#text ⇒ Object (readonly)
Returns the value of attribute text
2 3 4 |
# File 'lib/jade/source.rb', line 2 def text @text end |
#uri ⇒ Object (readonly)
Returns the value of attribute 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 = [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 |