class Jbuilder

Constants

BLANK
DependencyTracker
NON_ENUMERABLES

Public Class Methods

encode(*args, &block) click to toggle source

Yields a builder and automatically turns the result into a JSON string

# File lib/jbuilder.rb, line 22
def self.encode(*args, &block)
  new(*args, &block).target!
end
ignore_nil(value = true) click to toggle source

Same as instance method ::ignore_nil! except sets the default.

# File lib/jbuilder.rb, line 129
def self.ignore_nil(value = true)
  @@ignore_nil = value
end
key_format(*args) click to toggle source

Same as the instance method ::key_format! except sets the default.

# File lib/jbuilder.rb, line 105
def self.key_format(*args)
  @@key_formatter = KeyFormatter.new(*args)
end
new(options = {}) { |self| ... } click to toggle source
# File lib/jbuilder.rb, line 12
def initialize(options = {})
  @attributes = {}

  @key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil}
  @ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)

  yield self if ::Kernel.block_given?
end

Public Instance Methods

array!(collection = [], *attributes) click to toggle source

Turns the current element into an array and iterates over the passed collection, adding each iteration as an element of the resulting array.

Example:

json.array!(@people) do |person|
  json.name person.name
  json.age calculate_age(person.birthday)
end

[ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]

If you are using Ruby 1.9+, you can use the call syntax instead of an explicit extract! call:

json.(@people) { |person| ... }

It's generally only needed to use this method for top-level arrays. If you have named arrays, you can do:

json.people(@people) do |person|
  json.name person.name
  json.age calculate_age(person.birthday)
end

{ "people": [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ] }

If you omit the block then you can set the top level array directly:

json.array! [1, 2, 3]

[1,2,3]
# File lib/jbuilder.rb, line 184
def array!(collection = [], *attributes)
  array = if collection.nil?
    []
  elsif ::Kernel.block_given?
    _map_collection(collection, &::Proc.new)
  elsif attributes.any?
    _map_collection(collection) { |element| extract! element, *attributes }
  else
    collection.to_a
  end

  merge! array
end
attributes!() click to toggle source

Returns the attributes of the current builder.

# File lib/jbuilder.rb, line 239
def attributes!
  @attributes
end
call(object, *attributes) click to toggle source
# File lib/jbuilder.rb, line 223
def call(object, *attributes)
  if ::Kernel.block_given?
    array! object, &::Proc.new
  else
    extract! object, *attributes
  end
end
child!() { |self| ... } click to toggle source

Turns the current element into an array and yields a builder to add a hash.

Example:

json.comments do
  json.child! { json.content "hello" }
  json.child! { json.content "world" }
end

{ "comments": [ { "content": "hello" }, { "content": "world" } ]}

More commonly, you'd use the combined iterator, though:

json.comments(@post.comments) do |comment|
  json.content comment.formatted_content
end
# File lib/jbuilder.rb, line 149
def child!
  @attributes = [] unless ::Array === @attributes
  @attributes << _scope{ yield self }
end
extract!(object, *attributes) click to toggle source

Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.

Example:

@person = Struct.new(:name, :age).new('David', 32)

or you can utilize a Hash

@person = { name: 'David', age: 32 }

json.extract! @person, :name, :age

{ "name": David", "age": 32 }, { "name": Jamie", "age": 31 }

You can also use the call syntax instead of an explicit extract! call:

json.(@person, :name, :age)
# File lib/jbuilder.rb, line 215
def extract!(object, *attributes)
  if ::Hash === object
    _extract_hash_values(object, attributes)
  else
    _extract_method_values(object, attributes)
  end
end
ignore_nil!(value = true) click to toggle source

If you want to skip adding nil values to your JSON hash. This is useful for JSON clients that don't deal well with nil values, and would prefer not to receive keys which have null values.

Example:

json.ignore_nil! false
json.id User.new.id

{ "id": null }

json.ignore_nil!
json.id User.new.id

{}
# File lib/jbuilder.rb, line 124
def ignore_nil!(value = true)
  @ignore_nil = value
end
key_format!(*args) click to toggle source

Specifies formatting to be applied to the key. Passing in a name of a function will cause that function to be called on the key. So :upcase will upper case the key. You can also pass in lambdas for more complex transformations.

Example:

json.key_format! :upcase
json.author do
  json.name "David"
  json.age 32
end

{ "AUTHOR": { "NAME": "David", "AGE": 32 } }

You can pass parameters to the method using a hash pair.

json.key_format! camelize: :lower
json.first_name "David"

{ "firstName": "David" }

Lambdas can also be used.

json.key_format! ->(key){ "_" + key }
json.first_name "David"

{ "_first_name": "David" }
# File lib/jbuilder.rb, line 100
def key_format!(*args)
  @key_formatter = KeyFormatter.new(*args)
end
merge!(hash_or_array) click to toggle source

Merges hash or array into current builder.

# File lib/jbuilder.rb, line 244
def merge!(hash_or_array)
  @attributes = _merge_values(@attributes, hash_or_array)
end
method_missing(*args) click to toggle source
# File lib/jbuilder.rb, line 64
def method_missing(*args)
  if ::Kernel.block_given?
    set!(*args, &::Proc.new)
  else
    set!(*args)
  end
end
nil!() click to toggle source

Returns the nil JSON.

# File lib/jbuilder.rb, line 232
def nil!
  @attributes = nil
end
Also aliased as: null!
null!()
Alias for: nil!
set!(key, value = BLANK, *args) { |self| ... } click to toggle source
# File lib/jbuilder.rb, line 29
def set!(key, value = BLANK, *args)
  result = if ::Kernel.block_given?
    if !_blank?(value)
      # json.comments @post.comments { |comment| ... }
      # { "comments": [ { ... }, { ... } ] }
      _scope{ array! value, &::Proc.new }
    else
      # json.comments { ... }
      # { "comments": ... }
      _merge_block(key){ yield self }
    end
  elsif args.empty?
    if ::Jbuilder === value
      # json.age 32
      # json.person another_jbuilder
      # { "age": 32, "person": { ...  }
      value.attributes!
    else
      # json.age 32
      # { "age": 32 }
      value
    end
  elsif _is_collection?(value)
    # json.comments @post.comments, :content, :created_at
    # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
    _scope{ array! value, *args }
  else
    # json.author @post.creator, :name, :email_address
    # { "author": { "name": "David", "email_address": "david@loudthinking.com" } }
    _merge_block(key){ extract! value, *args }
  end

  _set_value key, result
end
target!() click to toggle source

Encodes the current builder as JSON.

# File lib/jbuilder.rb, line 249
def target!
  ::MultiJson.dump(@attributes)
end

Private Instance Methods

_blank?(value=@attributes) click to toggle source
# File lib/jbuilder.rb, line 315
def _blank?(value=@attributes)
  BLANK == value
end
_extract_hash_values(object, attributes) click to toggle source
# File lib/jbuilder.rb, line 255
def _extract_hash_values(object, attributes)
  attributes.each{ |key| _set_value key, object.fetch(key) }
end
_extract_method_values(object, attributes) click to toggle source
# File lib/jbuilder.rb, line 259
def _extract_method_values(object, attributes)
  attributes.each{ |key| _set_value key, object.public_send(key) }
end
_is_collection?(object) click to toggle source
# File lib/jbuilder.rb, line 311
def _is_collection?(object)
  _object_respond_to?(object, :map, :count) && NON_ENUMERABLES.none?{ |klass| klass === object }
end
_key(key) click to toggle source
# File lib/jbuilder.rb, line 284
def _key(key)
  @key_formatter ? @key_formatter.format(key) : key.to_s
end
_map_collection(collection) { |element| ... } click to toggle source
# File lib/jbuilder.rb, line 296
def _map_collection(collection)
  collection.map do |element|
    _scope{ yield element }
  end - [BLANK]
end
_merge_block(key) { |self| ... } click to toggle source
# File lib/jbuilder.rb, line 263
def _merge_block(key)
  current_value = _blank? ? BLANK : @attributes.fetch(_key(key), BLANK)
  raise NullError.build(key) if current_value.nil?
  new_value = _scope{ yield self }
  _merge_values(current_value, new_value)
end
_merge_values(current_value, updates) click to toggle source
# File lib/jbuilder.rb, line 270
def _merge_values(current_value, updates)
  if _blank?(updates)
    current_value
  elsif _blank?(current_value) || updates.nil?
    updates
  elsif ::Array === updates
    ::Array === current_value ? current_value + updates : updates
  elsif ::Hash === current_value
    current_value.merge(updates)
  else
    raise "Can't merge #{updates.inspect} with #{current_value.inspect}"
  end
end
_object_respond_to?(object, *methods) click to toggle source
# File lib/jbuilder.rb, line 319
def _object_respond_to?(object, *methods)
  methods.all?{ |m| object.respond_to?(m) }
end
_scope() { || ... } click to toggle source
# File lib/jbuilder.rb, line 302
def _scope
  parent_attributes, parent_formatter = @attributes, @key_formatter
  @attributes = BLANK
  yield
  @attributes
ensure
  @attributes, @key_formatter = parent_attributes, parent_formatter
end
_set_value(key, value) click to toggle source
# File lib/jbuilder.rb, line 288
def _set_value(key, value)
  raise NullError.build(key) if @attributes.nil?
  raise ArrayError.build(key) if ::Array === @attributes
  return if @ignore_nil && value.nil? or _blank?(value)
  @attributes = {} if _blank?
  @attributes[_key(key)] = value
end