Module: WGHashExtensions

Included in:
Hash
Defined in:
lib/wice_grid_core_ext.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


4
5
6
# File 'lib/wice_grid_core_ext.rb', line 4

def self.included(base)  #:nodoc:
  base.extend(ClassMethods)
end

Instance Method Details

#add_or_append_class_value!(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)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wice_grid_core_ext.rb', line 24

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

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

  return self
end

#deep_clone_ylObject

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


11
12
13
14
15
16
17
18
19
# File 'lib/wice_grid_core_ext.rb', line 11

def deep_clone_yl  #:nodoc:
  cloned = self.clone
  cloned.keys.each do |k|
    if cloned[k].kind_of?(Hash)
      cloned[k] = cloned[k].deep_clone_yl
    end
  end
  cloned
end

#parameter_names_and_values(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]]

51
52
53
54
55
56
57
58
# File 'lib/wice_grid_core_ext.rb', line 51

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

#rec_merge(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.


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wice_grid_core_ext.rb', line 64

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