Class: Nephos::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/nephos-server/params.rb

Overview

Params to a hash, where every elements are accessibles via a stringified key so, even if the entry was added with the key :KEY, it will be accessible via a string equivalent to :key.to_s

param["key"] == param[:key]

Every methods present in Hash are usable in Param.

Direct Known Subclasses

Cookies

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Params

Returns a new instance of Params.

Parameters:

  • hash (Hash) (defaults to: {})

    hash containing the parameters

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/nephos-server/params.rb', line 14

def initialize(hash={})
  raise ArgumentError, "the first argument must be a Hash" unless hash.is_a? Hash
  @hash = Hash[hash.map{|k,v| [k.to_s, v]}]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



19
20
21
# File 'lib/nephos-server/params.rb', line 19

def method_missing m, *a
  @hash.send(m, *(a.map(&:to_s)))
end

Instance Method Details

#[](i) ⇒ Object



23
24
25
# File 'lib/nephos-server/params.rb', line 23

def [] i
  @hash[i.to_s]
end

#[]=(i, v) ⇒ Object



27
28
29
# File 'lib/nephos-server/params.rb', line 27

def []= i, v
  @hash[i.to_s] = v.to_s
end

#to_hObject Also known as: to_hash



31
32
33
# File 'lib/nephos-server/params.rb', line 31

def to_h
  return @hash
end

#to_sObject



36
37
38
# File 'lib/nephos-server/params.rb', line 36

def to_s
  return to_h.to_s
end