Class: Rabl::Engine

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rabl/engine.rb

Instance Method Summary collapse

Methods included from Helpers

#data_name, #data_object, #fetch_source, #is_collection?, #is_object?, #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, options={})
  @_source = source
  @_options = 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



182
183
184
# File 'lib/rabl/engine.rb', line 182

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



92
93
94
95
96
97
98
99
100
# File 'lib/rabl/engine.rb', line 92

def attribute(*args)
  if args.first.is_a?(Hash)
    args.first.each_pair { |k,v| self.attribute(k, :as => v) }
  else # array of attributes
    options = args.extract_options!
    @_options[:attributes] ||= {}
    args.each { |name| @_options[:attributes][name] = options[:as] || name }
  end
end

#child(data, options = {}, &block) ⇒ Object

Creates a child node that is included in json output child(@user) { attribute :full_name }



114
115
116
117
# File 'lib/rabl/engine.rb', line 114

def child(data, options={}, &block)
  @_options[:child] ||= []
  @_options[:child].push({ :data => data, :options => options, :block => block })
end

#code(name = nil, 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” }



106
107
108
109
# File 'lib/rabl/engine.rb', line 106

def code(name = nil, options={}, &block)
  @_options[:code] ||= []
  @_options[:code] << { :name => name, :options => options, :block => block }
end

#collection(data) ⇒ Object

Sets the object as a collection casted to a simple array collection @users collection @users => :people



84
85
86
87
# File 'lib/rabl/engine.rb', line 84

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 }



128
129
130
131
# File 'lib/rabl/engine.rb', line 128

def extends(file, options={}, &block)
  @_options[:extends] ||= []
  @_options[:extends].push({ :file => file, :options => 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 }



121
122
123
124
# File 'lib/rabl/engine.rb', line 121

def glue(data, &block)
  @_options[:glue] ||= []
  @_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



135
136
137
# File 'lib/rabl/engine.rb', line 135

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



77
78
79
# File 'lib/rabl/engine.rb', line 77

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
24
25
26
27
# File 'lib/rabl/engine.rb', line 14

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
    instance_eval(@_source) if @_source.present?
  end
  instance_eval(&block) if block_given?
  self.send("to_" + @_options[:format].to_s)
end

#to_hash(options = {}) ⇒ Object

Returns a hash representation of the data object to_hash(:root => true, :child_root => true)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rabl/engine.rb', line 31

def to_hash(options={})
  options = options.reverse_merge(@_options)
  data = data_object(@_data)
  if is_object?(data) || !data # object @user
    Rabl::Builder.new(@_data, options).to_hash(options)
  elsif is_collection?(data) # collection @users
    if options[:root] # only calculate root name if needed
      object_name = data_name(@_data).to_s.singularize # @users => :users
      data.map { |object| Rabl::Builder.new({ object => object_name }, options).to_hash(options) }
    else
      data.map { |object| Rabl::Builder.new(object, options).to_hash(options) }
    end
  end
end

#to_json(options = {}) ⇒ Object

Returns a json representation of the data object to_json(:root => true)



48
49
50
51
52
53
# File 'lib/rabl/engine.rb', line 48

def to_json(options={})
  include_root = Rabl.configuration.include_json_root
  options = options.reverse_merge(:root => include_root, :child_root => include_root)
  result = defined?(@_collection_name) ? { @_collection_name => to_hash(options) } : to_hash(options)
  format_json(result)
end

#to_msgpack(options = {}) ⇒ Object

Returns a msgpack representation of the data object to_msgpack(:root => true)



57
58
59
60
61
62
# File 'lib/rabl/engine.rb', line 57

def to_msgpack(options={})
  include_root = Rabl.configuration.include_msgpack_root
  options = options.reverse_merge(:root => include_root, :child_root => include_root)
  result = defined?(@_collection_name) ? { @_collection_name => to_hash(options) } : to_hash(options)
  Rabl.configuration.msgpack_engine.pack result
end

#to_xml(options = {}) ⇒ Object

Returns an xml representation of the data object to_xml(:root => true)



66
67
68
69
70
71
# File 'lib/rabl/engine.rb', line 66

def to_xml(options={})
  include_root = Rabl.configuration.include_xml_root
  options = options.reverse_merge(:root => include_root, :child_root => include_root)
  xml_options = Rabl.configuration.default_xml_options.merge(:root => data_name(@_data))
  to_hash(options).to_xml(xml_options)
end