Files

Merb::Helpers::Text

Public Instance Methods

cycle(*values) click to toggle source

Allows you to cycle through elements in an array

Parameters

values

Array of objects to cycle through

values

Last element of array can be a hash with the key of :name to specify the name of the cycle

Returns

String

Notes

  • Default name is :default

Example

<%= 5.times { cycle(“odd! “,“even! “} %>

Generates:

odd! even! odd! even! odd!

    # File lib/merb-helpers/text_helpers.rb, line 21
21:   def cycle(*values)
22:     options = extract_options_from_args!(values) || {}
23:     key = (options[:name] || :default).to_sym
24:     (@cycle_positions ||= {})[key] ||= {:position => 1, :values => values}
25:     unless values == @cycle_positions[key][:values]
26:       @cycle_positions[key] = {:position => 1, :values => values}
27:     end
28:     current = @cycle_positions[key][:position]
29:     @cycle_positions[key][:position] = current + 1
30:     values.at( (current + 1) % values.length).to_s
31:   end
reset_cycle(name = :default) click to toggle source

Allows you to reset a cycle

Parameters

name

Name of the cycle

Returns

True if successful, otherwise nil

Notes

  • Default name is :default

Example

<%= cycle(“odd! “,“even! “,“what comes after even?”) %> <%= cycle(“odd! “,“even! “,“what comes after even?”) %> <% reset_cycle %> <%= cycle(“odd! “,“even! “,“what comes after even?”) %>

Generates:

odd! even! odd!

    # File lib/merb-helpers/text_helpers.rb, line 53
53:   def reset_cycle(name = :default)
54:     (@cycle_positions[name.to_sym] = nil) &&
55:       true if @cycle_positions && @cycle_positions[name.to_sym]
56:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.