Class: Rabl::Engine
Instance Method Summary collapse
- 
  
    
      #attribute(*args)  ⇒ Object 
    
    
      (also: #attributes)
    
  
  
  
  
  
  
  
  
  
    Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar” attribute :foo => :bar. 
- 
  
    
      #child(data, options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Creates a child node that is included in json output child(@user) { attribute :full_name }. 
- 
  
    
      #code(name, options = {}, &block)  ⇒ Object 
    
    
      (also: #node)
    
  
  
  
  
  
  
  
  
  
    Creates an arbitrary code node that is included in the json output code(:foo) { “bar” } code(:foo, :if => lambda { … }) { “bar” }. 
- 
  
    
      #collection(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the object as a collection casted to a simple array collection @users collection @users => :people. 
- 
  
    
      #extends(file, options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Extends an existing rabl template with additional attributes in the block extends(“users/show”, :object => @user) { attribute :full_name }. 
- 
  
    
      #glue(data, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }. 
- 
  
    
      #helper(*klazzes)  ⇒ Object 
    
    
      (also: #helpers)
    
  
  
  
  
  
  
  
  
  
    Includes a helper module with a RABL template helper ExampleHelper. 
- 
  
    
      #initialize(source, options = {})  ⇒ Engine 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Constructs a new ejs engine based on given vars, handler and declarations Rabl::Engine.new(“…source…”, { :format => “xml”, :root => true, :view_path => “/path/to/views” }). 
- 
  
    
      #object(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the object to be used as the data source for this template object(@user) object @user => :person object @users. 
- 
  
    
      #render(scope, locals, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Renders the representation based on source, object, scope and locals Rabl::Engine.new(“…source…”, { :format => “xml” }).render(scope, { :foo => “bar”, :object => @user }). 
- 
  
    
      #to_hash(options = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns a hash representation of the data object to_hash(:root => true, :child_root => true). 
- 
  
    
      #to_json(options = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns a json representation of the data object to_json(:root => true). 
- 
  
    
      #to_xml(options = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns an xml representation of the data object to_xml(:root => true). 
Methods included from Helpers
#data_name, #data_object, #fetch_source, #is_record?, #object_to_hash, #partial, #resolve_condition
Constructor Details
#initialize(source, options = {}) ⇒ Engine
Constructs a new ejs engine based on given vars, handler and declarations Rabl::Engine.new(“…source…”, { :format => “xml”, :root => true, :view_path => “/path/to/views” })
| 9 10 11 12 13 14 15 16 | # File 'lib/rabl/engine.rb', line 9 def initialize(source, ={}) @_source = source = if Rabl.configuration.json_engine MultiJson.engine = Rabl.configuration.json_engine end end | 
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
Supports calling helpers defined for the template scope using method_missing hook
| 168 169 170 | # File 'lib/rabl/engine.rb', line 168 def method_missing(name, *args, &block) @_scope.respond_to?(name) ? @_scope.send(name, *args, &block) : super end | 
Instance Method Details
#attribute(*args) ⇒ Object Also known as: attributes
Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar” attribute :foo => :bar
| 81 82 83 84 85 86 87 88 89 | # File 'lib/rabl/engine.rb', line 81 def attribute(*args) if args.first.is_a?(Hash) args.first.each_pair { |k,v| self.attribute(k, :as => v) } else # array of attributes = args. [:attributes] ||= {} args.each { |name| [:attributes][name] = [:as] || name } end end | 
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name }
| 103 104 105 106 | # File 'lib/rabl/engine.rb', line 103 def child(data, ={}, &block) [:child] ||= [] [:child].push({ :data => data, :options => , :block => block }) end | 
#code(name, options = {}, &block) ⇒ Object Also known as: node
Creates an arbitrary code node that is included in the json output code(:foo) { “bar” } code(:foo, :if => lambda { … }) { “bar” }
| 95 96 97 98 | # File 'lib/rabl/engine.rb', line 95 def code(name, ={}, &block) [:code] ||= {} [:code][name] = { :options => , :block => block } end | 
#collection(data) ⇒ Object
Sets the object as a collection casted to a simple array collection @users collection @users => :people
| 73 74 75 76 | # File 'lib/rabl/engine.rb', line 73 def collection(data) @_collection_name = data.values.first if data.respond_to?(:each_pair) self.object(data_object(data).to_a) if data end | 
#extends(file, options = {}, &block) ⇒ Object
Extends an existing rabl template with additional attributes in the block extends(“users/show”, :object => @user) { attribute :full_name }
| 117 118 119 120 | # File 'lib/rabl/engine.rb', line 117 def extends(file, ={}, &block) [:extends] ||= [] [:extends].push({ :file => file, :options => , :block => block }) end | 
#glue(data, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }
| 110 111 112 113 | # File 'lib/rabl/engine.rb', line 110 def glue(data, &block) [:glue] ||= [] [:glue].push({ :data => data, :block => block }) end | 
#helper(*klazzes) ⇒ Object Also known as: helpers
Includes a helper module with a RABL template helper ExampleHelper
| 124 125 126 | # File 'lib/rabl/engine.rb', line 124 def helper(*klazzes) klazzes.each { |klazz| self.class.send(:include, klazz) } end | 
#object(data) ⇒ Object
Sets the object to be used as the data source for this template object(@user) object @user => :person object @users
| 66 67 68 | # File 'lib/rabl/engine.rb', line 66 def object(data) @_data = data unless @_locals[:object] end | 
#render(scope, locals, &block) ⇒ Object
Renders the representation based on source, object, scope and locals Rabl::Engine.new(“…source…”, { :format => “xml” }).render(scope, { :foo => “bar”, :object => @user })
| 20 21 22 23 24 25 26 27 28 29 | # File 'lib/rabl/engine.rb', line 20 def render(scope, locals, &block) @_locals, @_scope = locals, scope self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers]) [:scope] = @_scope [:format] ||= self.request_format @_data = locals[:object] || self.default_object instance_eval(@_source) if @_source.present? instance_eval(&block) if block_given? self.send("to_" + [:format].to_s) end | 
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true, :child_root => true)
| 33 34 35 36 37 38 39 40 41 42 | # File 'lib/rabl/engine.rb', line 33 def to_hash(={}) = .reverse_merge() data = data_object(@_data) if is_record?(data) || !data # object @user Rabl::Builder.new(@_data, ).to_hash() elsif data.respond_to?(:each) # collection @users object_name = data_name(@_data).to_s.singularize # @users => :users data.map { |object| Rabl::Builder.new({ object => object_name }, ).to_hash() } end end | 
#to_json(options = {}) ⇒ Object
Returns a json representation of the data object to_json(:root => true)
| 46 47 48 49 50 51 | # File 'lib/rabl/engine.rb', line 46 def to_json(={}) include_root = Rabl.configuration.include_json_root = .reverse_merge(:root => include_root, :child_root => include_root) result = @_collection_name ? { @_collection_name => to_hash() } : to_hash() format_json MultiJson.encode(result) end | 
#to_xml(options = {}) ⇒ Object
Returns an xml representation of the data object to_xml(:root => true)
| 55 56 57 58 59 60 | # File 'lib/rabl/engine.rb', line 55 def to_xml(={}) include_root = Rabl.configuration.include_xml_root = .reverse_merge(:root => include_root, :child_root => include_root) = Rabl.configuration..merge(:root => data_name(@_data)) to_hash().to_xml() end |