Class: Vagrant::Smartos::Zones::Models::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/smartos/zones/models/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Config

Returns a new instance of Config.



18
19
20
# File 'lib/vagrant/smartos/zones/models/config.rb', line 18

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



16
17
18
# File 'lib/vagrant/smartos/zones/models/config.rb', line 16

def env
  @env
end

#hashObject (readonly)

Returns the value of attribute hash.



16
17
18
# File 'lib/vagrant/smartos/zones/models/config.rb', line 16

def hash
  @hash
end

Class Method Details

.config(env) ⇒ Object



12
13
14
# File 'lib/vagrant/smartos/zones/models/config.rb', line 12

def self.config(env)
  @config ||= new(env).load
end

.parse_cli(env, *args) ⇒ Object



8
9
10
# File 'lib/vagrant/smartos/zones/models/config.rb', line 8

def self.parse_cli(env, *args)
  config(env).parse(args)
end

Instance Method Details

#delete(name, *_args) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/vagrant/smartos/zones/models/config.rb', line 33

def delete(name, *_args)
  if hash.delete(name)
    env.ui.info(I18n.t('vagrant.smartos.zones.config.delete.success',
                       key: name))
  else
    env.ui.info(I18n.t('vagrant.smartos.zones.config.delete.failed',
                       key: name))
  end
end

#loadObject



49
50
51
52
53
# File 'lib/vagrant/smartos/zones/models/config.rb', line 49

def load
  @hash = YAML.load_file(path) if File.exist?(path)
  @hash ||= {}
  self
end

#parse(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant/smartos/zones/models/config.rb', line 22

def parse(args)
  ui.info(hash.inspect) && return if args.empty?
  options = args.pop if args.last.is_a?(Hash)
  if options && options[:delete]
    delete args.shift
  else
    set(*args)
  end
  save
end

#saveObject



55
56
57
# File 'lib/vagrant/smartos/zones/models/config.rb', line 55

def save
  File.open(path, 'w') { |f| f.write(YAML.dump(hash)) }
end

#set(name, value) ⇒ Object



43
44
45
46
47
# File 'lib/vagrant/smartos/zones/models/config.rb', line 43

def set(name, value)
  env.ui.info(I18n.t('vagrant.smartos.zones.config.set',
                     key: name, value: value))
  hash[name] = value
end