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” })
7 8 9 10 |
# File 'lib/rabl/engine.rb', line 7 def initialize(source, ={}) @_source = source = 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
162 163 164 |
# File 'lib/rabl/engine.rb', line 162 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
75 76 77 78 79 80 81 82 83 |
# File 'lib/rabl/engine.rb', line 75 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 }
97 98 99 100 |
# File 'lib/rabl/engine.rb', line 97 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” }
89 90 91 92 |
# File 'lib/rabl/engine.rb', line 89 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
67 68 69 70 |
# File 'lib/rabl/engine.rb', line 67 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 }
111 112 113 114 |
# File 'lib/rabl/engine.rb', line 111 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 }
104 105 106 107 |
# File 'lib/rabl/engine.rb', line 104 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
118 119 120 |
# File 'lib/rabl/engine.rb', line 118 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
60 61 62 |
# File 'lib/rabl/engine.rb', line 60 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 })
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rabl/engine.rb', line 14 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)
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rabl/engine.rb', line 27 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)
40 41 42 43 44 45 |
# File 'lib/rabl/engine.rb', line 40 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(result.to_json) end |
#to_xml(options = {}) ⇒ Object
Returns an xml representation of the data object to_xml(:root => true)
49 50 51 52 53 54 |
# File 'lib/rabl/engine.rb', line 49 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 |