Skip to content

Commit c8d7e66

Browse files
committed
YARD and some documentation
1 parent 85bbb5d commit c8d7e66

File tree

7 files changed

+146
-13
lines changed

7 files changed

+146
-13
lines changed

.yardopts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-m markdown

Gemfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ group :test do
1212
gem "rake"
1313
gem "contest", ">= 0.1.2"
1414
gem "mocha"
15-
gem "yard"
15+
16+
# For documentation
17+
gem "yard", "~> 0.6.1"
18+
gem "bluecloth"
1619

1720
platforms :mri_18 do
1821
gem "ruby-debug"

Gemfile.lock

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ GEM
2424
specs:
2525
abstract (1.0.0)
2626
archive-tar-minitar (0.5.2)
27+
bluecloth (2.0.7)
2728
columnize (0.3.1)
2829
contest (0.1.2)
2930
erubis (2.6.6)
@@ -64,11 +65,12 @@ PLATFORMS
6465
ruby
6566

6667
DEPENDENCIES
68+
bluecloth
6769
contest (>= 0.1.2)
6870
mocha
6971
rake
7072
ruby-debug
7173
ruby-debug19
7274
vagrant!
7375
virtualbox!
74-
yard
76+
yard (~> 0.6.1)

lib/vagrant/box.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Vagrant
44
# virtual machine, at the least. They are created with `vagrant package`
55
# and may contain additional files if specified by the creator. This
66
# class serves to help manage these boxes, although most of the logic
7-
# is kicked out to actions.
7+
# is kicked out to middlewares.
88
class Box
99
# The name of the box.
1010
attr_accessor :name
@@ -46,19 +46,21 @@ def initialize(env=nil, name=nil)
4646
# virtual machine file which contains specifications of the exported
4747
# virtual machine this box contains.
4848
#
49+
# This will only be valid once the box is imported.
50+
#
4951
# @return [String]
5052
def ovf_file
5153
directory.join(env.config.vm.box_ovf)
5254
end
5355

5456
# Begins the process of adding a box to the vagrant installation. This
5557
# method requires that `name` and `uri` be set. The logic of this method
56-
# is kicked out to the {Actions::Box::Add add box} action.
58+
# is kicked out to the `box_add` registered middleware.
5759
def add
5860
env.actions.run(:box_add, { "box" => self })
5961
end
6062

61-
# Begins the process of destroying this box.
63+
# Begins the process of destroying this box. This cannot be undone!
6264
def destroy
6365
env.actions.run(:box_remove, { "box" => self })
6466
end
@@ -69,14 +71,16 @@ def repackage(options=nil)
6971
end
7072

7173
# Returns the directory to the location of this boxes content in the local
72-
# filesystem.
74+
# filesystem. Note that if the box isn't imported yet, then the path may not
75+
# yet exist, but still represents where the box will be imported to.
7376
#
7477
# @return [String]
7578
def directory
7679
env.boxes_path.join(name)
7780
end
7881

79-
# Implemented for comparison with other boxes.
82+
# Implemented for comparison with other boxes. Comparison is implemented
83+
# by simply comparing name.
8084
def <=>(other)
8185
return super if !other.is_a?(self.class)
8286
name <=> other.name

lib/vagrant/cli.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@ module Vagrant
44
# Entrypoint for the Vagrant CLI. This class should never be
55
# initialized directly (like a typical Thor class). Instead,
66
# use {Environment#cli} to invoke the CLI.
7+
#
8+
# # Defining Custom CLI Commands
9+
#
10+
# If you're looking to define custom CLI commands, then look at
11+
# one of the two following classes:
12+
#
13+
# * {Command::Base} - Implementing a single command such as `vagrant up`, e.g.
14+
# one without subcommands. Also take a look at {Command::NamedBase}.
15+
# * {Command::GroupBase} - Implementing a command with subcommands, such as
16+
# `vagrant box`, which has the `list`, `add`, etc. subcommands.
17+
#
18+
# The above linked classes contain the main documentation for each
19+
# type of command.
720
class CLI < Thor
821
# Registers the given class with the CLI so it can be accessed.
9-
# The class must be a subclass of either {Command} or {GroupCommand}.
22+
# The class must be a subclass of either {Command::Base} or {Command::GroupBase}.
23+
# Don't call this method directly, instead call the {Command::Base.register}
24+
# or {Command::GroupBase.register} methods.
25+
#
26+
# @param [Class] klass Command class
27+
# @param [String] name Command name, accessed at `vagrant NAME`
28+
# @param [String] usage Command usage, such as "vagrant NAME [--option]"
29+
# @param [String] description Description of the command shown during the
30+
# command listing.
31+
# @param [Hash] opts Other options (not gone into detail here, look at
32+
# the source instead).
1033
def self.register(klass, name, usage, description, opts=nil)
1134
opts ||= {}
1235

lib/vagrant/command/base.rb

+45-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Vagrant
55
module Command
6-
# A CLI command is the subclass for all commands which are single
6+
# A {Base} is the superclass for all commands which are single
77
# commands, e.g. `vagrant init`, `vagrant up`. Not commands like
88
# `vagrant box add`. For commands which have more subcommands, use
99
# a {GroupBase}.
@@ -15,7 +15,50 @@ module Command
1515
# the command is invoked, it must be made `protected` or `private`.
1616
#
1717
# The best way to get examples of how to create your own command is to
18-
# view the various Vagrant commands, which are relatively simple.
18+
# view the various Vagrant commands, which are relatively simple, and
19+
# can be found in the Vagrant source tree at `lib/vagrant/command/`.
20+
#
21+
# # Defining a New Command
22+
#
23+
# To define a new single command, create a new class which inherits
24+
# from this class, then call {register} to register the command. That's
25+
# it! When the command is invoked, _all public methods_ will be called.
26+
# Below is an example `SayHello` class:
27+
#
28+
# class SayHello < Vagrant::Command::Base
29+
# register "hello", "Says hello"
30+
#
31+
# def hello
32+
# env.ui.info "Hello"
33+
# end
34+
# end
35+
#
36+
# In this case, the above class is invokable via `vagrant hello`. To give
37+
# this a try, just copy and paste the above into a Vagrantfile somewhere.
38+
# The command will be available for that project!
39+
#
40+
# Also note that the above example uses `env.ui` to output. It is recommended
41+
# you use this instead of raw "puts" since it is configurable and provides
42+
# additional functionality, such as colors and asking for user input. See
43+
# the {UI} class for more information.
44+
#
45+
# ## Defining Command-line Options
46+
#
47+
# Most command line actions won't be as simple as `vagrant hello`, and will
48+
# probably require parameters or switches. Luckily, Thor makes adding these
49+
# easy:
50+
#
51+
# class SayHello < Vagrant::Command::Base
52+
# register "hello", "Says hello"
53+
# argument :name, :type => :string
54+
#
55+
# def hello
56+
# env.ui.info "Hello, #{name}"
57+
# end
58+
# end
59+
#
60+
# Then, the above can be invoked with `vagrant hello Mitchell` which would
61+
# output "Hello, Mitchell"
1962
class Base < Thor::Group
2063
include Thor::Actions
2164
include Helpers

lib/vagrant/config.rb

+60-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22
require 'vagrant/config/error_recorder'
33

44
module Vagrant
5-
# The config class is responsible for loading Vagrant configurations
6-
# (usually through Vagrantfiles).
5+
# The config class is responsible for loading Vagrant configurations, which
6+
# are usually found in Vagrantfiles but may also be procs. The loading is done
7+
# by specifying a queue of files or procs that are for configuration, and then
8+
# executing them. The config loader will run each item in the queue, so that
9+
# configuration from later items overwrite that from earlier items. This is how
10+
# Vagrant "scoping" of Vagranfiles is implemented.
11+
#
12+
# If you're looking to create your own configuration classes, see {Base}.
13+
#
14+
# # Loading Configuration Files
15+
#
16+
# If you are in fact looking to load configuration files, then this is the
17+
# class you are looking for. Loading configuration is quite easy. The following
18+
# example assumes `env` is already a loaded instance of {Environment}:
19+
#
20+
# config = Vagrant::Config.new(env)
21+
# config.queue << "/path/to/some/Vagrantfile"
22+
# result = config.load!
23+
#
24+
# p "Your box is: #{result.vm.box}"
25+
#
726
class Config
827
extend Util::StackedProcRunner
928

@@ -12,6 +31,12 @@ class Config
1231
attr_reader :queue
1332

1433
class << self
34+
# Resets the current loaded config object to the specified environment.
35+
# This clears the proc stack and initializes a new {Top} for loading.
36+
# This method shouldn't be called directly, instead use an instance of this
37+
# class for config loading.
38+
#
39+
# @param [Environment] env
1540
def reset!(env=nil)
1641
@@config = nil
1742
proc_stack.clear
@@ -20,14 +45,29 @@ def reset!(env=nil)
2045
config(env)
2146
end
2247

48+
# Returns the current {Top} configuration object. While this is still
49+
# here for implementation purposes, it shouldn't be called directly. Instead,
50+
# use an instance of this class.
2351
def config(env=nil)
2452
@@config ||= Config::Top.new(env)
2553
end
2654

55+
# Adds the given proc/block to the stack of config procs which are all
56+
# run later on a single config object. This is the main way to configure
57+
# Vagrant, and is how all Vagrantfiles are formatted:
58+
#
59+
# Vagrant::Config.run do |config|
60+
# # ...
61+
# end
62+
#
2763
def run(&block)
2864
push_proc(&block)
2965
end
3066

67+
# Executes all the config procs onto the currently loaded {Top} object,
68+
# and returns the final configured object. This also validates the
69+
# configuration by calling {Top#validate!} on every configuration
70+
# class.
3171
def execute!(config_object=nil)
3272
config_object ||= config
3373

@@ -37,6 +77,10 @@ def execute!(config_object=nil)
3777
end
3878
end
3979

80+
# Initialize a {Config} object for the given {Environment}.
81+
#
82+
# @param [Environment] env Environment which config object will be part
83+
# of.
4084
def initialize(env)
4185
@env = env
4286
@queue = []
@@ -65,14 +109,24 @@ def load!
65109
end
66110

67111
class Config
112+
# This class is the "top" configure class, which handles registering
113+
# other configuration classes as well as validation of all configured
114+
# classes. This is the object which is returned by {Environment#config}
115+
# and has accessors to all other configuration classes.
116+
#
117+
# If you're looking to create your own configuration class, see {Base}.
68118
class Top < Base
69119
@@configures = []
70120

71121
class << self
122+
# The list of registered configuration classes as well as the key
123+
# they're registered under.
72124
def configures_list
73125
@@configures ||= []
74126
end
75127

128+
# Registers a configuration class with the given key. This method shouldn't
129+
# be called. Instead, inherit from {Base} and call {Base.configures}.
76130
def configures(key, klass)
77131
configures_list << [key, klass]
78132
attr_reader key.to_sym
@@ -90,7 +144,10 @@ def initialize(env=nil)
90144
end
91145

92146
# Validates the configuration classes of this instance and raises an
93-
# exception if they are invalid.
147+
# exception if they are invalid. If you are implementing a custom configuration
148+
# class, the method you want to implement is {Base#validate}. This is
149+
# the method that checks all the validation, not one which defines
150+
# validation rules.
94151
def validate!
95152
# Validate each of the configured classes and store the results into
96153
# a hash.

0 commit comments

Comments
 (0)