Sha256: d495d2e247ad2fc914e221153854c047bfbe86c2f0af4b05f6d91cfd5cd7ecb1

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 Bytes

Contents

class FreezingEmail::Storage
  class << self
    def cleanup(mask = "")
      FileUtils.rm_rf(files_list(mask))
    end

    def index
      emails = []

      files_list.each do |file|
        emails << load(file)
      end

      emails
    end

    def save(name, object)
      File.open(expand_name(name), 'w') { |f| f.puts YAML::dump(object) }
    end

    def load(name)
      file_name = expand_name(name)
      if File.exists?(file_name)
        YAML::load(IO.read(file_name))
      end
    end

    def dir
      dir = FreezingEmail::Config[:store_path]

      if defined?(Rails)
        dir = Rails.root.join(dir)
      end

      Dir.mkdir(dir) unless Dir.exists?(dir)

      dir
    end

    private

    def files_list(mask = "")
      Dir.glob(File.join(dir, "#{mask}*.yml"))
    end

    def expand_name(name)
      name = File.join(dir, "#{File.basename(name)}")
      name = "#{name}.yml" unless File.extname(name) == ".yml"
      name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
freezing_email-0.0.2 lib/freezing_email/storage.rb