Sha256: 05934c40eb4ca23922ffc5afe0128680ba711aa4090c5717de0cd47500b87a0f

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

#--
#   Copyright (C) 2009 Brown Beagle Software
#   Copyright (C) 2008 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
  def self.SaveableClass(kind)
    klass = Class.new
    path_name = "#{kind.to_s.upcase}_PATH"
    yaml_file_name = "#{kind}.yml"
    
    saveable_class_def = <<-END
    
      #{path_name} = File.join(GitAuth::GITAUTH_DIR, #{yaml_file_name.inspect})
    
      def self.all
        @@all_#{kind} ||= nil
      end
      
      def self.all=(value)
        @@all_#{kind} = value
      end
      
      def self.load!
        self.all = YAML.load_file(#{path_name}) rescue nil if File.exist?(#{path_name})
        self.all = [] unless self.all.is_a?(Array)
      end

      def self.save!
        load! if self.all.nil?
        File.open(#{path_name}, "w+") do |f|
          f.write self.all.to_yaml
        end
      end
      
      def self.add_item(item)
        self.load! if self.all.nil?
        self.all << item
        self.save!
      end
    
    END
    klass.class_eval(saveable_class_def)
    return klass
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
brownbeagle-gitauth-0.0.2 lib/gitauth/saveable_class.rb
brownbeagle-gitauth-0.0.3.3 lib/gitauth/saveable_class.rb
brownbeagle-gitauth-0.0.3 lib/gitauth/saveable_class.rb
mattman-gitauth-0.0.3.2 lib/gitauth/saveable_class.rb
mattman-gitauth-0.0.3.3 lib/gitauth/saveable_class.rb