##############################################################################
#    Sinatra Sample MVC Structure
#    Copyright (C) 2015  Sinan ISLEKDEMIR
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##############################################################################

##############################################################################
#    Sinatra Sample MVC Structure
#    Put your required global functions inside here.
#    They will be available in all CONTROLLERS.
#    For functions inside views, you must use HELPERS
##############################################################################


require 'singleton'
require 'digest'
require 'json'

module SSample
  def test
    'test function'
  end

  # Sinatra Pool is a system-wide variable holder
  # Will not be saved or wont be available for the next request!
  # This is just a way to use GLOBAL variable
  # But as it is not a good thing to keep things global
  # it is better to keep them as an instance
  class SinatraPool
    include Singleton

    def initialize
      @data_pool = {}
    end

    def add(key, value)
      @data_pool[key] = value
    end

    def delete(key)
      @data_pool.delete key
    end

    def append_array(key, data_to_append)
      @data_pool[key] = [] unless @data_pool.key?(key)
      if data_to_append.is_a? Array
        @data_pool[key] += data_to_append
      else
        @data_pool[key] << data_to_append
      end
      @data_pool[key] = @data_pool[key].uniq
      true
    end

    def get(key)
      @data_pool.key?(key) ? @data_pool[key] : nil
    end

    def exists(key)
      @data_pool.key?(key)
    end
  end
end
