Class: Strobe::CLI::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/cli/settings.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = self.class.global_config_file) ⇒ Settings

Returns a new instance of Settings.



19
20
21
22
23
24
25
26
27
# File 'lib/strobe/cli/settings.rb', line 19

def initialize(filename = self.class.global_config_file)
  @filename = filename

  if @filename.exist?
    @hash = YAML.load(@filename) || {}
  else
    @hash = {}
  end
end

Class Method Details

.application(root) ⇒ Object



15
16
17
# File 'lib/strobe/cli/settings.rb', line 15

def self.application(root)
  new application_config_file(root)
end

.application_config_file(root) ⇒ Object



10
11
12
13
# File 'lib/strobe/cli/settings.rb', line 10

def self.application_config_file(root)
  file = ENV['STROBE_CONFIG'] || 'config'
  Pathname.new(root).join(".strobe/#{file}")
end

.global_config_fileObject



5
6
7
8
# File 'lib/strobe/cli/settings.rb', line 5

def self.global_config_file
  file = ENV['STROBE_CONFIG'] || 'config'
  Pathname.new(Strobe.user_home).join(".strobe/#{file}")
end

Instance Method Details

#[](k) ⇒ Object



29
30
31
# File 'lib/strobe/cli/settings.rb', line 29

def [](k)
  ENV[key(k)] || @hash[key(k)]
end

#[]=(k, val) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/strobe/cli/settings.rb', line 33

def []=(k, val)
  @hash[key(k)] = val

  # Persist the setting
  FileUtils.mkdir_p(@filename.dirname)
  File.open(@filename, "wb") do |file|
    file.puts @hash.to_yaml
  end

  val
end

#authenticate!Object



56
57
58
59
60
# File 'lib/strobe/cli/settings.rb', line 56

def authenticate!
  if token = self[:token]
    connection.authenticate_with_token(token)
  end
end

#connect!Object



51
52
53
54
# File 'lib/strobe/cli/settings.rb', line 51

def connect!
  Strobe.connection = connection
  authenticate!
end

#connectionObject



45
46
47
48
49
# File 'lib/strobe/cli/settings.rb', line 45

def connection
  return @connection if @connection
  host = self[:url] || "http://api.strobeapp.com/"
  @connection = Connection.new(host)
end

#deleteObject



62
63
64
# File 'lib/strobe/cli/settings.rb', line 62

def delete
  FileUtils.rm_f(@filename)
end