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

- old
+ new

@@ -1,20 +1,29 @@ module Jsonify class Builder < BlankSlate class << self + # Compiles the given block into a JSON string without having to instantiate a Builder. + # + # @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 [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 compile( options={} ) builder = self.new options yield builder builder.compile! end + # Compiles the given block into a pretty JSON string without having to instantiate a Builder. def pretty(&block) compile( :format => :pretty, &block ) end + # Compiles the given block into a plain (e.g. no newlines and whitespace) JSON string without having to instantiate a Builder. def plain(&block) compile( :format => :plain, &block ) end end @@ -41,11 +50,11 @@ # Adds a new JsonPair to the builder. Use this method if the pair "key" has spaces or other characters that prohibit creation via method_missing. # # @param sym [String] the key for the pair # @param *args [arguments] If a block is passed, the first argument will be iterated over and the subsequent result will be added to a JSON array; otherwise, the arguments set value for the `JsonPair` # @param &block a code block the result of which will be used to populate the value for the JSON pair - def tag!(sym, *args, &block) + def tag!(sym, args=nil, &block) 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+. @@ -71,18 +80,20 @@ alias_method :[]=, :store! # Append -- pushes the given object on the end of a JsonArray. def <<(val) - __append(val) + __array + @stack[@level].add val self end # Append -- pushes the given variable list objects on to the end of the JsonArray def append!(*args) + __array args.each do |arg| - __append( arg ) + @stack[@level].add arg end self end # Adds a new JsonPair to the builder where the key of the pair is set to the method name @@ -123,27 +134,28 @@ # "href": "http://gatech.edu" # } # ] # # @param *args [Array] iterates over the given array yielding each array item to the block; the result of which is added to a JsonArray - def method_missing(sym, *args, &block) + def method_missing(sym, args=nil, &block) # When no block given, simply add the symbol and arg as key - value for a JsonPair to current - return __store( sym, args.length > 1 ? args : args.first ) unless block + return __store( sym, args ) unless block # In a block; create a JSON pair (with no value) and add it to the current object pair = Generate.pair_value(sym) __store pair # Now process the block @level += 1 - if args.empty? + if args.nil? block.call else - args.first.each do |arg| - __append block.call(arg) + __array + args.each do |arg| + @stack[@level].add block.call(arg) end end # Set the value on the pair to the object at the top of the stack pair.value = @stack[@level] @@ -177,17 +189,17 @@ end private # BlankSlate requires the __<method> names - + def __store(key,value=nil) pair = (JsonPair === key ? key : JsonPair.new(key, value)) (@stack[@level] ||= JsonObject.new).add(pair) end - def __append(value) - (@stack[@level] ||= JsonArray.new).add value + def __array + @stack[@level] ||= JsonArray.new end end end \ No newline at end of file