Class: MyHelp::Config
- Inherits:
-
Object
- Object
- MyHelp::Config
- Defined in:
- lib/my_help/config.rb
Overview
make @config from default and load yaml as shown stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#valid_config_keys ⇒ Object
readonly
Returns the value of attribute valid_config_keys.
Instance Method Summary collapse
- #[](sym) ⇒ Object
-
#configure(opts = nil) ⇒ Object
Configure through hash.
-
#configure_with(path) ⇒ Object
Configure through yaml file.
-
#initialize(conf_path = nil) ⇒ Config
constructor
Configuration defaults.
-
#save_config ⇒ Object
save config in @config.
Constructor Details
#initialize(conf_path = nil) ⇒ Config
Configuration defaults
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/my_help/config.rb', line 9 def initialize(conf_path = nil) conf_path ||= ENV["HOME"] local_help_dir = File.join(conf_path, ".my_help") @config = { template_dir: File.("../templates", __dir__), local_help_dir: local_help_dir, conf_file: File.join(local_help_dir, ".my_help_conf.yml"), editor: ENV["EDITOR"] || "emacs", ext: ".org", verbose: false, } @valid_config_keys = @config.keys configure_with(@config[:conf_file]) # YAML.dump(@config, File.open(@config[:conf_file], 'w')) # no good for multiple testers. end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
64 65 66 |
# File 'lib/my_help/config.rb', line 64 def config @config end |
#valid_config_keys ⇒ Object (readonly)
Returns the value of attribute valid_config_keys.
7 8 9 |
# File 'lib/my_help/config.rb', line 7 def valid_config_keys @valid_config_keys end |
Instance Method Details
#[](sym) ⇒ Object
66 67 68 |
# File 'lib/my_help/config.rb', line 66 def [](sym) @config[sym] end |
#configure(opts = nil) ⇒ Object
Configure through hash
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/my_help/config.rb', line 27 def configure(opts = nil) return if opts == nil opts.each do |k, v| if @valid_config_keys.include? k.to_sym @config[k.to_sym] = v elsif k == "".to_sym print "Valid key words are follows:" p @valid_config_keys else raise KeyError.new("Error: keyword '#{k}' is invalid", receiver: @config, key: k) end end @config end |
#configure_with(path) ⇒ Object
Configure through yaml file
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/my_help/config.rb', line 45 def configure_with(path) begin config = YAML.safe_load(IO.read(path), permitted_classes: [Symbol]) rescue Errno::ENOENT => e = "WARNING: #{e.}.\nUsing default conf." $stderr.puts if @config[:verbose] rescue Psych::SyntaxError => e = "WARNING: #{e.}.\nUsing default conf." $stderr.puts if @config[:verbose] end configure(config) end |
#save_config ⇒ Object
save config in @config
60 61 62 |
# File 'lib/my_help/config.rb', line 60 def save_config() File.write(@config[:conf_file], YAML.dump(config)) end |