Module: ParamsMixin

Defined in:
lib/rango/core_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(hash) ⇒ Object



53
54
55
56
57
58
# File 'lib/rango/core_ext.rb', line 53

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



43
44
45
46
47
48
49
50
51
# File 'lib/rango/core_ext.rb', line 43

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



60
61
62
63
64
65
66
67
68
# File 'lib/rango/core_ext.rb', line 60

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



70
71
72
73
74
75
76
77
78
# File 'lib/rango/core_ext.rb', line 70

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.



86
87
88
89
90
# File 'lib/rango/core_ext.rb', line 86

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

#merge(*args) ⇒ Object



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

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

#merge!(*args) ⇒ Object



96
97
98
# File 'lib/rango/core_ext.rb', line 96

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