module Bicycle::Methods
Public Instance Methods
Returns the current cycle string after a cycle has been started. Useful for complex table highlighting or any other design need which requires the current cycle string in more than one place.
# File lib/bicycle/methods.rb, line 31 def current_cycle(name = "default") cycle = get_cycle(name) cycle&.current_value end
Creates a Cycle
object whose to_s method cycles through elements of an array every time it is called. This can be used for example, to alternate classes for table rows. You can use named cycles to allow nesting in loops. Passing a Hash as the last parameter with a :name
key will create a named cycle. The default name for a cycle without a :name
key is "default"
. You can manually reset a cycle by calling reset_cycle
and passing the name of the cycle. The current cycle string can be obtained anytime using the current_cycle
method.
# File lib/bicycle/methods.rb, line 14 def cycle(*values) if values.last.instance_of? Hash params = values.pop name = params[:name] else name = "default" end cycle = get_cycle(name) cycle = set_cycle(name, Cycle.new(values)) unless cycle && cycle.values == values cycle.to_s end
Resets a cycle so that it starts from the first element the next time it is called. Pass in name
to reset a named cycle.
# File lib/bicycle/methods.rb, line 39 def reset_cycle(name = "default") cycle = get_cycle(name) cycle&.reset end
Private Instance Methods
The cycle helpers need to store the cycles in a place that is guaranteed to be reset every time a page is rendered, so it uses an instance variable of ActionView::Base.
# File lib/bicycle/methods.rb, line 86 def get_cycle(name) @_cycles = {} unless defined?(@_cycles) @_cycles[name] end
# File lib/bicycle/methods.rb, line 91 def set_cycle(name, cycle_object) @_cycles = {} unless defined?(@_cycles) @_cycles[name] = cycle_object end