Class: Strobe::CLI::Settings

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_filename, global_filename) ⇒ Settings

Returns a new instance of Settings.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/strobe/cli/settings.rb', line 25

def initialize(local_filename, global_filename)
  @local_filename  = Pathname.new(local_filename) if local_filename
  @global_filename = Pathname.new(global_filename)

  if @local_filename && @local_filename.exist?
    @local_hash = YAML.load_file(@local_filename)
  end

  @local_hash = {} unless Hash === @local_hash

  if @global_filename.exist?
    @global_hash = YAML.load_file(@global_filename)
  end

  @global_hash = {} unless Hash === @global_hash
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



23
24
25
# File 'lib/strobe/cli/settings.rb', line 23

def filename
  @filename
end

Class Method Details

.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

.for(path) ⇒ Object



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

def self.for(path)
  new(application_config_file(path), global_config_file)
end

.globalObject



19
20
21
# File 'lib/strobe/cli/settings.rb', line 19

def self.global
  new(nil, global_config_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

def application_path

filename.sub %r[/.strobe/config(\.[a-z]+)?$]i, ''

end



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

def [](k)
  k = key(k)
  ENV[k] || @local_hash[k] || @global_hash[k]
end

#application_pathsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/strobe/cli/settings.rb', line 107

def application_paths
  paths = self[:applications] || []

  # Handle legacy application paths
  # TODO: When it's safe, remove this code
  paths.map! do |p|
    if File.file?(p) && p =~ /\.strobe/
      File.expand_path('../..', p)
    else
      p
    end
  end

  # TODO: Handle the logacy case where applications points
  # to a .strobe/config file
  paths.delete_if do |path|
    !(File.file?("#{path}/.strobe/config") ||
      File.file?("#{path}/strobe/config.json"))
  end

  paths
end

#authenticate!Object



93
94
95
96
97
# File 'lib/strobe/cli/settings.rb', line 93

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

#configsObject



99
100
101
102
103
104
105
# File 'lib/strobe/cli/settings.rb', line 99

def configs
  @configs ||= begin
    application_paths.map do |path|
      Config.new_or_stub(path)
    end
  end
end

#connect!Object



88
89
90
91
# File 'lib/strobe/cli/settings.rb', line 88

def connect!
  Strobe.connection = connection
  authenticate!
end

#connectionObject



82
83
84
85
86
# File 'lib/strobe/cli/settings.rb', line 82

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

#fetch(k, v) ⇒ Object



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

def fetch(k, v)
  self[k] || v
end

#global_set!(k, v) ⇒ Object



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

def global_set!(k, v)
  @global_hash[key(k)] = v
  persist! @global_hash, @global_filename
  v
end

#register_app_path(path) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/strobe/cli/settings.rb', line 130

def register_app_path(path)
  if path == Strobe.user_home
    raise InvalidAppPathError, "Can't register the home directory!"
  end

  paths  = application_paths
  paths |= [File.expand_path(path.to_s)]
  global_set! :applications, paths
end

#set!(k, v) ⇒ Object



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

def set!(k, v)
  @local_hash[key(k)] = v
  persist! @local_hash, @local_filename
  v
end

#unregister_app_path(path) ⇒ Object



140
141
142
143
144
# File 'lib/strobe/cli/settings.rb', line 140

def unregister_app_path(path)
  paths = application_paths
  paths.delete File.expand_path(path.to_s)
  global_set! :applications, paths
end

#unset!(k) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/strobe/cli/settings.rb', line 67

def unset!(k)
  k = key(k)
  if @local_hash[k]
    @local_hash.delete(k)
    persist! @local_hash, @local_filename
  end

  if @global_hash[k]
    @global_hash.delete(k)
    persist! @global_hash, @global_filename
  end

  nil
end