class Rabl::Builder

Public Instance Methods

build(object, options={}) click to toggle source

Given an object and options, returns the hash representation build(@user, :format => “json”, :attributes => { … }, :root_name => “user”)

# File lib/rabl/builder.rb, line 17
def build(object, options={})
  @_object = object

  cache_results do
    compile_hash(options)
  end
end

Protected Instance Methods

attribute(name, options={}) click to toggle source

Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar”

# File lib/rabl/builder.rb, line 66
def attribute(name, options={})
  @_result[options[:as] || name] = data_object_attribute(name) if @_object && @_object.respond_to?(name)
end
Also aliased as: attributes
attributes(name, options={}) click to toggle source
Alias for: attribute
child(data, options={}, &block) click to toggle source

Creates a child node that is included in json output child(@user) { attribute :full_name } child(@user => :person) { … } child(@users => :people) { … }

# File lib/rabl/builder.rb, line 90
def child(data, options={}, &block)
  return false unless data.present? && resolve_condition(options)
  name, object = data_name(data), data_object(data)
  include_root = is_collection?(object) && @options[:child_root] # child @users
  engine_options = @options.slice(:child_root).merge(:root => include_root)
  object = { object => name } if data.respond_to?(:each_pair) && object # child :users => :people
  @_result[name] = self.object_to_hash(object, engine_options, &block)
end
code(name, options={}, &block) click to toggle source
Alias for: node
compile_hash(options={}) click to toggle source

Returns a hash representation of the data object #compile_hash(:root_name => false) #compile_hash(:root_name => “user”)

# File lib/rabl/builder.rb, line 30
def compile_hash(options={})
  @_result = {}
  # Extends
  @options[:extends].each do |settings|
    extends(settings[:file], settings[:options], &settings[:block])
  end if @options.has_key?(:extends)
  # Attributes
  @options[:attributes].each_pair do |attribute, name|
    attribute(attribute, :as => name)
  end if @options.has_key?(:attributes)
  # Node
  @options[:node].each do |settings|
    node(settings[:name], settings[:options], &settings[:block])
  end if @options.has_key?(:node)
  # Children
  @options[:child].each do |settings|
    child(settings[:data], settings[:options], &settings[:block])
  end if @options.has_key?(:child)
  # Glues
  @options[:glue].each do |settings|
    glue(settings[:data], &settings[:block])
  end if @options.has_key?(:glue)

  # Wrap result in root
  if options[:root_name].present?
    @_root_name = options[:root_name]
  else # no root
    @_root_name = nil
  end

  # Return Results
  @_root_name ? { @_root_name => @_result } : @_result
end
extends(file, options={}, &block) click to toggle source

Extends an existing rabl template with additional attributes in the block extends(“users/show”) { attribute :full_name }

# File lib/rabl/builder.rb, line 110
def extends(file, options={}, &block)
  options = @options.slice(:child_root).merge(:object => @_object).merge(options)
  result = self.partial(file, options, &block)
  @_result.merge!(result) if result.is_a?(Hash)
end
glue(data, &block) click to toggle source

Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }

# File lib/rabl/builder.rb, line 101
def glue(data, &block)
  return false unless data.present?
  object = data_object(data)
  glued_attributes = self.object_to_hash(object, :root => false, &block)
  @_result.merge!(glued_attributes) if glued_attributes
end
node(name, options={}, &block) click to toggle source

Creates an arbitrary node that is included in the json output node(:foo) { “bar” } node(:foo) { “bar” } node(:foo, :if => lambda { |m| m.foo.present? }) { “bar” }

# File lib/rabl/builder.rb, line 75
def node(name, options={}, &block)
  return unless resolve_condition(options)
  result = block.call(@_object)
  if name.present?
    @_result[name] = result
  elsif result.respond_to?(:each_pair) # merge hash into root hash
    @_result.merge!(result)
  end
end
Also aliased as: code
resolve_condition(options) click to toggle source

#resolve_condition(:if => true) => true #resolve_condition(:if => lambda { |m| false }) => false #resolve_condition(:unless => lambda { |m| true }) => true

# File lib/rabl/builder.rb, line 119
def resolve_condition(options)
  return true if options[:if].nil? && options[:unless].nil?
  result = options[:if] == true || (options[:if].respond_to?(:call) && options[:if].call(@_object)) if options.has_key?(:if)
  result = options[:unless] == false || (options[:unless].respond_to?(:call) && !options[:unless].call(@_object)) if options.has_key?(:unless)
  result
end

Public Class Methods

new(options={}, &block) click to toggle source

Constructs a new rabl hash based on given object and options options = { :format => “json”, :root => true, :child_root => true,

:attributes, :node, :child, :glue, :extends }
# File lib/rabl/builder.rb, line 9
def initialize(options={}, &block)
  @options    = options
  @_scope     = options[:scope]
  @_view_path = options[:view_path]
end