Sha256: 46492deca895fe06c13721a580823b3384703a313045928b28f456a0ce0f5d88

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

require 'set'

module BagIt

  module Info

    def package_info_txt_file
      File.join bag_dir, 'package-info.txt'
    end

    def package_info
      read_info_file package_info_txt_file
    end

    def write_package_info(hash)
      write_info_file package_info_txt_file, hash
    end

    def bagit_txt_file
      File.join bag_dir, 'bagit.txt'
    end

    def bag_info
      read_info_file bagit_txt_file
    end
    
    def write_bag_info(hash)
      write_info_file bagit_txt_file, hash
    end

    protected

    def read_info_file(file)
      
      open(file) do |io|
        
        entries = io.read.split /\n(?=[^\s])/
        
        entries.inject({}) do |hash, line|
          name, value = line.chomp.split /\s*:\s*/
          hash.merge({name => value})
        end
        
      end
      
    end

    def write_info_file(file, hash)

      dups = hash.keys.inject(Set.new) do |acc, key|
        a = hash.keys.grep(/#{key}/i)
        acc + (a.size > 1 ? a : [])
      end
      
      raise "Multiple labels (#{dups.to_a.join ', '}) in #{file}" unless dups.empty?
      
      open(file, 'w') do |io|
        
        hash.each do |name, value|
          simple_entry = "#{name}: #{value.gsub /\s+/, ' '}"
          
          entry = if simple_entry.length > 79
                    simple_entry.wrap(77).indent(2)
                  else
                    simple_entry
                  end
          
          io.puts entry
        end
        
      end
      
    end

  end

end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
flazz-bagit-0.0.1 lib/bagit/info.rb
flazz-bagit-0.0.2 lib/bagit/info.rb
bagit-0.0.4 lib/bagit/info.rb
bagit-0.0.3 lib/bagit/info.rb
bagit-0.0.2 lib/bagit/info.rb