Class: Billy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/billy/config.rb

Constant Summary collapse

BILLYRC =
'.billyrc'
SEPARATOR =
': '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/billy/config.rb', line 10

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

Instance Attribute Details

#storageObject

Returns the value of attribute storage.



7
8
9
# File 'lib/billy/config.rb', line 7

def storage
  @storage
end

#storage_pathObject

Returns the value of attribute storage_path.



8
9
10
# File 'lib/billy/config.rb', line 8

def storage_path
  @storage_path
end

Class Method Details

.instanceObject



102
103
104
# File 'lib/billy/config.rb', line 102

def instance
  @@instance ||= self.new
end

.method_missing(m, *args, &block) ⇒ Object



110
111
112
# File 'lib/billy/config.rb', line 110

def method_missing( m, *args, &block )
  instance.send( m, *args, &block ) if instance.respond_to?( m )
end

.settingsObject



106
107
108
# File 'lib/billy/config.rb', line 106

def settings
  instance.storage
end

Instance Method Details

#clearObject



52
53
54
# File 'lib/billy/config.rb', line 52

def clear
  self.storage.clear
end

#config_exists?(dir) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/billy/config.rb', line 20

def config_exists?( dir )
  File.exists?( File.expand_path( dir + "/#{BILLYRC}" ) )
end

#eat_string_config(string_config) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/billy/config.rb', line 83

def eat_string_config( string_config )
  clear
  string_config.each_line do |line|
    next unless !line.empty?
    items = line.split( SEPARATOR )
    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

#loadObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/billy/config.rb', line 32

def load
  %w(. ~).each do |path|
    begin
      load!( File.expand_path( path ) )
      return true
    rescue
      next
    end
  end
  
  false
end

#load!(dir) ⇒ Object



45
46
47
48
49
50
# File 'lib/billy/config.rb', line 45

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

#reload!(dir = nil) ⇒ Object



56
57
58
59
60
# File 'lib/billy/config.rb', line 56

def reload!( dir = nil )
  dir ||= storage_path
  clear
  load!( dir )
end

#save(dir = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/billy/config.rb', line 62

def save( dir = nil )
  dir ||= Dir.pwd
  begin
    save!( dir, true )
    true
  rescue
    false
  end
end

#save!(dir, force = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/billy/config.rb', line 72

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_sObject



24
25
26
27
28
29
30
# File 'lib/billy/config.rb', line 24

def to_s
  [].tap { |res|
    self.storage.each_pair do |k, v|
      res.push "#{k}#{SEPARATOR}#{v}"
    end
  }.push( "" ).join( "\n" )
end