Module: DohWeb

Defined in:
lib/dohweb/util.rb,
lib/dohweb/html_tags.rb,
lib/dohweb/form_builder.rb,
lib/dohweb/group_params.rb

Defined Under Namespace

Modules: HtmlTags Classes: FormBuilder

Instance Method Summary collapse

Instance Method Details

#cycle(*values) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object

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



4
5
6
7
8
9
# 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