Module: Zena::Use::Context::ZafuMethods

Includes:
RubyLess
Defined in:
lib/zena/use/context.rb

Overview

ViewMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dom_id_procObject



146
147
148
149
150
# File 'lib/zena/use/context.rb', line 146

def self.dom_id_proc
  Proc.new do |h, r, s|
    {:class => String, :method => h.node.dom_id(:code => true)}
  end
end

Instance Method Details

#node(klass = nil) ⇒ Object

Return the node context for a given class (looks up into the hierarchy) or the current node context if klass is nil.



164
165
166
# File 'lib/zena/use/context.rb', line 164

def node(klass = nil)
  super(klass && klass.kind_of?(VirtualClass) ? klass.real_class : klass)
end

#r_contextObject Also known as: r_find

Enter a new context (<r:context find=‘all’ select=‘pages’>). This is the same as ‘<r:pages>…</r:pages>’). It is considered better style to use ‘<r:pages>…</r:pages>’ instead of the more general ‘<r:context>’ because the tags give a clue on the context at start and end. Another way to open a context is the ‘do’ syntax: “<div do=‘pages’>…</div>”.



171
172
173
174
# File 'lib/zena/use/context.rb', line 171

def r_context
  return parser_error("missing 'select' parameter") unless method = @params[:select]
  querybuilder_eval(method)
end

#r_countObject



178
179
180
181
182
183
184
185
# File 'lib/zena/use/context.rb', line 178

def r_count
  if node.list_context? && !@params[:select]
    rubyless_eval
  else
    @params[:find] = 'count'
    r_context
  end
end

#r_dom_idObject



156
157
158
# File 'lib/zena/use/context.rb', line 156

def r_dom_id
  out node.dom_id
end

#r_each_groupObject



268
269
270
# File 'lib/zena/use/context.rb', line 268

def r_each_group
  r_each
end

#r_groupObject

Group elements in a list. Use :order to specify order.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/zena/use/context.rb', line 221

def r_group
  return parser_error("cannot be used outside of a list") unless node.list_context?
  return parser_error("missing 'by' clause") unless key = @params[:by]

  #sort_key = @params[:sort] || 'title'
  # if node.will_be?(DataEntry) && DataEntry::NodeLinkSymbols.include?(key.to_sym)
  #   key = "#{key}_id"
  #   #sort_block = "{|e| (e.#{key} || {})[#{sort_key.to_sym.inspect}]}"
  #   group_array = "group_array(#{node}) {|e| e.#{key}}"
  # elsif node.will_be?(Node)
  #   if ['project', 'parent', 'section'].include?(key)
  #     #sort_block  = "{|e| (e.#{key} || {})[#{sort_key.to_sym.inspect}]}"
  #     group_array = "group_array(#{node}) {|e| e.#{key}_id}"
  #   end
  # end

  if %w{parent project section}.include?(key)
    key = "e.#{key}_id"
  else
    receiver = RubyLess::TypedString.new('e', :class => node.klass.first, :query => node.opts[:query])
    key = RubyLess.translate(receiver, key)
  end

  #if sort_block
  #  out "<% grp_#{list_var} = sort_array(#{group_array}) #{sort_block} %>"
  #else
  #end
  method = "group_array(#{node}) {|e| #{key}}"
  out "<% if #{var} = #{method} %>"
    open_node_context({:method => method}, :node => node.move_to(var, [node.klass], :query => node.opts[:query])) do
      if descendant('each_group')
        out expand_with
      else
        @var = nil
        r_each
      end
    end
  out "<% end %>"

  #if descendant('each_group')
  #  out expand_with(:group => var)
  #else
  #  @context[:group] = var
  #  r_each_group
  #end
end

#r_master_templateObject



272
273
274
275
276
277
278
# File 'lib/zena/use/context.rb', line 272

def r_master_template
  if template = @context[:master_template]
    expand_if("#{var} = secure(Node) { Node.find_by_zip(#{template.zip}) }", node.move_to(var, template.vclass))
  else
    out ''
  end
end

#r_setObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/zena/use/context.rb', line 187

def r_set
  @params.each do |var, code|
    var = var.to_s
    begin
      typed_string = ::RubyLess.translate(self, code)
      
      # Do we have this variable already ?
      if up_var = get_context_var('set_var', var)
        if up_var.klass != typed_string.klass
          return parser_error("Type mismatch for var #{var}=#{code}: #{typed_string.klass} != #{up_var.klass}")
        end
        
        if typed_string.could_be_nil?
          if !up_var.could_be_nil?
            out "<% #{up_var} = #{typed_string} || #{up_var} %>"
          else
            out "<% #{up_var} = #{typed_string} %>"
          end
        else  
          out "<% #{up_var} = #{typed_string} %>"
        end
      else
        name = get_var_name('set_var', var)
        out "<% #{name} = #{typed_string} %>"
        set_context_var('set_var', var, RubyLess::TypedString.new(name, typed_string.opts))
      end
    rescue RubyLess::NoMethodError => err
      out parser_error(err.message, "set #{var}=#{code}")
    end
  end
  expand_with
end