Class: LEWT::Lewt

Inherits:
Object
  • Object
show all
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

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( library_options = nil )
  core_settings = YAML.load_file( File.expand_path( '../config/settings.yml', __FILE__) )
  if File.exists? File.expand_path( '~/.lewt_settings', __FILE__)
    core_settings.merge! YAML.load_file( File.expand_path( '~/.lewt_settings', __FILE__) )
  end

  @lewt_stash = core_settings['lewt_stash'] || File.expand_path('../', __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.expand_path('../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
  options = {}
  @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, library_options  )
  
  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, options, *data )
  algamation = Array.new
  @extensions.each { |e|
    ## filter hooks
    filter_regex = /#{options[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(options)
      when "process"
        processed = e.process(options, *data)
        if processed.kind_of? Array 
          algamation.concat processed
        else
          algamation.push processed
        end
      when "render"
        algamation.concat e.render(options, *data)
        # dump render output to console of option specified.
        puts algamation if options[: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

#runObject

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