class Jasonette::Template

Attributes

template_lookup_options[RW]
__layout[RW]
__locals[RW]
attributes[R]
context[R]

Public Class Methods

load(context) click to toggle source
# File lib/jasonette/template.rb, line 36
def load context
  JasonSingleton.fetch context
end
new(context) click to toggle source
# File lib/jasonette/template.rb, line 61
def initialize context
  @context = context
  @attributes = {}
  @blocks = []
  self.extend ContexEmbedder if @context.present?
end

Public Instance Methods

array!(collection = [], *args) click to toggle source
Calls superclass method
# File lib/jasonette/template.rb, line 112
def array! collection = [], *args
  super
end
attributes!() click to toggle source
# File lib/jasonette/template.rb, line 100
def attributes!
  @attributes
end
build(name=nil, &block)
Alias for: jason
encode(builder, &block) click to toggle source
# File lib/jasonette/template.rb, line 79
def encode builder, &block
  if ::Kernel.block_given?
    @blocks << block
    block.instance_variable_set("@builder", builder)
    instance_eval(&block)
    @blocks.delete(block)
  else
    raise "Wrong encode"
  end
  self
end
extract!(object, *attributes) click to toggle source
Calls superclass method
# File lib/jasonette/template.rb, line 116
def extract! object, *attributes
  super
end
has_layout?(template_id) click to toggle source
# File lib/jasonette/template.rb, line 68
def has_layout? template_id
  template = _get_view_template(template_id)
  _layout_path && _layout_path != template.virtual_path
end
jason(name=nil, &block) click to toggle source
# File lib/jasonette/template.rb, line 91
def jason name=nil, &block
  builder = Jasonette::Jason.new(context)
  encode(builder, &block)
  @attributes[name || "$jason"] = builder.attributes!
  JasonSingleton.reset context
  self
end
Also aliased as: build
merge!(key) click to toggle source
Calls superclass method
# File lib/jasonette/template.rb, line 120
def merge! key
  case key
  when ActiveSupport::SafeBuffer
    values = ::MultiJson.load(key) || {}

    if template = _get_view_template(values["_template"])
      options = { locals: __locals, template: template.virtual_path }
      _render_partial_with_options options
    end
  else
    super
  end
  @attributes
end
method_missing(name, *args, &block) click to toggle source
# File lib/jasonette/template.rb, line 42
def method_missing name, *args, &block
  if _last_builder
    if _last_builder.property_names.include? name
      _last_builder.public_send name, *args, &block
    else
      begin
        context_method name, *args, &block
      rescue
        _last_builder.public_send name, *args, &block
      end
    end
  else
    context_method name, *args, &block
  end
end
new_jason(template_id) click to toggle source
# File lib/jasonette/template.rb, line 73
def new_jason template_id
  new_jason = self.class.new context
  new_jason.attributes["_template"] = "#{template_id}"
  new_jason
end
partial!(*args) click to toggle source
# File lib/jasonette/template.rb, line 135
def partial! *args
  _render_explicit_partial(*args)
end
set!(name, object = nil, *args) click to toggle source
Calls superclass method
# File lib/jasonette/template.rb, line 108
def set! name, object = nil, *args
  super
end
target!() click to toggle source
# File lib/jasonette/template.rb, line 104
def target!
  ::MultiJson.dump attributes!
end

Private Instance Methods

_get_view_template(template_id) click to toggle source
# File lib/jasonette/template.rb, line 153
def _get_view_template template_id
  ObjectSpace._id2ref(template_id.to_i)
end
_last_builder() click to toggle source
# File lib/jasonette/template.rb, line 145
def _last_builder
  @blocks.last.instance_variable_get("@builder")
end
_layout_path() click to toggle source
# File lib/jasonette/template.rb, line 149
def _layout_path
  __layout && __layout.try(:virtual_path)
end
_render_explicit_partial(name_or_options, locals = {}) click to toggle source
# File lib/jasonette/template.rb, line 157
def _render_explicit_partial(name_or_options, locals = {})
  case name_or_options
    when ::Hash
      # partial! partial: 'name', foo: 'bar'
      options = name_or_options
    else
      # partial! 'name', locals: {foo: 'bar'}
      if locals.one? && (locals.keys.first == :locals)
        options = locals.merge(partial: name_or_options)
      else
        options = { partial: name_or_options, locals: locals }
      end
    # partial! 'name', foo: 'bar'
    # TODO : Add feature for option :as and :collection
    # as = locals.delete(:as)
    # options[:as] = as if as.present?
    # options[:collection] = locals[:collection] if locals.key?(:collection)
  end

  _render_partial_with_options options
end
_render_partial(options) click to toggle source
# File lib/jasonette/template.rb, line 186
def _render_partial(options)
  options[:locals].merge! _jasonette_handler: _last_builder
  context.render options
end
_render_partial_with_options(options) click to toggle source
# File lib/jasonette/template.rb, line 179
def _render_partial_with_options(options)
  options.reverse_merge! locals: {}
  options.reverse_merge! self.class.template_lookup_options

  _render_partial options
end
context_method(name, *args, &block) click to toggle source
# File lib/jasonette/template.rb, line 141
def context_method name, *args, &block
  context.public_send(name, *args, &block)
end