Module: Main::Program::ClassMethods

Included in:
Main::Program
Defined in:
lib/main/program/class_methods.rb

Defined Under Namespace

Classes: Factory

Instance Method Summary collapse

Instance Method Details

#argument(*a, &b) ⇒ Object



183
184
185
# File 'lib/main/program/class_methods.rb', line 183

def argument(*a, &b)
  (parameters << Parameter.create(:argument, self, *a, &b)).last
end

#can_has(ptype, *a, &b) ⇒ Object



210
211
212
213
214
# File 'lib/main/program/class_methods.rb', line 210

def can_has(ptype, *a, &b)
  key = a.map{|s| s.to_s}.sort_by{|s| -s.size }.first
  can_has_hash.update key => [ptype, a, b]
  key
end

#chunknameObject

TODO - for some reason these hork the usage!



241
242
243
244
245
246
247
248
# File 'lib/main/program/class_methods.rb', line 241

%w[ examples samples api ].each do |chunkname|
  module_eval <<-code
    def #{ chunkname } *a, &b 
      txt = b ? b.call : a.join("\\n")
      usage['#{ chunkname }'] = txt
    end
  code
end

#default_options!Object



199
200
201
# File 'lib/main/program/class_methods.rb', line 199

def default_options!
  option 'help', 'h' unless parameters.has_option?('help', 'h')
end

#dynamically_extend_via_commandline_modes!Object

extend the class based on modules given in argv



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
# File 'lib/main/program/class_methods.rb', line 96

def dynamically_extend_via_commandline_modes!
  size = modes.size
  depth_first_modes = Array.fields

  loop do
    modes.each do |mode|
      arg = argv.first && %r/^#{ argv.first }/
      if arg and mode.name =~ arg
        argv.shift
        modes.clear
        evaluate(&mode)
        depth_first_modes[mode.name] = mode
        break
      end
    end

    arg = argv.first && %r/^#{ argv.first }/
    more_modes = (
      !modes.empty? and modes.any?{|mode| arg && mode.name =~ arg}
    )

    break unless more_modes
  end

  self.modes = depth_first_modes
end

#environment(*a, &b) ⇒ Object



195
196
197
# File 'lib/main/program/class_methods.rb', line 195

def environment(*a, &b)
  (parameters << Parameter.create(:environment, self, *a, &b)).last
end

#evaluate(&block) ⇒ Object



84
85
86
# File 'lib/main/program/class_methods.rb', line 84

def evaluate(&block)
  module_eval(&block)
end

#factory(&block) ⇒ Object Also known as: create



31
32
33
# File 'lib/main/program/class_methods.rb', line 31

def factory(&block)
  Factory.new(&block)
end

#fully_qualified_modeObject

TODO



161
162
163
# File 'lib/main/program/class_methods.rb', line 161

def fully_qualified_mode
  modes.map{|mode| mode.name}.join(' ')
end

#has(key, *keys) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/main/program/class_methods.rb', line 216

def has(key, *keys)
  keys = [key, *keys].flatten.compact.map{|k| k.to_s}
  keys.map do |key|
    ptype, a, b = can_has_hash[key]
    abort "yo - can *not* has #{ key.inspect }!?" unless(ptype and a and b)
    send ptype, *a, &b
    key
  end
end

#keyword(*a, &b) ⇒ Object



191
192
193
# File 'lib/main/program/class_methods.rb', line 191

def keyword(*a, &b)
  (parameters << Parameter.create(:keyword, self, *a, &b)).last
end

#mixin(name, *names, &block) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/main/program/class_methods.rb', line 226

def mixin(name, *names, &block)
  names = [name, *names].flatten.compact.map{|name| name.to_s}
  if block
    names.each do |name|
      mixin_table[name] = block
    end
  else
    names.each do |name|
      module_eval(&mixin_table[name])
    end
  end
end

#mode(name, &block) ⇒ Object



203
204
205
206
207
208
# File 'lib/main/program/class_methods.rb', line 203

def mode(name, &block)
  name = name.to_s
  modes[name] = block
  block.fattr(:name => name)
  block
end

#mode_nameObject



165
166
167
168
# File 'lib/main/program/class_methods.rb', line 165

def mode_name
  return 'main' if modes.empty?
  fully_qualified_mode
end

#newObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/main/program/class_methods.rb', line 71

def new()
  instance = allocate
  instance.instance_eval do
    pre_initialize()
    before_initialize()
    main_initialize()
    initialize()
    after_initialize()
    post_initialize()
  end
  instance
end

#option(*a, &b) ⇒ Object



187
188
189
# File 'lib/main/program/class_methods.rb', line 187

def option(*a, &b)
  (parameters << Parameter.create(:option, self, *a, &b)).last
end

#parameter(*a, &b) ⇒ Object



179
180
181
# File 'lib/main/program/class_methods.rb', line 179

def parameter(*a, &b)
  (parameters << Parameter.create(:parameter, self, *a, &b)).last
end

#run(&block) ⇒ Object



252
253
254
255
# File 'lib/main/program/class_methods.rb', line 252

def run(&block)
  block ||= lambda{}
  define_method(:run, &block) if block
end

#set_default_options!Object



88
89
90
# File 'lib/main/program/class_methods.rb', line 88

def set_default_options!
  option('help', 'h') unless parameters.has_option?('help', 'h')
end

#usage(*args, &block) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/main/program/class_methods.rb', line 171

def usage(*args, &block)
  usage! unless defined? @usage 
  return @usage if args.empty? and block.nil?
  key, value, *ignored = args
  value = block.call if block
  @usage[key.to_s] = value.to_s
end

#wrap_run!Object

wrap up users run method to handle errors, etc



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/main/program/class_methods.rb', line 125

def wrap_run!
  evaluate do
    alias_method 'run!', 'run'

    def run()
      exit_status =
        catch :exit do
          begin
            parse_parameters

            if help?
              puts(usage)
              exit
            end

            pre_run
            before_run
            run!
            after_run
            post_run

            finalize
          rescue Object => exception
            self.exit_status ||= exception.status if exception.respond_to?(:status)
            handle_exception(exception)
          end
          nil
        end

      self.exit_status ||= (exit_status || exit_success)
      handle_exit(self.exit_status)
    end
  end
end