Class: Jasonette::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/jasonette/template.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Template

Returns a new instance of Template.



61
62
63
64
65
66
# File 'lib/jasonette/template.rb', line 61

def initialize context
  @context = context
  @attributes = {}
  @blocks = []
  self.extend ContexEmbedder if @context.present?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

Class Attribute Details

.template_lookup_optionsObject

Returns the value of attribute template_lookup_options.



34
35
36
# File 'lib/jasonette/template.rb', line 34

def template_lookup_options
  @template_lookup_options
end

Instance Attribute Details

#__layoutObject

Returns the value of attribute __layout.



58
59
60
# File 'lib/jasonette/template.rb', line 58

def __layout
  @__layout
end

#__localsObject

Returns the value of attribute __locals.



58
59
60
# File 'lib/jasonette/template.rb', line 58

def __locals
  @__locals
end

#attributesObject (readonly)

Returns the value of attribute attributes.



59
60
61
# File 'lib/jasonette/template.rb', line 59

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



59
60
61
# File 'lib/jasonette/template.rb', line 59

def context
  @context
end

Class Method Details

.load(context) ⇒ Object



36
37
38
# File 'lib/jasonette/template.rb', line 36

def load context
  JasonSingleton.fetch context
end

Instance Method Details

#array!(collection = [], *args) ⇒ Object



112
113
114
# File 'lib/jasonette/template.rb', line 112

def array! collection = [], *args
  super
end

#attributes!Object



100
101
102
# File 'lib/jasonette/template.rb', line 100

def attributes!
  @attributes
end

#encode(builder, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



116
117
118
# File 'lib/jasonette/template.rb', line 116

def extract! object, *attributes
  super
end

#has_layout?(template_id) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# 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) ⇒ Object Also known as: build



91
92
93
94
95
96
97
# 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

#merge!(key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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

#new_jason(template_id) ⇒ Object



73
74
75
76
77
# 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) ⇒ Object



135
136
137
# File 'lib/jasonette/template.rb', line 135

def partial! *args
  _render_explicit_partial(*args)
end

#set!(name, object = nil, *args) ⇒ Object



108
109
110
# File 'lib/jasonette/template.rb', line 108

def set! name, object = nil, *args
  super
end

#target!Object



104
105
106
# File 'lib/jasonette/template.rb', line 104

def target!
  ::MultiJson.dump attributes!
end