Class: Tapas::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
# File 'lib/tapas/config.rb', line 9

def initialize(args = {})
  @path = ::Pathname.new(args.delete(:path) || default_path)
  @config = default_config
  load_config(path.expand_path)
  args.each { |k,v| config[k] = v }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

def method_missing(name, *args)
  if name.to_s.end_with?('=')
    key = name.to_s.gsub(/=$/, '')
    config[key] = args.first
  else
    config[name]
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/tapas/config.rb', line 6

def path
  @path
end

Instance Method Details

#default_pathObject



16
17
18
# File 'lib/tapas/config.rb', line 16

def default_path
  '~/.tapas'
end

#delete_setting(setting) ⇒ Object



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

def delete_setting(setting)
  config.delete_field setting
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/tapas/config.rb', line 33

def respond_to_missing?(name)
  true
end

#to_sObject



37
38
39
40
41
42
# File 'lib/tapas/config.rb', line 37

def to_s
  max_length = config.to_h.keys.map(&:length).max
  config.each_pair.map do |setting, value|
    "%-#{max_length}s = %s" % format_setting_value(setting, value)
  end.join("\n")
end