Class: Tbox::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/tb-cli/config_file.rb

Overview

ConfigFile class provides some simple YAML configuration file helpers. While normal YAML methods within Ruby are fine (and used heavily), these helpers assist in adding, removing, and replacing configurations for the Torquebox.yml default specification file. These methods make adding a config to the torquebox.yml much easier, as it abstracts out the details of having to know the underlying data structure (Hashes, Arrays, boolean, string) and just pass in to this ConfigFile the pieces you want to add

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination_root = nil, file = 'torquebox.yml') ⇒ ConfigFile

Create and/or open a YAML configuration file [destination_root] Directory that you will find the YAML file [file] Filename of the YAML file to load



17
18
19
20
21
22
23
24
25
# File 'lib/tb-cli/config_file.rb', line 17

def initialize(destination_root=nil, file='torquebox.yml')
  @filename = file
  @torquebox_yml = File.join(destination_root, @filename)
  if config_present?
    @config = YAML.load_file @torquebox_yml
  else
    @config = {}
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/tb-cli/config_file.rb', line 12

def config
  @config
end

#torquebox_ymlObject

Returns the value of attribute torquebox_yml.



12
13
14
# File 'lib/tb-cli/config_file.rb', line 12

def torquebox_yml
  @torquebox_yml
end

Instance Method Details

#add_config(root, config = nil, value = nil) ⇒ String

Dynamically adds to the config object based on what was passed in through Thor

Parameters:

  • options (Thor::CoreExt::HashWithIndifferentAccess)

    options hash given to you by Thor

  • meth (String)

    string of the method passed in

  • config (String) (defaults to: nil)

    what the config for that method sets

  • value (String) (defaults to: nil)

    value assocated with it

Returns:

  • (String)

    the YAML output for this config, e.g.: options = HashWithIndifferentAccess.new 'topic' => 'some_topic_name' add_config(options, "topics", "topic"


    topics: some_topic_name:



52
53
54
55
56
57
58
# File 'lib/tb-cli/config_file.rb', line 52

def add_config(root, config=nil, value=nil)
  conf = @config[root.to_s] || {}
  conf[config.to_s] = value if config
  @config[root.to_s] = nil  # Some attributes need no settings and just exist, like queues.yml
  @config[root.to_s] = conf unless conf.empty?
  puts "Current #{@filename} configuration file:\n\n#{yaml}\n"
end

#config_present?Boolean

Is the configuration file there or does a new config there to laod?

Returns:

  • (Boolean)


28
29
30
# File 'lib/tb-cli/config_file.rb', line 28

def config_present?
  File.exist? @torquebox_yml
end

#remove_config(meth, config) ⇒ Object

Not tested or documented



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tb-cli/config_file.rb', line 61

def remove_config(meth, config)
  conf = @config[meth.to_s]
  unless conf
    puts "No such config available for #{meth}"
  else
    puts "removing config \n\t#{config}"
    begin
      @config.delete(config)
    rescue e
      puts "No such config #{config} to remove, skipping..."
    end
  end
end

#yamlObject

Convert config to YAML



33
34
35
# File 'lib/tb-cli/config_file.rb', line 33

def yaml
  @config.to_yaml
end