Class: LEWT::Lewt
- Inherits:
-
Object
- Object
- LEWT::Lewt
- Defined in:
- lib/lewt.rb
Overview
The Lewt class contains the major functionality of this program. It handles loading all extensions, gathering the results and passing options ariound the place. It also works quite closely with the LewtExtension and LewtOpts classes.
Constant Summary collapse
- OPTION_DELIMITER_REGEX =
Can be used to split strings passed to lewt through CL optios ie:
"acme,wayne_corp".split(CLIENT_SPLIT_REGEX) # get each argument for client mathching
/[,+:]/
- OPTION_SYMBOL_REGEX =
This matches symbols passed thought command ie: -p meta-stat => meta_stat
/\W/
Instance Method Summary collapse
-
#fire_hooks(hook, options, *data) ⇒ Object
Fire a hook with the given options and overloaded parameters.
-
#get_client(query) ⇒ Object
fn returns the desired customer given there name or alias.
-
#hook_process(hook, data) ⇒ Object
Fire the logic loop from the specified hook onwards.
-
#initialize(library_options = nil) ⇒ Lewt
constructor
A new instance of Lewt.
-
#run ⇒ Object
Runs extract, process, render hooks.
Constructor Details
#initialize(library_options = nil) ⇒ Lewt
Returns a new instance of Lewt.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lewt.rb', line 34 def initialize( = nil ) core_settings = YAML.load_file( File.( '../config/settings.yml', __FILE__) ) if File.exists? File.( '~/.lewt_settings', __FILE__) core_settings.merge! YAML.load_file( File.( '~/.lewt_settings', __FILE__) ) end @lewt_stash = core_settings['lewt_stash'] || File.('../', __FILE__) + "/config/" @settings = YAML.load_file( @lewt_stash + 'settings.yml' ) # Start by loading the local config files @customers = YAML.load_file(@lewt_stash + "customers.yml") @enterprise = YAML.load_file(@lewt_stash + "enterprise.yml") # Referenceall registered extension for later invocation @extensions = LEWT::Extension.new.lewt_extensions # Load core extensions load_extensions( File.('../extensions', __FILE__) ) if core_settings.has_key?("lewt_stash") # load user defined extesnions load_extensions end # Stores default options returned from extensions # and the LEWT defaults at large = {} @command = ARGV[0] # argument supplied for `cmd' (if any). ignore option flags IE args that start with the `-' symbol @argument = ARGV[1] == nil || ARGV[1].match(/\-/i) ? nil : ARGV[1] @options = LewtOpts.new( @extensions, ) parse_internal_commands end |
Instance Method Details
#fire_hooks(hook, options, *data) ⇒ Object
Fire a hook with the given options and overloaded parameters.
- hook [String]
-
Expected hooks are ‘extract’, ‘process’, ‘render’.
- options [Hash]
-
The options gathered from using LewtOpts
- data [Mixed]
-
The data to pass to the extensions.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/lewt.rb', line 119 def fire_hooks( hook, , *data ) algamation = Array.new @extensions.each { |e| ## filter hooks filter_regex = /#{[hook.to_sym].gsub(",","|")}/ if e.methods.include?(hook.to_sym) and e.command_name.match(filter_regex) case hook when "extract" algamation.concat e.extract() when "process" processed = e.process(, *data) if processed.kind_of? Array algamation.concat processed else algamation.push processed end when "render" algamation.concat e.render(, *data) # dump render output to console of option specified. puts algamation if [:dump_output] == true end end } return algamation; end |
#get_client(query) ⇒ Object
fn returns the desired customer given there name or alias. A REGEXP query can be passed to this object i.e. “ACME|NovaCorp|…” to match multiple customers.
- query [String]
-
The query to search against.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lewt.rb', line 84 def get_client( query ) client = nil @customers.each do |c| buildQ = [ c["name"], c["alias"] ].join("|") regex = Regexp.new(buildQ, Regexp::IGNORECASE) if regex.match( query ) != nil client = c end end return client end |
#hook_process(hook, data) ⇒ Object
Fire the logic loop from the specified hook onwards. Used when piping data in from CL
- hook [Sting]
-
extract, process, or render
- data
-
The data to pass to fire_hooks
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lewt.rb', line 100 def hook_process (hook, data) case hook when "extract" extracted_data = fire_hooks("extract",@options,data) hook_process("process", extracted_data) when "process" processed_data = fire_hooks("process",@options,data) hook_process("render",processed_data) when "render" render_data = fire_hooks("render",@options,data) else raise ArugmentError, "#{self.class.name}.hook_process requires the start hook to be either render or process" end end |
#run ⇒ Object
Runs extract, process, render hooks.
73 74 75 76 77 |
# File 'lib/lewt.rb', line 73 def run extract = fire_hooks("extract", @options) process = fire_hooks("process", @options, extract ) render = fire_hooks("render", @options, process ) end |