Module: Merb::Helpers::Text

Included in:
GlobalHelpers
Defined in:
merb-helpers/lib/merb-helpers/text_helpers.rb

Instance Method Summary (collapse)

Instance Method Details

- (String) cycle(*values)

Note:

Default name is :default.

Allows you to cycle through elements in an array.

Examples:

5.times { cycle("odd! ","even! "}
# => "odd! even! odd! even! odd! "


15
16
17
18
19
20
21
22
23
24
25
# File 'merb-helpers/lib/merb-helpers/text_helpers.rb', line 15

def cycle(*values)
  options = extract_options_from_args!(values) || {}
  key = (options[: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

- (true?) reset_cycle(name = :default)

TODO:

Docs, correctness, true/false would be better?

Note:

Default name is :default.

Reset a cycle.

Examples:

cycle("odd! ","even! ","what comes after even?")
cycle("odd! ","even! ","what comes after even?")
reset_cycle
cycle("odd! ","even! ","what comes after even?")
# => "odd! even! odd! "


42
43
44
45
# File 'merb-helpers/lib/merb-helpers/text_helpers.rb', line 42

def reset_cycle(name = :default)
  (@cycle_positions[name.to_sym] = nil) &&
    true if @cycle_positions && @cycle_positions[name.to_sym]
end