module DohWeb

Public Instance Methods

cycle(*values) click to toggle source

this is a simplified version of Rails ActionView::Helpers::TextHelper.cycle it only supports a single active cycle at a name, and no options

# File lib/dohweb/util.rb, line 13
def cycle(*values)
  if values != @cycle_values
    @cycle_values = values
    @cycle_index = 0
  else
    @cycle_index += 1
    @cycle_index = 0 if @cycle_index >= @cycle_values.size
  end
  @cycle_values[@cycle_index]
end
flatten_multi_param(map) click to toggle source
# File lib/dohweb/group_params.rb, line 5
def flatten_multi_param(map)
  if map.key?('hour') || map.key?('minute') || map.key?('second')
    DateTime.new(map['year'].to_i, map['month'].to_i, map['day'].to_i, map['hour'].to_i, map['minute'].to_i, map['second'].to_i) rescue nil
  elsif map.key?('year') || map.key?('month') || map.key?('day')
    Date.new(map['year'].to_i, map['month'].to_i, map['day'].to_i) rescue nil
  else
    map.to_s
  end
end
group_params(group, _params = nil) click to toggle source
# File lib/dohweb/group_params.rb, line 15
def group_params(group, _params = nil)
  prefix = "#{group}_"
  prefix_size = prefix.size
  retval = {}

  # first, go through all params to get the group ones, and combine the multi params ones into hashes
  (_params || params).each_pair do |key, value|
    next unless key.start_with?(prefix)
    key = key.slice(prefix_size..-1)

    unless key.index('__')
      retval[key] = value
      next
    end

    key, _, subkey = key.partition('__')
    raise "missing subkey for multi part param #{key}" if subkey.empty?
    parts = (retval[key] ||= {})
    parts[subkey] = value
  end

  # now, go back through and flatten all the multi part params into a single value object
  retval.each_pair do |key, value|
    retval[key] = flatten_multi_param(value) if value.is_a?(Hash)
  end

  retval
end
humanize(lower_case_and_underscored_word) click to toggle source

this is a simplified version of Rails ActiveSupport::Inflector.humanize

# File lib/dohweb/util.rb, line 4
def humanize(lower_case_and_underscored_word)
  retval = lower_case_and_underscored_word.to_s.dup
  retval.gsub!(/_id$/, "")
  retval.gsub!(/_/, ' ')
  retval.gsub(/([a-z\d]*)/i) { |match| "#{match.downcase}" }.gsub(/^\w/) { $&.upcase }
end