Class: Rabl::Engine
- Inherits:
-
Object
- Object
- Rabl::Engine
- Includes:
- Helpers, Helpers::Escaper, Partials
- Defined in:
- lib/rabl/engine.rb
Constant Summary collapse
- FORMATS =
List of supported rendering formats
[:json, :xml, :plist, :bson, :msgpack]
Constants included from Helpers
Instance Method Summary collapse
-
#apply(context_scope, locals, &block) ⇒ Object
Renders the representation based on source, object, context_scope and locals Rabl::Engine.new(“…source…”, { :format => “xml” }).apply(context_scope, { :foo => “bar”, :object => @user }).
-
#attribute(*args) ⇒ Object
(also: #attributes)
Indicates an attribute or method should be included in the json output attribute :foo, :as => “bar” attribute :foo => :bar, :bar => :baz attribute :foo => :bar, :bar => :baz, :if => lambda { |r| r.foo }.
-
#cache(key = nil, options = {}) ⇒ 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.
- #cache_key ⇒ Object
-
#cache_read_on_render=(read) ⇒ Object
Disables reading (but not writing) from the cache when rendering.
- #cache_read_on_render? ⇒ Boolean
-
#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, options = {}, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }.
-
#helper(*klasses) ⇒ 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(template_data) ⇒ Object
Sets the object to be used as the data source for this template object(@user) object @user => :person object @users.
-
#partial(file, options = {}, &block) ⇒ Object
Returns a hash representing the partial partial(“users/show”, :object => @user) options must have :object options can have :view_path, :child_root, :root.
-
#render(context_scope = nil, locals = nil, &block) ⇒ Object
Renders the representation based on a previous apply Rabl::Engine.new(“…source…”, { :format => “xml” }).apply(context_scope, { :foo => “bar”, :object => @user }).render.
- #root_name ⇒ Object
-
#root_object ⇒ Object
Returns the current object that is the topic of this template Can be the collection or the object depending on topic assigned root_object => @user.
- #source=(source) ⇒ Object
-
#to_bson(options = {}) ⇒ Object
Returns a bson representation of the data object to_bson(:root => true).
- #to_dumpable(options = {}) ⇒ Object
-
#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 Helpers::Escaper
Methods included from Partials
Methods included from Sources
Methods included from Helpers
#collection_root_name, #context_scope, #data_name, #data_object, #data_object_attribute, #determine_object_root, #fetch_result_from_cache, #is_collection?, #is_name_value?, #is_object?, #object_root_name, #object_to_engine, #template_cache_configured?, #view_path, #write_result_to_cache
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” })
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rabl/engine.rb', line 12 def initialize(source, = {}) @_source = source @_settings = {} @_options = @_view_path = [:view_path] @_context_scope = [:scope] @_cache_read_on_render = true 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 context_scope using method_missing hook
355 356 357 |
# File 'lib/rabl/engine.rb', line 355 def method_missing(name, *args, &block) context_scope.respond_to?(name, true) ? context_scope.__send__(name, *args, &block) : super end |
Instance Method Details
#apply(context_scope, locals, &block) ⇒ Object
Renders the representation based on source, object, context_scope and locals Rabl::Engine.new(“…source…”, { :format => “xml” }).apply(context_scope, { :foo => “bar”, :object => @user })
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rabl/engine.rb', line 29 def apply(context_scope, locals, &block) set_instance_variables!(context_scope, locals) reset_settings! eval_source(locals, &block) instance_exec(root_object, &block) if block_given? self end |
#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, :bar => :baz attribute :foo => :bar, :bar => :baz, :if => lambda { |r| r.foo }
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/rabl/engine.rb', line 226 def attribute(*args) if args.first.is_a?(Hash) # :foo => :bar, :bar => :baz attr_aliases = args.first.except(:if, :unless) conditions = args.first.slice(:if, :unless) attr_aliases.each do |key, as| attribute(key, conditions.merge(:as => as)) end else # array of attributes i.e :foo, :bar, :baz = args. args.each do |name| @_settings[:attributes] << { :name => name, :options => } end end end |
#cache(key = nil, options = {}) ⇒ 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
216 217 218 219 220 |
# File 'lib/rabl/engine.rb', line 216 def cache(key = nil, = {}) key ||= root_object # if called but missing, use object @_cache_key = key @_cache_options = end |
#cache_key ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rabl/engine.rb', line 52 def cache_key return unless defined?(@_cache_key) @_full_cache_key ||= begin cache_key = Array(@_cache_key) + [@_options[:root_name], @_options[:format]] if digestor_available? && respond_to?(:lookup_context) && lookup_context template = @_options[:template] || @virtual_path digest = \ if Gem::Version.new(Rails.version) >= Gem::Version.new('4.1') Digestor.digest(:name => template, :finder => lookup_context) else Digestor.digest(template, :rabl, lookup_context) end cache_key << digest end cache_key end end |
#cache_read_on_render=(read) ⇒ Object
Disables reading (but not writing) from the cache when rendering.
289 290 291 |
# File 'lib/rabl/engine.rb', line 289 def cache_read_on_render=(read) @_cache_read_on_render = read end |
#cache_read_on_render? ⇒ Boolean
293 294 295 |
# File 'lib/rabl/engine.rb', line 293 def cache_read_on_render? @_cache_read_on_render end |
#child(data, options = {}, &block) ⇒ Object
Creates a child node that is included in json output child(@user) { attribute :full_name }
253 254 255 |
# File 'lib/rabl/engine.rb', line 253 def child(data, = {}, &block) @_settings[:child] << { :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
200 201 202 203 204 205 206 207 |
# File 'lib/rabl/engine.rb', line 200 def collection(data, = {}) @_collection_name = [:root] if [:root] @_collection_name ||= data.values.first if data.is_a?(Hash) @_object_root_name = [:object_root] if .has_key?(:object_root) object(data_object(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 }
265 266 267 268 269 |
# File 'lib/rabl/engine.rb', line 265 def extends(file, = {}, &block) = { :view_path => [:view_path] || view_path }.merge() @_settings[:extends] << { :file => file, :options => , :block => block } end |
#glue(data, options = {}, &block) ⇒ Object
Glues data from a child node to the json_output glue(@user) { attribute :full_name => :user_full_name }
259 260 261 |
# File 'lib/rabl/engine.rb', line 259 def glue(data, = {}, &block) @_settings[:glue] << { :data => data, :options => , :block => block } end |
#helper(*klasses) ⇒ Object Also known as: helpers
Includes a helper module with a RABL template helper ExampleHelper
273 274 275 |
# File 'lib/rabl/engine.rb', line 273 def helper(*klasses) klasses.each { |klass| self.class.__send__(:include, klass) } 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” }
246 247 248 |
# File 'lib/rabl/engine.rb', line 246 def node(name = nil, = {}, &block) @_settings[:node] << { :name => name, :options => , :block => block } end |
#object(template_data) ⇒ Object
Sets the object to be used as the data source for this template object(@user) object @user => :person object @users
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rabl/engine.rb', line 163 def object(template_data) current_data = (@_locals[:object].nil? || template_data == false) ? template_data : @_locals[:object] @_data_object = data_object(current_data) @_root_name_data = template_data.is_a?(Hash) && !current_data.is_a?(Hash) ? template_data : current_data @_root_name_data = @_root_name_data.values.first if @_root_name_data.is_a?(Hash) # If we turn this around, `@_root_name_date ==` may trigger data to be loaded unnecessarily. if false == @_root_name_data @_object_root_name = false @_collection_name = false end end |
#partial(file, options = {}, &block) ⇒ Object
Returns a hash representing the partial partial(“users/show”, :object => @user) options must have :object options can have :view_path, :child_root, :root
282 283 284 285 286 |
# File 'lib/rabl/engine.rb', line 282 def partial(file, = {}, &block) engine = partial_as_engine(file, , &block) engine = engine.render if engine.is_a?(Engine) engine end |
#render(context_scope = nil, locals = nil, &block) ⇒ Object
Renders the representation based on a previous apply Rabl::Engine.new(“…source…”, { :format => “xml” }).apply(context_scope, { :foo => “bar”, :object => @user }).render
44 45 46 47 48 49 50 |
# File 'lib/rabl/engine.rb', line 44 def render(context_scope = nil, locals = nil, &block) apply(context_scope, locals, &block) if context_scope || locals || block cache_results do send("to_#{@_options[:format]}") end end |
#root_name ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/rabl/engine.rb', line 186 def root_name return @_data_name if defined?(@_data_name) @_data_name = @_options[:object_root_name] || begin data = defined?(@_root_name_data) ? @_root_name_data : (@_locals[:object].nil? ? root_object : @_locals[:object]) data_name(data) end end |
#root_object ⇒ Object
Returns the current object that is the topic of this template Can be the collection or the object depending on topic assigned root_object => @user
179 180 181 182 183 184 |
# File 'lib/rabl/engine.rb', line 179 def root_object return @_data_object if defined?(@_data_object) data = @_locals[:object].nil? ? default_object : @_locals[:object] @_data_object = data_object(data) end |
#source=(source) ⇒ Object
23 24 25 |
# File 'lib/rabl/engine.rb', line 23 def source=(source) @_source = source end |
#to_bson(options = {}) ⇒ Object
Returns a bson representation of the data object to_bson(:root => true)
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rabl/engine.rb', line 148 def to_bson( = {}) = { :root => Rabl.configuration.include_bson_root }.merge() result = to_dumpable() if !collection_root_name && is_collection?(root_object) && root_object.is_a?(Array) result = { root_name => result } end Rabl.configuration.bson_engine.serialize(result).to_s end |
#to_dumpable(options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/rabl/engine.rb', line 96 def to_dumpable( = {}) = { :child_root => Rabl.configuration.include_child_root }.merge() result = to_hash() result = { collection_root_name => result } if collection_root_name result end |
#to_hash(options = {}) ⇒ Object
Returns a hash representation of the data object to_hash(:root => true, :child_root => true)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rabl/engine.rb', line 77 def to_hash( = {}) = @_options.merge() data = root_object [:root_name] = determine_object_root(data, root_name, [:root]) result = \ if is_object?(data) || !data # object @user Builder.new(data, @_settings, ).to_hash elsif is_collection?(data) # collection @users MultiBuilder.new(data, @_settings, ).to_a end result = escape_output(result) if Rabl.configuration.escape_all_output result end |
#to_json(options = {}) ⇒ Object
Returns a json representation of the data object to_json(:root => true)
108 109 110 111 112 |
# File 'lib/rabl/engine.rb', line 108 def to_json( = {}) = { :root => Rabl.configuration.include_json_root }.merge() result = to_dumpable() 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)
116 117 118 119 120 |
# File 'lib/rabl/engine.rb', line 116 def to_msgpack( = {}) = { :root => Rabl.configuration.include_msgpack_root }.merge() result = to_dumpable() Rabl.configuration.msgpack_engine.pack(result) end |
#to_plist(options = {}) ⇒ Object
Returns a plist representation of the data object to_plist(:root => true)
125 126 127 128 129 |
# File 'lib/rabl/engine.rb', line 125 def to_plist( = {}) = { :root => Rabl.configuration.include_plist_root }.merge() result = to_dumpable() Rabl.configuration.plist_engine.dump(result) end |
#to_xml(options = {}) ⇒ Object
Returns an xml representation of the data object to_xml(:root => true)
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rabl/engine.rb', line 133 def to_xml( = {}) = { :root => (include_root = Rabl.configuration.include_xml_root), :child_root => include_root && Rabl.configuration.include_child_root }.merge() = Rabl.configuration..merge(:root => collection_root_name || root_name) result = to_hash() result.to_xml() end |