Class: Boson::Repo

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

Overview

A class for repositories. A repository has a root directory with required subdirectories config/ and commands/ and optional subdirectory lib/. Each repository has a primary config file at config/boson.yml.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Repo

Creates a repository given a root directory.



12
13
14
# File 'lib/boson/repo.rb', line 12

def initialize(dir)
  @dir = dir
end

Instance Attribute Details

#config(reload = false) ⇒ Object

A hash read from the YAML config file at config/boson.yml. See here for an example config file. Top level config keys, library attributes and config attributes need to be symbols.

Config keys for all repositories:

:libraries

Hash of libraries mapping their name to attribute hashes. See Library.new for configurable attributes.

Example:
:libraries=>{'completion'=>{:namespace=>true}}
:command_aliases

Hash of commands names and their aliases. Since this is global it will be read by all libraries. This is useful for quickly creating aliases without having to worry about placing them under the correct library config. For non-global aliasing, aliases should be placed under the :command_aliases key of a library entry in :libraries.

Example:
 :command_aliases=>{'libraries'=>'lib', 'commands'=>'com'}
:defaults

Array of libraries to load at start up for commandline and irb. This is useful for extending boson i.e. adding your own option types since these are loaded before any other libraries. Default is no libraries.

:bin_defaults

Array of libraries to load at start up when used from the commandline. Default is no libraries.

:add_load_path

Boolean specifying whether to add a load path pointing to the lib subdirectory/. This is useful in sharing classes between libraries without resorting to packaging them as gems. Defaults to false if the lib subdirectory doesn’t exist in the boson directory.

Config keys specific to the main repo config ~/.boson/config/boson.yml

:error_method_conflicts

Boolean specifying library loading behavior when its methods conflicts with existing methods in the global namespace. When set to false, Boson automatically puts the library in its own namespace. When set to true, the library fails to load explicitly. Default is false.

:auto_namespace

Boolean which automatically namespaces all user-defined libraries. Be aware this can break libraries which depend on commands from other libraries. Default is false.

:ignore_directories

Array of directories to ignore when detecting local repositories for Boson.local_repo.

:option_underscore_search

When set, OptionParser option values (with :values or :keys) are auto aliased with underscore searching. Default is true. See Util.underscore_search.



64
65
66
# File 'lib/boson/repo.rb', line 64

def config
  @config
end

#dirObject

Returns the value of attribute dir.



10
11
12
# File 'lib/boson/repo.rb', line 10

def dir
  @dir
end

Class Method Details

.commands_dir(dir) ⇒ Object

:nodoc:



6
7
8
# File 'lib/boson/repo.rb', line 6

def self.commands_dir(dir) #:nodoc:
  File.join(dir, 'commands')
end

Instance Method Details

#all_librariesObject

:nodoc:



92
93
94
# File 'lib/boson/repo.rb', line 92

def all_libraries #:nodoc:
  (detected_libraries + config[:libraries].keys).uniq
end

#commands_dirObject

Points to the commands/ subdirectory and is automatically created when called. Used for command libraries.



31
32
33
# File 'lib/boson/repo.rb', line 31

def commands_dir
  @commands_dir ||= (cdir = self.class.commands_dir(@dir)) && FileUtils.mkdir_p(cdir) && cdir
end

#config_dirObject

Points to the config/ subdirectory and is automatically created when called. Used for config files.



17
18
19
# File 'lib/boson/repo.rb', line 17

def config_dir
  @config_dir ||= FileUtils.mkdir_p(config_dir_path) && config_dir_path
end

#config_dir_pathObject



21
22
23
# File 'lib/boson/repo.rb', line 21

def config_dir_path
  "#{dir}/config"
end

#config_file(create_dir = false) ⇒ Object

Path name of main config file. If passed true, parent directory of file is created.



26
27
28
# File 'lib/boson/repo.rb', line 26

def config_file(create_dir=false)
  File.join((create_dir ? config_dir : config_dir_path), 'boson.yml')
end

#detected_librariesObject

:nodoc:



88
89
90
# File 'lib/boson/repo.rb', line 88

def detected_libraries #:nodoc:
  Dir[File.join(commands_dir, '**/*.rb')].map {|e| e.gsub(/^#{commands_dir}\/|\.rb$/, '') }
end

#update_config {|config| ... } ⇒ Object

Updates main config file by passing config into a block to be modified and then saved

Yields:



79
80
81
82
# File 'lib/boson/repo.rb', line 79

def update_config
  yield(config)
  write_config_file
end

#write_config_fileObject

:nodoc:



84
85
86
# File 'lib/boson/repo.rb', line 84

def write_config_file #:nodoc:
  File.open(config_file, 'w') {|f| f.write config.to_yaml }
end