Class: Coral::Config::Project

Inherits:
Coral::Config show all
Includes:
Mixin::SubConfig
Defined in:
lib/coral_core/config/project.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::SubConfig

#config, #config=, #defaults, #directory, #directory=, #export, #get, #name, #name=

Methods inherited from Coral::Config

#[], #[]=, array, #array, #defaults, ensure, #export, #filter, filter, #get, #get_array, #get_hash, #hash, hash, #init, init, init_flat, #string, string, #string_map, string_map, #symbol, symbol, #symbol_map, symbol_map, test, #test

Methods included from Mixin::ConfigOptions

#clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property

Methods included from Mixin::Lookup

#hiera, #hiera_config, #initialized?, #lookup, #lookup_array, #lookup_hash, #normalize

Methods included from Mixin::ConfigOps

#parse

Constructor Details

#initialize(data = {}, defaults = {}, force = true) ⇒ Project




10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/coral_core/config/project.rb', line 10

def initialize(data = {}, defaults = {}, force = true)
  super(data, defaults, force)
  
  init_subconfig(true)
  
  unless _get(:project)
    _set(:project, Repository.open(_delete(:directory, Dir.pwd), {
      :origin    => _delete(:origin),
      :revision  => _delete(:revision)
    }))
  end
  
  _init(:autoload, true)
  _init(:autosave, true)
  _init(:autocommit, true)
  _init(:commit_message, 'Saving state')
  
  self.config_file = _get(:config_file, '')
end

Class Method Details

.finalize(file_name) ⇒ Object




32
33
34
35
36
# File 'lib/coral_core/config/project.rb', line 32

def self.finalize(file_name)
  proc do
    Util::Disk.close(file_name)
  end
end

Instance Method Details

#autocommit(default = false) ⇒ Object




80
81
82
# File 'lib/coral_core/config/project.rb', line 80

def autocommit(default = false)
  return _get(:autocommit, default)
end

#autocommit=(autocommit) ⇒ Object




86
87
88
# File 'lib/coral_core/config/project.rb', line 86

def autocommit=autocommit
  _set(:autocommit, test(autocommit))
end

#autoload(default = false) ⇒ Object


Property accessors / modifiers



56
57
58
# File 'lib/coral_core/config/project.rb', line 56

def autoload(default = false)
  return _get(:autoload, default)
end

#autoload=(autoload) ⇒ Object




62
63
64
# File 'lib/coral_core/config/project.rb', line 62

def autoload=autoload
  _set(:autoload, test(autoload))
end

#autosave(default = false) ⇒ Object




68
69
70
# File 'lib/coral_core/config/project.rb', line 68

def autosave(default = false)
  return _get(:autosave, default)
end

#autosave=(autosave) ⇒ Object




74
75
76
# File 'lib/coral_core/config/project.rb', line 74

def autosave=autosave
  _set(:autosave, test(autosave))
end

#can_persist?Boolean


Checks

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/coral_core/config/project.rb', line 47

def can_persist?
  success = project.can_persist?
  success = false if Util::Data.empty?(@absolute_config_file)
  return success
end

#clear(options = {}) ⇒ Object




166
167
168
169
170
# File 'lib/coral_core/config/project.rb', line 166

def clear(options = {})
  super
  save(options) if autosave
  return self
end

#commit_message(default = false) ⇒ Object




92
93
94
# File 'lib/coral_core/config/project.rb', line 92

def commit_message(default = false)
  return _get(:commit_message, default)
end

#commit_message=(commit_message) ⇒ Object




98
99
100
# File 'lib/coral_core/config/project.rb', line 98

def commit_message=commit_message
  _set(:commit_message, string(commit_message))
end

#config_file(default = nil) ⇒ Object




110
111
112
# File 'lib/coral_core/config/project.rb', line 110

def config_file(default = nil)
  return _get(:config_file, default)
end

#config_file=(file) ⇒ Object




116
117
118
119
120
121
# File 'lib/coral_core/config/project.rb', line 116

def config_file=file
  unless Util::Data.empty?(file)
    _set(:config_file, Util::Disk.filename(file))
  end    
  set_absolute_config_file
end

#delete(keys, options = {}) ⇒ Object




158
159
160
161
162
# File 'lib/coral_core/config/project.rb', line 158

def delete(keys, options = {})
  super(keys)
  save(options) if autosave
  return self
end

#import(properties, options = {}) ⇒ Object


Import / Export



175
176
177
178
179
# File 'lib/coral_core/config/project.rb', line 175

def import(properties, options = {})
  super(properties, options)
  save(options) if autosave
  return self
end

#load(options = {}) ⇒ Object


Configuration loading / saving



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/coral_core/config/project.rb', line 184

def load(options = {})
  local_config = Config.ensure(options)
  
  if can_persist? && File.exists?(@absolute_config_file)
    raw = Util::Disk.read(@absolute_config_file)    
    if raw && ! raw.empty?
      config.clear if local_config.get(:override, false)
      config.import(Util::Data.parse_json(raw), local_config)
    end
  end
  return self
end

#project(default = nil) ⇒ Object




104
105
106
# File 'lib/coral_core/config/project.rb', line 104

def project(default = nil)
  return _get(:project, default)
end

#rm(options = {}) ⇒ Object




214
215
216
217
218
219
220
221
222
# File 'lib/coral_core/config/project.rb', line 214

def rm(options = {})
  local_config = Config.ensure(options)
  
  if can_persist?
    config.clear
    File.delete(@absolute_config_file)
    project.commit(@absolute_config_file, local_config) if autocommit
  end  
end

#save(options = {}) ⇒ Object




199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/coral_core/config/project.rb', line 199

def save(options = {})
  local_config = Config.ensure(options)
  
  if can_persist?
    rendering = Util::Data.to_json(config.export, local_config.get(:pretty, true))
    if rendering && ! rendering.empty?
      Util::Disk.write(@absolute_config_file, rendering)
      project.commit(@absolute_config_file, local_config) if autocommit
    end
  end
  return self
end

#set(keys, value = '', options = {}) ⇒ Object




150
151
152
153
154
# File 'lib/coral_core/config/project.rb', line 150

def set(keys, value = '', options = {})
  super(keys, value)
  save(options) if autosave
  return self
end

#set_location(directory) ⇒ Object




139
140
141
142
143
144
145
146
# File 'lib/coral_core/config/project.rb', line 139

def set_location(directory)
  if directory && directory.is_a?(Coral::Repository)
    project.set_location(directory.directory)
  elsif directory && directory.is_a?(String) || directory.is_a?(Symbol)
    project.set_location(directory.to_s)
  end
  set_absolute_config_file if directory
end