Class: Ufo::DSL::Outputter
- Inherits:
-
Object
- Object
- Ufo::DSL::Outputter
- Defined in:
- lib/ufo/dsl/outputter.rb
Instance Method Summary collapse
-
#initialize(name, erb_result, options = {}) ⇒ Outputter
constructor
A new instance of Outputter.
- #output_json(json) ⇒ Object
- #validate(json, path) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(name, erb_result, options = {}) ⇒ Outputter
Returns a new instance of Outputter.
4 5 6 7 8 9 |
# File 'lib/ufo/dsl/outputter.rb', line 4 def initialize(name, erb_result, ={}) @name = name @erb_result = erb_result @options = @pretty = [:pretty].nil? ? true : [:pretty] end |
Instance Method Details
#output_json(json) ⇒ Object
35 36 37 |
# File 'lib/ufo/dsl/outputter.rb', line 35 def output_json(json) @options[:pretty] ? JSON.pretty_generate(JSON.parse(json)) : json end |
#validate(json, path) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/ufo/dsl/outputter.rb', line 25 def validate(json, path) begin JSON.parse(json) rescue JSON::ParserError => e puts "Invalid json. Output written to #{path} for debugging".color(:red) File.open(path, 'w') {|f| f.write(json) } exit 1 end end |
#write ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ufo/dsl/outputter.rb', line 11 def write output_path = "#{Ufo.root}/.ufo/output" FileUtils.rm_rf(output_path) if @options[:clean] FileUtils.mkdir(output_path) unless File.exist?(output_path) path = "#{output_path}/#{@name}.json".sub(/^\.\//,'') puts " #{path}" unless @options[:quiet] validate(@erb_result, path) json = @pretty ? JSON.pretty_generate(JSON.parse(@erb_result)) : @erb_result File.open(path, 'w') {|f| f.write(output_json(json)) } end |