Sha256: 0285637e820fc8a56cb202dcabe19833cacfd2fe833fd6ef8d214cd1db81cca2

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'yaml'
require 'forwardable'

module Texico
  class ConfigFile
    extend Forwardable
    
    DEFAULT_NAME       = '.texico'.freeze
    GLOBAL_CONFIG_PATH = File.expand_path(DEFAULT_NAME, ENV['HOME']).freeze
    DEFAULT_CONFIG = {
      name: 'main',
      title: 'Title',
      author: 'Author',
      email: 'author@example.com',
      build: 'build',
      main_file: 'main.tex'
    }.freeze
    
    def_delegator :@config, :[]
    def_delegator :@config, :to_a
    
    def to_hash
      @config.dup
    end
    
    private
    
    def initialize(config, defaults = {})
      @config = defaults.merge(config).freeze
    end
    
    class << self
      def exist?(opts)
        File.exist? opts[:config]
      end
      
      def global
        new read_global
      end
      
      def default
        return @default if @default
        @default = DEFAULT_CONFIG.merge read_global
      end
      
      def load(opts, full = true)
        return false unless File.exist? opts[:config]
        new read_local(opts[:config]), (full ? default : {})
      end
      
      def store(config, dest = '', opts = {})
        return if opts[:dry_run]
        dest_path = File.expand_path opts[:config], dest
        File.open dest_path, 'wb' do |file|
          file.write YAML.dump(config.to_hash)
        end
      end
      
      private
      
      def read_local(filename)
        yaml = File.open(filename, 'rb') { |f| f.read }
        YAML.load(yaml) || {}
      rescue Errno::ENOENT
        {}
      end
      
      def read_global
        @global_defaults ||= read_local GLOBAL_CONFIG_PATH
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
texico-0.2.0 lib/texico/config_file.rb