lib/jsonify/builder.rb in jsonify-0.1.1 vs lib/jsonify/builder.rb in jsonify-0.1.2

- old
+ new

@@ -1,16 +1,36 @@ module Jsonify class Builder < BlankSlate + + class << self + def compile( options={} ) + builder = self.new options + yield builder + builder.compile! + end + + def pretty(&block) + compile( :format => :pretty, &block ) + end + + def plain(&block) + compile( :format => :plain, &block ) + end + + end + # Initializes a new builder. The Jsonify::Builder works by keeping a stack of +JsonValue+s. # # @param [Hash] options the options to create with # @option options [boolean] :verify Builder will verify that the compiled JSON string is parseable; this option does incur a performance penalty and generally should only be used in development - # @option options [pretty] :pretty Builder will output the JSON string in a prettier format with new lines and indentation; this option does incur a performance penalty and generally should only be used in development + # @option options [symbol] :format Format for the resultant JSON string; + # `:pretty`, the JSON string will be output in a prettier format with new lines and indentation; this option does incur a performance penalty and generally should only be used in development + # `:plain`, no formatting (compact one-line JSON -- best for production) def initialize(options={}) @verify = options[:verify].nil? ? false : options[:verify] - @pretty = options[:pretty].nil? ? false : options[:pretty] + @pretty = options[:format].to_s == 'pretty' ? true : false reset! end # Clears the builder data def reset! @@ -27,10 +47,10 @@ method_missing(sym, *args, &block) end # Compiles the JSON objects into a string representation. # If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +JSON.parse+. - # If initialized with +:pretty => true+, the compiled result will be parsed and regenerated via +JSON.pretty_generate+. + # If initialized with +:format => :pretty+, the compiled result will be parsed and regenerated via +JSON.pretty_generate+. # This method can be called without any side effects. You can call +compile!+ at any time, and multiple times if desired. # # @raise [TypeError] only if +:verify+ is set to true # @raise [JSON::ParseError] only if +:verify+ is set to true def compile! \ No newline at end of file