Sha256: 48b326c4472b507e4ea15d73c16b7d5eb72bb14cf54e35f46dc46505179c5a8d

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# coding: utf-8

module Nephos

  # 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}.
  class Cookies < Params

    # @param hash [Hash] hash containing the parameters
    def initialize(hash={})
      raise ArgumentError, "the first argument must be a Hash" unless hash.is_a? Hash
      # TODO: take care of the path
      @hash = Hash[hash.map{|k,v| [k.to_s, {content: v, path: "/"}]}]
    end

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

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

    def []= i, v, path="/"
      @hash[i.to_s] = {content: v.to_s, path: path.to_s}
    end

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

    def set_path i, v
      raise InvalidPath, v unless v.is_a? String
      @hash[i.to_s][:path] = URI.encode(v)
    end

    def to_h
      return @hash
    end
    alias :to_hash :to_h

    def to_s
      return to_h.to_s
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nephos-server-0.7.0 lib/nephos-server/cookies.rb