class Billy::Config

Constants

BILLYRC
SEPARATOR
SEPARATOR_PATTERN

Attributes

storage[RW]
storage_path[RW]

Public Class Methods

new() click to toggle source
# File lib/billy/config.rb, line 97
def initialize
  self.storage = Hash.new
end

Protected Class Methods

instance() click to toggle source
# File lib/billy/config.rb, line 103
def instance
  @@instance ||= self.new
end
method_missing( m, *args, &block ) click to toggle source
# File lib/billy/config.rb, line 111
def method_missing( m, *args, &block )
  instance.send( m, *args, &block ) if instance.respond_to?( m )
end
settings() click to toggle source
# File lib/billy/config.rb, line 107
def settings
  instance.storage
end

Public Instance Methods

clear() click to toggle source
# File lib/billy/config.rb, line 53
def clear
  self.storage.clear
end
config_exists?( dir ) click to toggle source
# File lib/billy/config.rb, line 21
def config_exists?( dir )
  File.exists?( File.expand_path( dir + "/#{BILLYRC}" ) )
end
eat_string_config( string_config ) click to toggle source
# File lib/billy/config.rb, line 84
def eat_string_config( string_config )
  clear
  string_config.each_line do |line|
    next unless !line.empty?
    items = line.split( SEPARATOR_PATTERN )
    k = items.shift
    v = items.join( SEPARATOR ).strip
    ( self.storage[ k.to_s ] = v ) unless k.nil? || k.empty? || v.nil? || v.empty?
  end
end
load() click to toggle source
# File lib/billy/config.rb, line 33
def load
  %w(. ~).each do |path|
    begin
      load!( File.expand_path( path ) )
      return true
    rescue
      next
    end
  end
  
  false
end
load!( dir ) click to toggle source
# File lib/billy/config.rb, line 46
def load!( dir )
  self.storage_path = File.expand_path( dir )
  file_path = storage_path + "/#{BILLYRC}"
  raise "Config was not found in #{path}" unless File.exists?( file_path )
  eat_string_config( File.read( file_path ) )
end
method_missing( m, *args, &block ) click to toggle source
# File lib/billy/config.rb, line 11
def method_missing( m, *args, &block )
  if m.to_s[ /=$/ ].nil?
    self.storage[ m.to_s ]
  else
    key = ( m.to_s )[ /^([^=]+)/ ]
    val = args.shift
    ( self.storage[ key ] = val ) unless key.nil? && key.empty?
  end
end
reload!( dir = nil ) click to toggle source
# File lib/billy/config.rb, line 57
def reload!( dir = nil )
  dir ||= storage_path
  clear
  load!( dir )
end
save( dir = nil ) click to toggle source
# File lib/billy/config.rb, line 63
def save( dir = nil )
  dir ||= Dir.pwd
  begin
    save!( dir, true )
    true
  rescue
    false
  end
end
save!( dir, force = false ) click to toggle source
# File lib/billy/config.rb, line 73
def save!( dir, force = false )
  raise 'Directory name should not be empty' unless !dir.empty?
  raise "Directory #{dir.to_s} doesn't exist" unless File.exist?( File.expand_path( dir ) )
  billyrc_path = File.expand_path( dir + "/#{BILLYRC}" )
  raise "Config already exists in #{billyrc_path}" unless !File.exists?( billyrc_path ) || force
  File.open( billyrc_path, 'w' ) { |file|
    file.flush
    file.write( self.to_s )
  }
end
to_s() click to toggle source
# File lib/billy/config.rb, line 25
def to_s
  [].tap { |res|
    self.storage.each_pair do |k, v|
      res.push "#{k}#{SEPARATOR}#{v}"
    end
  }.push( '' ).join( "\n" )
end