Sha256: dce3f16d9b36967f90b79427f2842eec3860c580fb66681637fdc1e33cec1595

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

#--
#   Copyright (C) 2009 Brown Beagle Software
#   Copyright (C) 2009 Darcy Laycock <sutto@sutto.net>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

module GitAuth
  
  class BasicSaveable
    
    class_inheritable_accessor :all, :store_path
    self.all = nil
    
    class << self
      
      def load!
        self.all = YAML.load(File.read(store_path)) rescue nil if File.file?(store_path)
        self.all = [] unless all.is_a?(Array)
      end
      
      def save!
        load! if all.nil?
        File.open(store_path, "w+") { |f| f.write all.to_yaml }
      end
      
      def add_item(item)
        load! if all.nil?
        all << item
        save!
      end
      
    end
  end
  
  def self.SaveableClass(kind)
    klass            = Class.new(BasicSaveable)
    klass.store_path = GitAuth::GITAUTH_DIR.join("#{kind}.yml").to_s
    klass.all        = nil
    return klass
  end
  
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
gitauth-0.1.0 lib/gitauth/saveable_class.rb
mbbx6spp-gitauth-0.0.5.2 lib/gitauth/saveable_class.rb
gitauth-0.0.5.2 lib/gitauth/saveable_class.rb
gitauth-0.0.5.1 lib/gitauth/saveable_class.rb
gitauth-0.0.5.0 lib/gitauth/saveable_class.rb