Class: Ampt::Config

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

Overview

Class containing all of the configuration details for Ampt.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ Config

Create a new configuration. Optionally takes a parameter, and a block with which to set up configuration options.



43
44
45
46
47
48
49
50
# File 'lib/ampt.rb', line 43

def initialize(*a, &b)
    @commands = {}
    @alias    = {}
    @default  = nil
    @api      = nil
    @base_url = nil
    cloaker(&b).bind(self).call(*a) if b
end

Instance Attribute Details

#__userObject (readonly)

Holds on to user config data



98
99
100
# File 'lib/ampt.rb', line 98

def __user
  @__user
end

Instance Method Details

#alias_cmd(cmd) ⇒ Object

Alias a command. Takes a hash of ‘new command’ => ‘previous command’.



101
102
103
# File 'lib/ampt.rb', line 101

def alias_cmd cmd
    @alias.merge! cmd
end

#api(api) ⇒ Object

Select an API to use. Defaults to the acoustics API. The path should be an absolute path.



112
113
114
# File 'lib/ampt.rb', line 112

def api api
    @api = api
end

#base_url(url = nil) ⇒ Object

Base URL of music player



117
118
119
120
121
122
123
# File 'lib/ampt.rb', line 117

def base_url url = nil
    if url.nil?
        @base_url
    else
        @base_url = url
    end
end

#command(name, &b) ⇒ Object

Define a new Ampt command. Must specify a name, and a block containing the configuration for the command. The configuration should contain one or more desc and opt’s, plus an on_run block. See Ampt::Command for the on_run block.



129
130
131
132
133
134
135
136
137
# File 'lib/ampt.rb', line 129

def command name, &b
    # If the command already exists, reconfigure it. Else, create a new
    # instance of Ampt::Command.
    if @commands[name]
        @commands[name].reconfig &b
    else
        @commands[name] = Ampt::Command.new self, name, &b
    end
end

#default(cmd) ⇒ Object

Set as the default command to run if Ampt is run with no arguments.



106
107
108
# File 'lib/ampt.rb', line 106

def default cmd
    @default = cmd
end

#load_config(file) ⇒ Object

Specify a file path from which to load the config from. If a relative path is provided, the path is taken as relative to the Ampt library directory.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ampt.rb', line 54

def load_config file
    # If we got a relative path, take it as relative to the lib path.
    # Otherwise, just use the absolute path.
    name = if file[0...1] != '/'
        File.dirname(__FILE__) + '/' + file
    else
        file
    end
    # Try to open the file. It may fail, as by default, we try to read
    # from the user's .amptrc, which may not exist.
    begin
        File.open(name, 'r') do |f|
            # Create a new copy of the User module for holding our
            # user-defined methods and variables.
            unless @__user
                @__user = User.dup
                @__user.parent_config = self
            end
            # Read in the configuration file.
            @__user.module_eval(f.read)
        end
    rescue Errno::ENOENT => e
    end
    @base_url ||= "https://www-s.acm.uiuc.edu/acoustics"
    # If the user has configured an API, try to require it in.
    # Otherwise, use the default acoustics API.
    if @api
        begin
            require @api
        rescue Exception => e
            $stderr.puts "Couldn't load API #{@api}"
            $stderr.puts e.message
            exit
        end
    else
        require 'ampt_api/acoustics'
    end
    # Include that API in the command class.
    Command.class_eval do
        include Ampt::API
    end
end

#run(args) ⇒ Object

Run the config. Usually not called manually.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ampt.rb', line 140

def run args
    Trollop::options args, @commands, @alias do |commands, aliases|
        banner "Usage ampt [command] [options] [args]"
        banner "\nCommands include:\n"
        # Print out a nice listing of each command
        commands.each do |name, command|
            banner sprintf("  %-16.16s %s", name, command.desc)
        end
        banner ""
        version "ampt version #{Ampt::VERSION}"
        # Stop parsing if we reach a command
        stop_on(commands.keys + aliases.keys)
    end
    cmd = args.shift
    # Try to figure out if it's an alias or a command or nothing or invalid
    begin
        if @alias[cmd]
            # Go down through the chain of aliases to find the original command.
            # A command could be aliased to itself
            begin
                cmd, *a = @alias[cmd].split(' ')
                args = a + args
            end until @alias[cmd].nil? or cmd == @alias[cmd].split(' ').first
            unless @commands[cmd]
                raise InvalidCommand.new(cmd)
            end
        elsif cmd.nil? and (@default.nil? or @commands[@default].nil?)
            raise InvalidCommand.new(cmd)
        elsif @commands[cmd].nil?
            raise InvalidCommand.new(cmd)
        end
    rescue InvalidCommand => e
        Trollop::die "#{e.cmd} is not an ampt command" if e.cmd
        Trollop::die "No command specified, and no default is set"
    else
        @commands[cmd].run(args)
    end
end