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
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
Indicates an attribute or method should be included in the json output attribute :foo, :as => "bar" attribute :foo, :as => "bar", :if => lambda { |m| m.foo }
# File lib/rabl/builder.rb, line 75 def attribute(name, options={}) if @_object && attribute_present?(name) && resolve_condition(options) @_result[options[:as] || name] = data_object_attribute(name) end end
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 100 def child(data, options={}, &block) return false unless data.present? && resolve_condition(options) name = is_name_value?(options[:root]) ? options[:root] : data_name(data) object = data_object(data) include_root = is_collection?(object) && options.fetch(:object_root, @options[:child_root]) # child @users engine_options = @options.slice(:child_root).merge(:root => include_root) engine_options.merge!(:object_root_name => options[:object_root]) if is_name_value?(options[:object_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
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, settings| attribute(attribute, settings) 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[:options], &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 # Replace nil values with empty strings if configured if Rabl.configuration.replace_nil_values_with_empty_strings @_result = @_result.inject({}) do |hash, (k, v)| hash[k] = v.nil? ? '' : v hash end end # Return Results @_root_name ? { @_root_name => @_result } : @_result end
Extends an existing rabl template with additional attributes in the block extends("users/show") { attribute :full_name }
# File lib/rabl/builder.rb, line 122 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
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }
# File lib/rabl/builder.rb, line 113 def glue(data, options={}, &block) return false unless data.present? && resolve_condition(options) object = data_object(data) glued_attributes = self.object_to_hash(object, :root => false, &block) @_result.merge!(glued_attributes) if glued_attributes end
Creates an arbitrary node that is included in the json output node(:foo) { "bar" } node(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
# File lib/rabl/builder.rb, line 85 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
#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 131 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