Class: RubyLens::ReportWriter
- Inherits:
-
Object
- Object
- RubyLens::ReportWriter
- Defined in:
- lib/rubylens/report_writer.rb
Direct Known Subclasses
Constant Summary collapse
- MODEL_PLACEHOLDER =
"{{MODEL_BASE64}}"
Instance Method Summary collapse
-
#initialize(template_path: nil, asset_assembler: nil) ⇒ ReportWriter
constructor
A new instance of ReportWriter.
- #rubylens_report?(path) ⇒ Boolean
- #write(model, output:) ⇒ Object
Constructor Details
#initialize(template_path: nil, asset_assembler: nil) ⇒ ReportWriter
Returns a new instance of ReportWriter.
13 14 15 16 17 18 |
# File 'lib/rubylens/report_writer.rb', line 13 def initialize(template_path: nil, asset_assembler: nil) raise ArgumentError, "provide template_path or asset_assembler, not both" if template_path && asset_assembler @template_path = template_path @asset_assembler = asset_assembler || ReportAssetAssembler.new unless template_path end |
Instance Method Details
#rubylens_report?(path) ⇒ Boolean
36 37 38 39 40 |
# File 'lib/rubylens/report_writer.rb', line 36 def rubylens_report?(path) File.file?(path) && File.open(path, "rb") { |file| file.read(2048).include?('<meta name="generator" content="RubyLens">') } rescue Errno::ENOENT, Errno::EACCES false end |
#write(model, output:) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rubylens/report_writer.rb', line 20 def write(model, output:) output = File.(output) directory = File.dirname(output) FileUtils.mkdir_p(directory, mode: 0o700) protect_default_directory(directory) template = @template_path ? File.read(@template_path) : @asset_assembler.assemble unless template.scan(MODEL_PLACEHOLDER).length == 1 raise Error, "report template must contain exactly one #{MODEL_PLACEHOLDER} placeholder" end payload = Base64.strict_encode64(JSON.generate(model)) html = template.sub(MODEL_PLACEHOLDER, payload) atomic_write(output, html) output end |