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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Bolt

Returns 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 = {}
  config = Lightning.config.bolts[@name] || {}
  config.each { |k,v| instance_variable_set("@#{k}", v) }
  @globs = Generator.run(@name, :live => true) unless config.key?('globs')
end

Instance Attribute Details

#aliasesHash (readonly)

Returns 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

#functionsArray<Hash, String>

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

#globalBoolean (readonly)

Returns 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

#globsArray

Returns 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

#nameString (readonly)

Returns 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

#function_name(scmd) ⇒ Object

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

#generate_functionsArray

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