Class: Butler::Control

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

Overview

this module provides services to botcontrol for gems, this module 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
# 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%,
		: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



78
79
80
81
82
83
# File 'lib/butler/control.rb', line 78

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
end

#configure_user(user = nil) ⇒ Object

Raises:

  • (Errno::EACCES)


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

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)


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

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

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



85
86
87
# File 'lib/butler/control.rb', line 85

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

#languageObject



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

def language
	"en"
end

#update_configObject



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

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

#user(name = nil) ⇒ Object



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

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

#user_config(user = nil) ⇒ Object



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

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
end