Class: Rhales::TemplateEngine::EachContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/template_engine.rb

Overview

Context wrapper for {{#each} iterations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_context, current_item, current_index, items_var) ⇒ EachContext

Returns a new instance of EachContext.



321
322
323
324
325
326
# File 'lib/rhales/template_engine.rb', line 321

def initialize(parent_context, current_item, current_index, items_var)
  @parent_context = parent_context
  @current_item   = current_item
  @current_index  = current_index
  @items_var      = items_var
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



360
361
362
363
364
365
366
# File 'lib/rhales/template_engine.rb', line 360

def method_missing(method_name, *)
  if @parent_context.respond_to?(method_name)
    @parent_context.public_send(method_name, *)
  else
    super
  end
end

Instance Attribute Details

#current_indexObject (readonly)

Returns the value of attribute current_index.



319
320
321
# File 'lib/rhales/template_engine.rb', line 319

def current_index
  @current_index
end

#current_itemObject (readonly)

Returns the value of attribute current_item.



319
320
321
# File 'lib/rhales/template_engine.rb', line 319

def current_item
  @current_item
end

#items_varObject (readonly)

Returns the value of attribute items_var.



319
320
321
# File 'lib/rhales/template_engine.rb', line 319

def items_var
  @items_var
end

#parent_contextObject (readonly)

Returns the value of attribute parent_context.



319
320
321
# File 'lib/rhales/template_engine.rb', line 319

def parent_context
  @parent_context
end

Instance Method Details

#get(variable_name) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rhales/template_engine.rb', line 328

def get(variable_name)
  # Handle special each variables
  case variable_name
  when 'this', '.'
    return @current_item
  when '@index'
    return @current_index
  when '@first'
    return @current_index == 0
  when '@last'
    # We'd need to know the total length for this
    return false
  end

  # Check if it's a property of the current item
  if @current_item.respond_to?(:[])
    item_value = @current_item[variable_name] || @current_item[variable_name.to_sym]
    return item_value unless item_value.nil?
  end

  if @current_item.respond_to?(variable_name)
    return @current_item.public_send(variable_name)
  end

  # Fall back to parent context
  @parent_context.get(variable_name) if @parent_context.respond_to?(:get)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/rhales/template_engine.rb', line 356

def respond_to?(method_name)
  super || @parent_context.respond_to?(method_name)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/rhales/template_engine.rb', line 368

def respond_to_missing?(method_name, include_private = false)
  super || @parent_context.respond_to?(method_name, include_private)
end