lib/jsonify/builder.rb in jsonify-0.4.0 vs lib/jsonify/builder.rb in jsonify-0.4.1
- old
+ new
@@ -53,12 +53,22 @@
# @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=nil, &block)
method_missing(sym, *args, &block)
end
-
- # Compiles the JSON objects into a string representation.
+
+ # Adds a new JsonPair for each attribute in attrs by taking attr as key and value of that attribute in object.
+ #
+ # @param object [Object] Object to take values from
+ # @param *attrs [Array<Symbol>] Array of attributes for JsonPair keys
+ def attributes!(object, *attrs)
+ attrs.each do |attr|
+ method_missing attr, object.send(attr)
+ end
+ 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 +MultiJson.load+.
# If initialized with +:format => :pretty+, the compiled result will be parsed and encoded via +MultiJson.dump(<json>, :pretty => true)+
# 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
@@ -94,10 +104,51 @@
args.each do |arg|
@stack[@level].add arg
end
self
end
+
+ # Creates array of json objects in current element from array passed to this method.
+ # Accepts block which yields each array element.
+ #
+ # @example Create array in root JSON element
+ # json.array!(@links) do |link|
+ # json.rel link.first
+ # json.href link.last
+ # end
+ #
+ # @example compiles to something like ...
+ # [
+ # {
+ # "rel": "self",
+ # "href": "http://example.com/people/123"
+ # },
+ # {
+ # "rel": "school",
+ # "href": "http://gatech.edu"
+ # }
+ # ]
+ #
+ def array!(args)
+ __array
+ args.each do |arg|
+ @level += 1
+ yield arg
+ @level -= 1
+
+ value = @stack.pop
+
+ # If the object created was an array with a single value
+ # assume that just the value should be added
+ if (JsonArray === value && value.values.length <= 1)
+ value = value.values.first
+ end
+
+ @stack[@level].add value
+ end
+ end
+
# Adds a new JsonPair to the builder where the key of the pair is set to the method name
# (`sym`).
# When passed a block, the value of the pair is set to the result of that
# block; otherwise, the value is set to the argument(s) (`args`).
@@ -151,25 +202,10 @@
@level += 1
if args.nil?
block.call
else
- __array
- args.each do |arg|
- @level += 1
- block.call(arg)
- @level -= 1
-
- value = @stack.pop
-
- # If the object created was an array with a single value
- # assume that just the value should be added
- if (JsonArray === value && value.values.length <= 1)
- value = value.values.first
- end
-
- @stack[@level].add value
- end
+ array!(args, &block)
end
# Set the value on the pair to the object at the top of the stack
pair.value = @stack[@level]