Class: Rabl::Engine
Constant Summary collapse
- FORMATS =
List of supported rendering formats
[:json, :xml, :plist, :bson, :msgpack]
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.
-
#cache(key = nil, options = nil) ⇒ Object
Sets the cache key to be used by ActiveSupport::Cache.expand_cache_key cache @user # calls @user.cache_key cache [‘rabl’, @user] # calls @user.cache_key and prefixes with rabl/ cache ‘user’ # explicit key of ‘user’ cache # uses the current item within a collection cache ‘user’, expires_in: 1.hour options is passed through to the cache store.
-
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name }.
-
#collection(data, options = {}) ⇒ Object
Sets the object as a collection casted to a simple array collection @users collection @users => :people collection @users, :root => :person collection @users, :object_root => :person.
-
#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” }).
-
#node(name = nil, options = {}, &block) ⇒ Object
(also: #code)
Creates an arbitrary node that is included in the json output.
-
#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 }).
- #source=(string) ⇒ Object
-
#to_bson(options = {}) ⇒ Object
Returns a bson representation of the data object to_bson(:root => true).
-
#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_msgpack(options = {}) ⇒ Object
(also: #to_mpac)
Returns a msgpack representation of the data object to_msgpack(:root => true).
-
#to_plist(options = {}) ⇒ Object
Returns a plist representation of the data object to_plist(:root => true).
-
#to_xml(options = {}) ⇒ Object
Returns an xml representation of the data object to_xml(:root => true).
Methods included from Partials
#fetch_source, #object_to_hash, #partial
Methods included from Helpers
#collection_root_name, #context_scope, #data_name, #data_object, #determine_object_root, #fetch_result_from_cache, #is_collection?, #is_object?, #object_root_name, #template_cache_configured?
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” })
10 11 12 13 |
# File 'lib/rabl/engine.rb', line 10 def initialize(source, ={}) @_source = source @_options = 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
227 228 229 |
# File 'lib/rabl/engine.rb', line 227 def method_missing(name, *args, &block) context_scope.respond_to?(name) ? context_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
139 140 141 142 143 144 145 146 |
# File 'lib/rabl/engine.rb', line 139 def attribute(*args) if args.first.is_a?(Hash) # :foo => :bar, :bar => :baz args.first.each_pair { |k,v| self.attribute(k, :as => v) } else # array of attributes i.e :foo, :bar, :baz = args. args.each { |name| @_options[:attributes][name] = [:as] || name } end end |
#cache(key = nil, options = nil) ⇒ Object
Sets the cache key to be used by ActiveSupport::Cache.expand_cache_key cache @user # calls @user.cache_key cache [‘rabl’, @user] # calls @user.cache_key and prefixes with rabl/ cache ‘user’ # explicit key of ‘user’ cache # uses the current item within a collection cache ‘user’, expires_in: 1.hour options is passed through to the cache store
131 132 133 134 |
# File 'lib/rabl/engine.rb', line 131 def cache(key = nil, = nil) key ||= @_data # if called but missing, use object @_cache = [key, ] end |
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name }
159 160 161 |
# File 'lib/rabl/engine.rb', line 159 def child(data, ={}, &block) @_options[:child].push({ :data => data, :options => , :block => block }) end |
#collection(data, options = {}) ⇒ Object
Sets the object as a collection casted to a simple array collection @users collection @users => :people collection @users, :root => :person collection @users, :object_root => :person
117 118 119 120 121 122 |
# File 'lib/rabl/engine.rb', line 117 def collection(data, ={}) @_collection_name = [:root] if [:root] @_collection_name ||= data.values.first if data.respond_to?(:each_pair) @_object_root_name = [:object_root] if .has_key?(:object_root) self.object(data_object(data).to_a) 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 }
171 172 173 174 |
# File 'lib/rabl/engine.rb', line 171 def extends(file, ={}, &block) extend_ops = .merge(:view_path => .fetch(:view_path, @_options[:view_path])) @_options[:extends].push({ :file => file, :options => extend_ops, :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 }
165 166 167 |
# File 'lib/rabl/engine.rb', line 165 def glue(data, &block) @_options[:glue].push({ :data => data, :block => block }) end |
#helper(*klazzes) ⇒ Object Also known as: helpers
Includes a helper module with a RABL template helper ExampleHelper
178 179 180 |
# File 'lib/rabl/engine.rb', line 178 def helper(*klazzes) klazzes.each { |klazz| self.class.send(:include, klazz) } end |
#node(name = nil, options = {}, &block) ⇒ Object Also known as: code
Creates an arbitrary node that is included in the json output. node(:foo) { “bar” } node(:foo, :if => lambda { … }) { “bar” }
152 153 154 |
# File 'lib/rabl/engine.rb', line 152 def node(name = nil, ={}, &block) @_options[:node].push({ :name => name, :options => , :block => block }) end |
#object(data) ⇒ Object
Sets the object to be used as the data source for this template object(@user) object @user => :person object @users
108 109 110 |
# File 'lib/rabl/engine.rb', line 108 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 })
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rabl/engine.rb', line 21 def render(scope, locals, &block) @_locals, @_scope = locals, scope self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers]) @_options[:scope] = @_scope @_options[:format] ||= self.request_format @_data = locals[:object] || self.default_object if @_options[:source_location] instance_eval(@_source, @_options[:source_location]) if @_source.present? else # without source location instance_eval(@_source) if @_source.present? end instance_eval(&block) if block_given? cache_results { self.send("to_" + @_options[:format].to_s) } end |
#source=(string) ⇒ Object
15 16 17 |
# File 'lib/rabl/engine.rb', line 15 def source=(string) @_source = string end |
#to_bson(options = {}) ⇒ Object
Returns a bson representation of the data object to_bson(:root => true)
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rabl/engine.rb', line 91 def to_bson(={}) include_root = Rabl.configuration.include_bson_root = .reverse_merge(:root => include_root, :child_root => include_root) result = if collection_root_name { collection_root_name => to_hash() } elsif is_collection?(@_data) && @_data.is_a?(Array) { data_name(@_data) => to_hash() } else to_hash() end Rabl.configuration.bson_engine.serialize(result).to_s end |
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true, :child_root => true)
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rabl/engine.rb', line 39 def to_hash(={}) = @_options.merge() data = data_object(@_data) builder = Rabl::Builder.new() [:root_name] = determine_object_root(@_data, [:root]) if is_object?(data) || !data # object @user builder.build(data, ) elsif is_collection?(data) # collection @users data.map { |object| builder.build(object, ) } end end |
#to_json(options = {}) ⇒ Object
Returns a json representation of the data object to_json(:root => true)
54 55 56 57 58 59 |
# File 'lib/rabl/engine.rb', line 54 def to_json(={}) include_root = Rabl.configuration.include_json_root = .reverse_merge(:root => include_root, :child_root => include_root) result = collection_root_name ? { collection_root_name => to_hash() } : to_hash() format_json(result) end |
#to_msgpack(options = {}) ⇒ Object Also known as: to_mpac
Returns a msgpack representation of the data object to_msgpack(:root => true)
63 64 65 66 67 68 |
# File 'lib/rabl/engine.rb', line 63 def to_msgpack(={}) include_root = Rabl.configuration.include_msgpack_root = .reverse_merge(:root => include_root, :child_root => include_root) result = collection_root_name ? { collection_root_name => to_hash() } : to_hash() Rabl.configuration.msgpack_engine.pack result end |
#to_plist(options = {}) ⇒ Object
Returns a plist representation of the data object to_plist(:root => true)
73 74 75 76 77 78 |
# File 'lib/rabl/engine.rb', line 73 def to_plist(={}) include_root = Rabl.configuration.include_plist_root = .reverse_merge(:root => include_root, :child_root => include_root) result = defined?(@_collection_name) ? { @_collection_name => to_hash() } : to_hash() Rabl.configuration.plist_engine.dump(result) end |
#to_xml(options = {}) ⇒ Object
Returns an xml representation of the data object to_xml(:root => true)
82 83 84 85 86 87 |
# File 'lib/rabl/engine.rb', line 82 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 |