Class: Butler::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/butler/control.rb

Overview

This Class provides services to botcontrol For gems, this class uses the Gem module to get information on the where- abouts of data. If installed using rake from tarball, it is hard-coded.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeControl

Returns a new instance of Control.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/butler/control.rb', line 24

def initialize
	@path = OpenStruct.new({
		:template => Gem.datadir("butler")+'/config_template.yaml', # DELETE
		:config   => Gem.datadir("butler")+'/config.yaml',          # REPLACE: :config   => %CONFIG_DIR%+'/config.yaml',
		:dialogs  => Gem.datadir("butler")+'/dialogs',              # REPLACE: :dialogs  => %DIALOGS_DIR%,
		:plugins  => Gem.datadir("butler")+'/plugins',              # REPLACE: :plugins  => %PLUGINS_DIR%,
		:services => Gem.datadir("butler")+'/services',             # REPLACE: :services => %SERVICES_DIR%,
		:strings  => Gem.datadir("butler")+'/strings',              # REPLACE: :strings  => %STRINGS_DIR%,
		:man      => Gem.datadir("butler")+'/man1',                 # DELETE
	})
	File.write(@path.config, File.read(@path.template)) unless File.exist?(@path.config)
	@config = OpenStruct.new(YAML.load_file(@path.config))
	@dialog = DialogLine.new(@path.dialogs, @config.language)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/butler/control.rb', line 21

def config
  @config
end

#dialogObject (readonly)

Returns the value of attribute dialog.



20
21
22
# File 'lib/butler/control.rb', line 20

def dialog
  @dialog
end

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/butler/control.rb', line 22

def path
  @path
end

Instance Method Details

#butler_path(user = nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/butler/control.rb', line 80

def butler_path(user=nil)
	path        = @config.users[user(user)]
	user_config = OpenStruct.new(YAML.load_file(path))
	user_config.plugin_repository  = @path.plugins
	user_config.service_repository = @path.services
	user_config
end

#configure_user(user = nil) ⇒ Object

Raises:

  • (Errno::EACCES)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/butler/control.rb', line 49

def configure_user(user=nil)
	raise Errno::EACCES unless File.writable?(@path.config)
	user      ||= user()
	installer   = Installer.new("butler")
	user_config = @dialog.discuss(:botcontrol, false,
		:installer  => installer,
		:user       => user,
		:path       => path
	)
	path = @config.users[user] = user_config.delete(:config)+"/config.yaml"
	user_config[:language] = "en" # FIXME multilingualize
	update_config
	FileUtils.mkdir_p(File.dirname(path))
	FileUtils.chown(user, nil, File.dirname(path))
	File.write(path, user_config.to_yaml)
	FileUtils.chown(user, nil, path)
	OpenStruct.new(@config.users[user])
end

#configured?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/butler/control.rb', line 44

def configured?(user=nil)
	path = @config.users[user(user)]
	path && File.exist?(path)
end

#discuss(dir, lang = nil, variables = {}, &block) ⇒ Object



88
89
90
# File 'lib/butler/control.rb', line 88

def discuss(dir, lang=nil, variables={}, &block)
	@dialog.discuss(dir, lang, variables.merge(:botcontrol => self), &block)
end

#languageObject



92
93
94
# File 'lib/butler/control.rb', line 92

def language
	"en"
end

#update_configObject



68
69
70
# File 'lib/butler/control.rb', line 68

def update_config
	File.write(@path.config, @config.to_hash.to_yaml)
end

#user(name = nil) ⇒ Object



39
40
41
42
# File 'lib/butler/control.rb', line 39

def user(name=nil)
	# use Etc.getlogin?
	name || ENV['SUDO_USER'] || ENV['USER'] || @dialog.discuss("username", false)[:username]
end

#user_config(user = nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/butler/control.rb', line 72

def user_config(user=nil)
	path        = @config.users[user(user)]
	user_config = OpenStruct.new(YAML.load_file(path))
	user_config.plugin_repository  = @path.plugins
	user_config.service_repository = @path.services
	user_config
end