Module: Wice::WgHash

Defined in:
lib/wice/wice_grid_core_ext.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.add_or_append_class_value!(hash, klass_value, prepend = false) ⇒ Object

Used to modify options submitted to view helpers. If there is no :klass option, it will be added, if there is, the css class name will be appended to the existing class name(s)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wice/wice_grid_core_ext.rb', line 19

def add_or_append_class_value!(hash, klass_value, prepend = false) #:nodoc:
  if hash.key?('class')
    hash[:class] = hash['class']
    hash.delete('class')
  end

  hash[:class] = if hash.key?(:class)
    if prepend
      "#{klass_value} #{hash[:class]}"
    else
      "#{hash[:class]} #{klass_value}"
    end
  else
    klass_value
  end

  hash
end

.deep_clone(hash) ⇒ Object

if there’s a hash of hashes, the original structure and the returned structure should not contain any shared deep hashes



6
7
8
9
10
11
12
13
14
# File 'lib/wice/wice_grid_core_ext.rb', line 6

def deep_clone(hash)  #:nodoc:
  cloned = hash.clone
  cloned.keys.each do |k|
    if cloned[k].is_a?(Hash)
      cloned[k] = Wice::WgHash.deep_clone cloned[k]
    end
  end
  cloned
end

.make_hash(key, value) ⇒ Object

Used mostly for submitting options to view helpers, that is, like this:

(:th, col_link, Wice::WgHash.make_hash(:class, css_class))

In some it is important that if the value is empty, no option is submitted at all. Thus, there’s a check for an empty value



42
43
44
# File 'lib/wice/wice_grid_core_ext.rb', line 42

def make_hash(key, value) #:nodoc:
  value.blank? ? {} : { key => value }
end

.parameter_names_and_values(hash, initial = []) ⇒ Object

Used to transform a traditional params hash into an array of two element arrays where element zero is a parameter name as it appears in HTTP requests, and the first element is the value: { a: { b: 3, c: 4, d: { e: 5 }} }.parameter_names_and_values #=> [[“a[e]”, 5], [“a”, 3], [“a”, 4]] The parameter is an optional array of parameter names to prepend: { a: { b: 3, c: 4, d: { e: 5 }} }.parameter_names_and_values([‘foo’, ‘baz’]) #=>

[["foo[baz][a][d][e]", 5], ["foo[baz][a][b]", 3], ["foo[baz][a][c]", 4]]


69
70
71
72
73
74
75
76
# File 'lib/wice/wice_grid_core_ext.rb', line 69

def parameter_names_and_values(hash, initial = []) #:nodoc:
  res = []
  recursively_gather_finite_non_hash_values_with_key_path(hash, res, [])
  res.collect do |parameter_struct|
    parameter_struct[0] = initial + parameter_struct[0]
    [Wice::WgArray.to_parameter_name(parameter_struct[0]), parameter_struct[1]]
  end
end

.rec_merge(hash, other) ⇒ Object

A deep merge of two hashes. That is, if both hashes have the same key and the values are hashes, these two hashes should also be merged. Used for merging two sets of params.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wice/wice_grid_core_ext.rb', line 49

def rec_merge(hash, other)  #:nodoc:
  res = hash.clone
  other.each do |key, other_value|
    value = res[key]
    if value.is_a?(Hash) && other_value.is_a?(Hash)
      res[key] = rec_merge value, other_value
    else
      res[key] = other_value
    end
  end
  res
end