Module: ParamsMixin

Defined in:
lib/rango/core_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(hash) ⇒ Object



59
60
61
62
63
64
# File 'lib/rango/core_ext.rb', line 59

def self.convert(hash)
  hash.extend(ParamsMixin)
  hash.each do |key, value|
    self.convert(value) if value.is_a?(Hash)
  end
end

.switch_string_to_symbol_or_back(value) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rango/core_ext.rb', line 49

def self.switch_string_to_symbol_or_back(value)
  if value.is_a?(String)
    value.to_sym
  elsif value.is_a?(Symbol)
    value.to_s
  else
    value
  end
end

Instance Method Details

#[](key) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rango/core_ext.rb', line 66

def [](key)
  if self.has_key?(key)
    super(key)
  elsif key2 = ParamsMixin.switch_string_to_symbol_or_back(key)
    super(key2)
  else # get the default value or run the default block
    super(key)
  end
end

#[]=(key, value) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/rango/core_ext.rb', line 76

def []=(key, value)
  if self.has_key?(key)
    super(key, value)
  elsif key2 = ParamsMixin.switch_string_to_symbol_or_back(key)
    super(key2, value)
  else # get the default value or run the default block
    super(key, value)
  end
end

#keysObject

This might be very nasty if you expect a symbol, but we can’t just call #to_sym on it because of security. Symbols aren’t GCed, so if someone would send a lot of requests with different keys, it will crash your app or even server because it would run out of memory.



92
93
94
95
96
# File 'lib/rango/core_ext.rb', line 92

def keys
  super.map do |key|
    key.is_a?(Symbol) ? key.to_s : key
  end
end

#merge(*args) ⇒ Object



98
99
100
# File 'lib/rango/core_ext.rb', line 98

def merge(*args)
  super(*args).extend(ParamsMixin)
end

#merge!(*args) ⇒ Object



102
103
104
# File 'lib/rango/core_ext.rb', line 102

def merge!(*args)
  super(*args).extend(ParamsMixin)
end