Module: Merb::Helpers::Text
- Included in:
- GlobalHelpers
- Defined in:
- lib/merb-helpers/text_helpers.rb
Instance Method Summary collapse
-
#cycle(*values) ⇒ Object
Allows you to cycle through elements in an array.
-
#reset_cycle(name = :default) ⇒ Object
Allows you to reset a cycle.
Instance Method Details
#cycle(*values) ⇒ Object
Allows you to cycle through elements in an array
Parameters
- values<Array>
-
Array of objects to cycle through
- values<Hash>
-
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!
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/merb-helpers/text_helpers.rb', line 21 def cycle(*values) = (values) || {} key = ([:name] || :default).to_sym (@cycle_positions ||= {})[key] ||= {:position => -1, :values => values} unless values == @cycle_positions[key][:values] @cycle_positions[key] = {:position => -1, :values => values} end current = @cycle_positions[key][:position] @cycle_positions[key][:position] = current + 1 values.at( (current + 1) % values.length).to_s end |
#reset_cycle(name = :default) ⇒ Object
Allows you to reset a cycle
Parameters
- name<Symbol|String>
-
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!
53 54 55 56 |
# File 'lib/merb-helpers/text_helpers.rb', line 53 def reset_cycle(name = :default) (@cycle_positions[name.to_sym] = nil) && true if @cycle_positions && @cycle_positions[name.to_sym] end |