lib/jason.rb in jason-0.4.0 vs lib/jason.rb in jason-0.5.0

- old
+ new

@@ -4,20 +4,33 @@ # Renders and compiles Jason templates. module Jason class << self attr_writer :output_format + attr_writer :output_listeners # The output format of the JSON. # # I suggest using `:pretty` for development and testing and `:compact` for # production. # # @returns [:pretty, :compact] def output_format @output_format ||= :compact end + + # The output listeners for jason. + # + # All objects in this array are sent #call with the generated JSON whenever + # jason processes a buffer. This can be useful for logging output like so: + # + # Jason.output_listeners << lambda { |json| Rails.logger.info(json) } + # + # @returns [<#call>] + def output_listeners + @output_listeners ||= [] + end end # Render a template. # # @example @@ -46,22 +59,27 @@ "#{eruby_template(template).src}; Jason.process(_buf)" end # Process a rendered buffer. # - # Removes any trailing commas and compresses the buffer. + # Removes any trailing commas and compresses the buffer. After generating the + # JSON, it calls each one of the output listeners with the generated JSON. # - # Usually, you should not have to call this method. + # You should not have to directly call this method. # # @param [String] buffer def self.process(buffer) obj = JSON.load(remove_trailing_commas(buffer)) if output_format == :pretty - JSON.pretty_generate(obj) + json = JSON.pretty_generate(obj) else - JSON.generate(obj) + json = JSON.generate(obj) end + + output_listeners.each { |listener| listener.call(json) } + + json end private def self.eruby_template(template) \ No newline at end of file