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 collapse

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

#initialize(hash) ⇒ Library

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].

Parameters:

  • hash (Hash)

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

.handle_blocksObject

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

#lib_fileObject (readonly)

Private attribute for use within Boson.



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

def lib_file
  @lib_file
end

#new_commandsObject (readonly)

Private attribute for use within Boson.



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

def new_commands
  @new_commands
end

#new_moduleObject (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

.handles(&block) ⇒ Object

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

#clean_nameObject

handles names under directories



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

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

#command_object(name) ⇒ Object

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

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

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

#library_typeObject

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

#set_name(name) ⇒ Object

sets name



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

def set_name(name)
  name.to_s
end