Class: LEWT::LewtOpts

Inherits:
Hash
  • Object
show all
Defined in:
lib/lewtopts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions, library_options = nil) ⇒ LewtOpts

Sets up this extension.



28
29
30
31
32
33
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lewtopts.rb', line 28

def initialize ( extensions, library_options = nil )
  default_options  = {
    :start => {
      :definition => "Start time for LEWT snapshot",
      :default => DateTime.now - 8,
      :type => DateTime,
      :short_flag => "-s"
    },
    :end => {
      :definition => "End time for LEWT snapshot",
      :default => DateTime.now,
      :type => DateTime,
      :short_flag => "-e"
    },
    :target => {
      :definition => "The target to filter data with. In the case of most extensions this will be the target customers but other alternatives are possible",
      :short_flag => "-t",
      :type => String
    },
    :extract => {
      :definition => "The extraction extension(s) LEWT should use to pull data with. This can be a comma separated list for multiple sources",
      :default => "calendar",
      :type => String,
      :short_flag => "-x"
    },
    :process => {
      :definition => "The processor extensions LEWT should use to process the data with.",
      :default => "invoice",
      :type => String,
      :short_flag => "-p"
    },
    :render => {
      :definition => "The render(s) LEWT should use to output the data with. This can be a comma separate list for multiple outputs.",
      :default => "liquid_render",
      :type => String,
      :short_flag => "-o"
    },
    :dump_output => {
      :definition => "Toggle dumping output to console or log",
      :default => true,
      :short_flag => "-d"
    }
  }
  
  # gather extension options & merge into LEWTs defaults
  extensions.each do |e|
    if e.options != nil
      default_options.merge!( e.options )
    end
  end

  @defaults = default_options

  # determine if using LEWT from command line (CL) or as a drop in library and parse options accordingly
  if File.basename($0).match(/\.rb/) != nil
    parse_library_options( default_options, library_options )
  else
    parse_command_line_options( default_options )
  end

end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



25
26
27
# File 'lib/lewtopts.rb', line 25

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/lewtopts.rb', line 25

def options
  @options
end

Instance Method Details

#parse_command_line_options(default_options) ⇒ Object

handles parsing & translating options in command line mode sets the value of the instantized object equal to the parsed options hash

default_options [Hash]

The default options gathered from all extension & Lewt itself to use in case user supplied values aren’t given.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lewtopts.rb', line 93

def parse_command_line_options( default_options )
  options = self
  
  # Parse internal commands before extension commands & options to avoid any conflicts & to avoid extension invocation
  # in case a internal command is called.
  OptionParser.new do |opts|

    default_options.each do | name, details |
      # translate options default value if defined
      if details.key?(:default) == true
        options[name] = details[:default]
      end

      cl_type = details[:type].to_s == "DateTime" ? String : details[:type]
      cl_sub = details[:type].to_s == "DateTime" ? "Date" : details[:type]

      cl_option = "--#{name.to_s.gsub("_","-")} [#{cl_sub}]"

      if details.key?(:short_flag) == true && details.key?(:type) == true
        opts.on( details[:short_flag], cl_option, cl_type, details[:definition] ) do |o|
          options[ name ] = prepare_input( details, o )
        end 
      elsif details.key?(:short_flag) == true && details.key?(:type) == false
        opts.on( details[:short_flag], "--#{name.to_s.gsub("_","-")}", details[:definition] ) do |o|
          options[ name ] = prepare_input( details, o )
        end  
      elsif details.key?(:short_flag) == false 
        opts.on( cl_option, cl_type, details[:definition] ) do |o|
          options[ name ] = prepare_input( details, o )
        end
      end

    end
    
    opts.banner = "Usage: lewt -x EXTRACTOR -p PROCESSOR -o RENDERER"
    opts.version = LEWT::VERSION
    
  end.parse!(ARGV)
  
  return options
end

#parse_library_options(default_options, library_options) ⇒ Object

handles parsing & translating options in library mode sets the value of the instantized object equal to the parsed options hash

default_options [Hash]

The default options gathered from all extension & Lewt itself to use in case user supplied values aren’t given.

library_options [Hash]

Some options translated for use in lib mode



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lewtopts.rb', line 139

def parse_library_options ( default_options, library_options )
  options = Hash.new

  default_options.each do | name, details |
    # translate options default value if defined
    if details.key?(:default) == true
      options[name] = details[:default]
    end
  end

  # merge library options with options array
  options.merge!(library_options)
  
  # assign options to self
  options.each do |k,v|
    self[k] = v
  end
end

#prepare_input(details, value) ⇒ Object

this function basically exists to get around the limited variable initialization functionality of Ruby standard option parser class.



160
161
162
163
164
165
166
167
168
# File 'lib/lewtopts.rb', line 160

def prepare_input ( details, value )
  type = details[:type]
  if type == DateTime
    initialized_value = DateTime.parse(value)
  else
    initialized_value = value
  end
  return initialized_value
end