Class: Lightning::Bolt

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

Overview

A Bolt object is mainly a user-defined set of globs required by a Function object to translate paths.

Globs

Globs are interpreted by Dir.glob and are fairly similar to shell globs. Some glob examples:

  • Files ending with specific file extensions for a given directory.
     "/some/path/*.{rb,erb}"-> Matches files ending with .rb or .erb
    
  • Files and directories that don’t start with specific letters.
     "[^ls]*" -> Matches anything not starting with l or s
    
  • All directories, however many levels deep, under the current directory.
     "**/"
    

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Bolt) initialize(name)

A new instance of Bolt



27
28
29
30
31
32
33
34
35
# File 'lib/lightning/bolt.rb', line 27

def initialize(name)
  @name = name
  @globs = []
  @functions = []
  @aliases = {}
  (Lightning.config.bolts[@name] || {}).each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

- (Hash) aliases (readonly)

Maps aliases to full paths. Aliases are valid for a bolt’s functions

Returns:

  • (Hash) — Maps aliases to full paths. Aliases are valid for a bolt’s functions


19
20
21
# File 'lib/lightning/bolt.rb', line 19

def aliases
  @aliases
end

- (Array<Hash, String>) functions

Used to generate functions.

Returns:

  • (Array<Hash, String>) — An array element can be a hash of function attributes or a shell command


26
27
28
# File 'lib/lightning/bolt.rb', line 26

def functions
  @functions
end

- (Boolean) global (readonly)

When true adds global commands to list of commands used to generate a bolt’s functions

Returns:

  • (Boolean) — When true adds global commands to list of commands used to generate a bolt’s functions


21
22
23
# File 'lib/lightning/bolt.rb', line 21

def global
  @global
end

- (Array) globs

User-defined globs that are translated by Dir.glob().

Returns:

  • (Array) — User-defined globs that are translated by Dir.glob().


23
24
25
# File 'lib/lightning/bolt.rb', line 23

def globs
  @globs
end

- (String) name (readonly)

Unique alphanumeric name

Returns:

  • (String) — Unique alphanumeric name


17
18
19
# File 'lib/lightning/bolt.rb', line 17

def name
  @name
end

Instance Method Details

- (Object) function_name(scmd)

Creates function name for a bolt given a shell command



51
52
53
# File 'lib/lightning/bolt.rb', line 51

def function_name(scmd)
  Lightning.config.function_name(scmd, alias_or_name)
end

- (Array) generate_functions

Generates functions from a bolt’s functions and global shell commands

Returns:

  • (Array)


39
40
41
42
43
44
45
46
47
48
# File 'lib/lightning/bolt.rb', line 39

def generate_functions
  @functions += Lightning.config.global_commands if @global
  @functions.inject({}) {|acc, e|
    cmd = e.is_a?(Hash) ? e.dup : {'shell_command'=>e}
    cmd['bolt'] = self
    cmd['name'] ||= function_name(cmd['shell_command'])
    acc[cmd['name']] ||= cmd if cmd['shell_command']
    acc
  }.values
end