Module: Laborantin::Commands::Config
Defined Under Namespace
Classes: ConfigError, Del, Get, PathError, PathNotHashError, RootNotHashError, Set
Instance Method Summary collapse
-
#build_path(path_arg) ⇒ Object
Creates a path structure in the runner’s config.
-
#get_node(path) ⇒ Object
Given a path made of an array of nodes (i.e., symbols to respect the syntax).
-
#interpret_path(path) ⇒ Object
Interprets a string provided path as an array of symbols (the nodes of the config tree).
-
#interpret_value(val) ⇒ Object
Interprets a string provided value into a ruby object.
-
#save_config ⇒ Object
Stores the runner configuration into the configuration file.
Instance Method Details
#build_path(path_arg) ⇒ Object
Creates a path structure in the runner’s config. If there already a non-hash node, will raise an error.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/laborantin/runner/commands/config.rb', line 76 def build_path(path_arg) path = path_arg.clone tree = runner.config while (node = path.shift) case tree[node] when Hash tree = tree[node] when NilClass tree[node] = Hash.new tree = tree[node] else raise PathNotHashError.new(orig.join(':')) end end end |
#get_node(path) ⇒ Object
Given a path made of an array of nodes (i.e., symbols to respect the syntax). Returns the object corresponding to this node in the configuration tree. Returns nil if there is no such object. If path is empty, simply returns the root node (i.e., the runner’s config).
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/laborantin/runner/commands/config.rb', line 57 def get_node(path) tree = runner.config return tree if path.empty? #easy solution path = path.clone while (node = path.shift) if tree.is_a? Hash if path.empty? #terminal node return tree[node] else tree = tree[node] end else #error return nil end end end |
#interpret_path(path) ⇒ Object
Interprets a string provided path as an array of symbols (the nodes of the config tree).
30 31 32 |
# File 'lib/laborantin/runner/commands/config.rb', line 30 def interpret_path(path) path.split(':').map{|i| i.to_sym} end |
#interpret_value(val) ⇒ Object
Interprets a string provided value into a ruby object.
-
if val is ‘true’ then the true object of class TrueClass
-
if val is ‘false’ then the false object of class FalseClass * if val is
a string that can be read like a decimal, it’s a decimal (Fixnum or Bignum), unlike in ruby interpreter 1_000 is not valid for 1000, see source for details * otherwise the string val itself
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/laborantin/runner/commands/config.rb', line 40 def interpret_value(val) case val when 'true' true when 'false' false when val.to_i.to_s == val val.to_i else val end end |
#save_config ⇒ Object
Stores the runner configuration into the configuration file.
22 23 24 25 26 27 |
# File 'lib/laborantin/runner/commands/config.rb', line 22 def save_config raise RootNotHashError unless runner.config.is_a? Hash File.open(runner.config_path, 'w') do |f| f.puts YAML.dump(runner.config) end end |