Class: Boson::Library

Inherits:
Object
  • Object
show all
Includes:
API, Loader
Defined in:
lib/boson/library.rb

Overview

A library is a group of commands (Command objects) usually grouped together by a module. Libraries are loaded from different sources depending on the library subclass.

Creating Your Own Library

To create your own subclass you need to define what sources the subclass can handle with handles(). See Loader to see what instance methods to override for a subclass.

Direct Known Subclasses

RunnerLibrary

Defined Under Namespace

Modules: API

Constant Summary

ATTRIBUTES =

Public attributes for use outside of Boson.

[:commands, :loaded, :module, :name]

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from API

#before_initialize, #config, #local?, #namespace_object

Methods included from Loader

#actual_load_commands, #after_include, #before_load_commands, #clean_library_commands, #detect_additions, #handle_method_conflict_error, #load, #load_commands, #load_commands?, #load_source_and_set_module, #loaded_correctly?, #method_conflicts, #module_callbacks, #set_library_commands

Constructor Details

- (Library) initialize(hash)

Creates a library object with the given hash. Each hash pair maps directly to an instance variable and value. Defaults for attributes are read from config[@library_name].

Options Hash (hash):

  • :name (String)

    Required attribute

  • :commands (Array, Hash)

    Commands belonging to a library. A hash configures command attributes for the given commands with command names pointing to their configs. See Command.new for a command's configurable attributes. If an array, the commands are set for the given library, overidding default command detection. Example:

    :commands=>{'commands'=>{:desc=>'Lists commands', :alias=>'com'}}
  • :force (Boolean)

    Forces a library to ignore when a library's methods are overriding existing ones. Use with caution. Default is false.



40
41
42
43
44
45
46
47
48
49
# File 'lib/boson/library.rb', line 40

def initialize(hash)
  before_initialize
  @name = set_name(hash.delete(:name)) or
    raise ArgumentError, "Library missing required key :name"
  @loaded = false
  @commands_hash = {}
  @commands = []
  set_config (config[:libraries][@name] || {}).merge(hash), true
  set_command_aliases(config[:command_aliases])
end

Class Attribute Details

+ (Object) handle_blocks

Returns the value of attribute handle_blocks



13
14
15
# File 'lib/boson/library.rb', line 13

def handle_blocks
  @handle_blocks
end

Instance Attribute Details

- (Object) lib_file (readonly)

Private attribute for use within Boson.



24
25
26
# File 'lib/boson/library.rb', line 24

def lib_file
  @lib_file
end

- (Object) new_commands (readonly)

Private attribute for use within Boson.



24
25
26
# File 'lib/boson/library.rb', line 24

def new_commands
  @new_commands
end

- (Object) new_module (readonly)

Private attribute for use within Boson.



24
25
26
# File 'lib/boson/library.rb', line 24

def new_module
  @new_module
end

Class Method Details

+ (Object) handles(&block)

Returns true when the subclass is chosen to load.



15
16
17
# File 'lib/boson/library.rb', line 15

def handles(&block)
  (Library.handle_blocks ||= []) << [self,block]
end

Instance Method Details

- (Object) clean_name

handles names under directories



58
59
60
# File 'lib/boson/library.rb', line 58

def clean_name
  @name[/\w+$/]
end

- (Object) command_object(name)

Command object for given command name



96
97
98
# File 'lib/boson/library.rb', line 96

def command_object(name)
  command_objects([name])[0]
end

- (Object) command_objects(names = self.commands, command_array = Boson.commands)

Command objects of library's commands



91
92
93
# File 'lib/boson/library.rb', line 91

def command_objects(names=self.commands, command_array=Boson.commands)
  command_array.select {|e| names.include?(e.name) && e.lib == self.name }
end

- (Object) library_type

A concise symbol version of a library type i.e. FileLibrary -> :file.



52
53
54
55
# File 'lib/boson/library.rb', line 52

def library_type
  str = self.class.to_s[/::(\w+)Library$/, 1] || 'library'
  str.downcase.to_sym
end

- (Object) set_name(name)

sets name



63
64
65
# File 'lib/boson/library.rb', line 63

def set_name(name)
  name.to_s
end