Class: Stove::Config

Inherits:
Object
  • Object
show all
Includes:
Logify, Mixin::Instanceable
Defined in:
lib/stove/config.rb

Instance Method Summary collapse

Methods included from Mixin::Instanceable

extended, included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(m, *args, &block)
  if m.to_s.end_with?('=')
    __set__(m.to_s.chomp('='), args.first)
  else
    __get__(m)
  end
end

Instance Method Details

#__get__(key) ⇒ Object



36
37
38
# File 'lib/stove/config.rb', line 36

def __get__(key)
  __raw__[key.to_sym]
end

#__has__?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/stove/config.rb', line 40

def __has__?(key)
  __raw__.key?(key.to_sym)
end

#__path__Object



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

def __path__
  @path ||= File.expand_path(ENV['STOVE_CONFIG'] || '~/.stove')
end

#__raw__Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/stove/config.rb', line 56

def __raw__
  return @__raw__ if @__raw__

  @__raw__ = JSON.parse(File.read(__path__), symbolize_names: true)

  if @__raw__.key?(:community)
    $stderr.puts "Detected old Stove configuration file, converting..."

    @__raw__ = {
      :username => @__raw__[:community][:username],
      :key      => @__raw__[:community][:key],
    }
  end

  @__raw__
rescue Errno::ENOENT => e
  log.warn { "No config file found at `#{__path__}'!" }
  @__raw__ = {}
end

#__set__(key, value) ⇒ Object



44
45
46
# File 'lib/stove/config.rb', line 44

def __set__(key, value)
  __raw__[key.to_sym] = value
end

#__unset__(key) ⇒ Object



48
49
50
# File 'lib/stove/config.rb', line 48

def __unset__(key)
  __raw__.delete(key.to_sym)
end

#inspectObject



32
33
34
# File 'lib/stove/config.rb', line 32

def inspect
  "#<#{self.class.name} #{__raw__.inspect}>"
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/stove/config.rb', line 17

def respond_to_missing?(m, include_private = false)
  __has__?(m) || super
end

#saveObject



21
22
23
24
25
26
# File 'lib/stove/config.rb', line 21

def save
  FileUtils.mkdir_p(File.dirname(__path__))
  File.open(__path__, 'w') do |f|
    f.write(JSON.fast_generate(__raw__))
  end
end

#to_sObject



28
29
30
# File 'lib/stove/config.rb', line 28

def to_s
  "#<#{self.class.name} #{__raw__.to_s}>"
end