Class: Stove::Config

Inherits:
Object
  • Object
show all
Includes:
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



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

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



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

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

#__has__?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#__path__Object



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

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

#__raw__Object



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

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
  Stove::Log.warn { "No config file found at `#{__path__}'!" }
  @__raw__ = {}
end

#__set__(key, value) ⇒ Object



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

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

#__unset__(key) ⇒ Object



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

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

#inspectObject



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

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

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

Returns:

  • (Boolean)


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

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

#saveObject



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

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

#to_sObject



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

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