Class: Myoack::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/myoack/config_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(home = MYOACK_HOME) ⇒ ConfigManager

Returns a new instance of ConfigManager.



9
10
11
12
13
14
15
16
# File 'lib/myoack/config_manager.rb', line 9

def initialize home=MYOACK_HOME
  @home = home
  @init_file_path = File.join(@home, 'init.rb')
  @keys_file_path = File.join(@home, 'keys.yml')
  @configs = {}
  @config_types = {}
  @config_classes = {}
end

Instance Attribute Details

#config_classesObject (readonly)

Returns the value of attribute config_classes.



54
55
56
# File 'lib/myoack/config_manager.rb', line 54

def config_classes
  @config_classes
end

#config_typesObject (readonly)

Returns the value of attribute config_types.



53
54
55
# File 'lib/myoack/config_manager.rb', line 53

def config_types
  @config_types
end

#configsObject (readonly)

Returns the value of attribute configs.



52
53
54
# File 'lib/myoack/config_manager.rb', line 52

def configs
  @configs
end

#init_file_pathObject (readonly)

Returns the value of attribute init_file_path.



50
51
52
# File 'lib/myoack/config_manager.rb', line 50

def init_file_path
  @init_file_path
end

#keys_file_pathObject (readonly)

Returns the value of attribute keys_file_path.



51
52
53
# File 'lib/myoack/config_manager.rb', line 51

def keys_file_path
  @keys_file_path
end

Instance Method Details

#<<(cfgclass) ⇒ Object



43
44
45
46
47
48
# File 'lib/myoack/config_manager.rb', line 43

def << cfgclass
  cfg = cfgclass.new(self)
  add_config(cfgclass.id, cfg) if cfgclass.id
  add_config_type(cfgclass.type, cfgclass) if cfgclass.type
  configure cfgclass.id
end

#add_config(id, cfg) ⇒ Object



85
86
87
# File 'lib/myoack/config_manager.rb', line 85

def add_config id, cfg
  configs[id.to_s] = cfg
end

#add_config_type(type, cfgclass) ⇒ Object



89
90
91
# File 'lib/myoack/config_manager.rb', line 89

def add_config_type type, cfgclass
  config_types[type.to_s] = cfgclass
end

#authorize!(id) ⇒ Object Also known as: authorize



36
37
38
39
# File 'lib/myoack/config_manager.rb', line 36

def authorize! id
  cfg = require_config(id)
  cfg.authorize!
end

#configure(id, sitecfg = nil, cfg = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/myoack/config_manager.rb', line 74

def configure id, sitecfg=nil, cfg=nil
  return nil unless id
  sitecfg ||= load_keys_file[id.to_s] or return nil
  cfg ||= configs[id.to_s] ||
          ( cfgclass = config_types[sitecfg["type"]];
            cfgclass and cfgclass.new(self)) or return nil
  sitecfg.each { |k,v| cfg.send(:"#{k}=", v) }
  add_config id, cfg
  cfg
end

#configure_allObject



66
67
68
69
70
71
72
# File 'lib/myoack/config_manager.rb', line 66

def configure_all
  cfg = load_keys_file
  cfg.each do |id, sitecfg|
    configure(id, sitecfg) if configs.key? id or sitecfg["site"] && sitecfg["type"]
  end
  true
end

#dump_config_file(cfg) ⇒ Object



60
61
62
63
64
# File 'lib/myoack/config_manager.rb', line 60

def dump_config_file cfg
  open @keys_file_path, 'w' do |f|
    f.write YAML.dump(cfg)
  end
end

#init_as_cliObject



29
30
31
32
33
34
# File 'lib/myoack/config_manager.rb', line 29

def init_as_cli
  configure_all
  if File.exist? @init_file_path
    load @init_file_path
  end
end

#load_keys_fileObject



56
57
58
# File 'lib/myoack/config_manager.rb', line 56

def load_keys_file
  YAML.load_file @keys_file_path
end

#main(*argv) ⇒ Object



24
25
26
27
# File 'lib/myoack/config_manager.rb', line 24

def main *argv
  init_as_cli
  option_parser.parse! argv
end

#option_parserObject



18
19
20
21
22
# File 'lib/myoack/config_manager.rb', line 18

def option_parser
  OptionParser.new.tap do |opts|
    opts.on('--authorize ID', 'Try authorize me on the <ID> which is in "$HOME/.myoack/keys.yml".') { |id| authorize(id)  }
  end
end

#require_config(id) ⇒ Object



93
94
95
# File 'lib/myoack/config_manager.rb', line 93

def require_config id
  configs[id.to_s] or (configure id; configs[id.to_s]) or raise "Myoack don't know '#{id}'."
end