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



209
210
211
# File 'lib/main/program/class_methods.rb', line 209

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

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



237
238
239
240
241
# File 'lib/main/program/class_methods.rb', line 237

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!



268
269
270
271
272
273
274
275
# File 'lib/main/program/class_methods.rb', line 268

%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

#config(*args, &block) ⇒ Object Also known as: edit_config_file!



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/main/program/class_methods.rb', line 366

def config(*args, &block)
  unless defined?(@config)
    require 'yaml' unless defined?(YAML)
    if test(?s, config_path)
      @config = Map.for(YAML.load(IO.read(config_path)))
    else
      config = args.last
      lines =
        case config
          when Hash
            config.to_yaml.split(/\n/)
          when String
            Util.unindent(config).split(/\n/)
          else
            []
        end
      lines.shift if lines.first.to_s =~ /^---/
      require 'fileutils' unless defined?(FileUtils)
      FileUtils.mkdir_p(File.dirname(config_path))
      open(config_path, 'w') do |fd|
        fd.puts "## file: #{ config_path }"
        fd.puts "#"
        fd.puts
        fd.puts lines
      end
      editor = ENV['EDITOR'] || ENV['EDIT'] || 'vi'
      system("#{ editor.inspect } #{ config_path }")
      @config = Map.for(YAML.load(IO.read(config_path)))
    end
  end
  @config
end

#config_path(*config_path) ⇒ Object



400
401
402
403
404
# File 'lib/main/program/class_methods.rb', line 400

def config_path(*config_path)
  @config_path = File.join(state_path, 'config.yml') unless defined?(@config_path)
  @config_path = File.join(*config_path) unless config_path.empty?
  @config_path
end

#daemonizes!(*args) ⇒ Object



428
429
430
431
432
433
434
435
436
# File 'lib/main/program/class_methods.rb', line 428

def daemonizes!(*args)
  mode(:daemon){
    run {
      cmd = argv.shift || :usage

      daemon.cmd(cmd)
    }
  }
end

#db(*args, &block) ⇒ Object



350
351
352
353
354
355
356
357
358
# File 'lib/main/program/class_methods.rb', line 350

def db(*args, &block)
  unless defined?(@db)
    require 'sequel' unless defined?(Sequel)
    require 'amalgalite' unless defined?(Amalgalite)
    @db = state_path{ Sequel.amalgalite(db_path) }
    @db.instance_eval(&block) if block
  end
  @db
end

#db_path(*db_path) ⇒ Object



360
361
362
363
364
# File 'lib/main/program/class_methods.rb', line 360

def db_path(*db_path)
  @db_path = File.join(state_path, 'db.sqlite') unless defined?(@db_path)
  @db_path = File.join(*db_path) unless db_path.empty?
  @db_path
end

#default_options!Object



225
226
227
# File 'lib/main/program/class_methods.rb', line 225

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/main/program/class_methods.rb', line 121

def dynamically_extend_via_commandline_modes!
  self.breadth_first_modes = modes.dup

  loop do
    modes.each do |mode|
      arg = argv.first && %r/^#{ argv.first }/
      if arg and mode.name =~ arg
        argv.shift
        modes.clear()
        breadth_first_modes.clear()
        evaluate(&mode)
        self.breadth_first_modes = modes.dup
        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.dup
end

#environment(*a, &b) ⇒ Object



221
222
223
# File 'lib/main/program/class_methods.rb', line 221

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

#evaluate(&block) ⇒ Object



109
110
111
# File 'lib/main/program/class_methods.rb', line 109

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

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



34
35
36
# File 'lib/main/program/class_methods.rb', line 34

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

#factory=(factory) ⇒ Object



39
40
41
# File 'lib/main/program/class_methods.rb', line 39

def factory=(factory)
  @factory = factory
end

#fully_qualified_modeObject

TODO



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

def fully_qualified_mode
  modes.map{|mode| mode.name}
end

#has(key, *keys) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/main/program/class_methods.rb', line 243

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

#input(*args, &block) ⇒ Object



406
407
408
409
410
411
412
# File 'lib/main/program/class_methods.rb', line 406

def input(*args, &block)
  first = args.first
  args.push(:input) unless(first.is_a?(Symbol) or first.is_a?(String))
  param = argument(*args, &block)
  param.cast(:input)
  param
end

#io(*args) ⇒ Object



422
423
424
425
426
# File 'lib/main/program/class_methods.rb', line 422

def io(*args)
  i = input(*[args.shift].compact).default('-')
  o = output(*[args.shift].compact).default('-')
  [i, o]
end

#keyword(*a, &b) ⇒ Object



217
218
219
# File 'lib/main/program/class_methods.rb', line 217

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

#main_env(*args, &block) ⇒ Object



284
285
286
287
288
289
290
291
292
293
# File 'lib/main/program/class_methods.rb', line 284

def main_env(*args, &block)
  @main_env ||= main_env! 

  if args.empty?
    @main_env
  else
    key = main_env_key_for(args.first)
    @main_env[key]
  end
end

#main_env!Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/main/program/class_methods.rb', line 299

def main_env!
  @main_env = Map.new

  @main_env[:state]          = env['STATE']
  @main_env[:state_dirname]  = env['STATE_DIRNAME']
  @main_env[:state_basename] = env['STATE_BASENAME']

  env.each do |key, val|
    next unless key.to_s =~ /^\s*MAIN_/i
    k = main_env_key_for(key)
    @main_env[k] = val
  end

  @main_env
end

#main_env_key_for(key) ⇒ Object



295
296
297
# File 'lib/main/program/class_methods.rb', line 295

def main_env_key_for(key)
  key.to_s.strip.downcase.sub(/^main_/, '')
end

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



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/main/program/class_methods.rb', line 253

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



229
230
231
232
233
234
235
# File 'lib/main/program/class_methods.rb', line 229

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

#mode_nameObject



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

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

#newObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/main/program/class_methods.rb', line 80

def new()
  instance = allocate

  setup_finalizers(instance)

  instance.instance_eval do
    pre_initialize()
    before_initialize()
    main_initialize()
    initialize()
    after_initialize()
    post_initialize()
  end

  instance
end

#option(*a, &b) ⇒ Object



213
214
215
# File 'lib/main/program/class_methods.rb', line 213

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

#output(*args, &block) ⇒ Object



414
415
416
417
418
419
420
# File 'lib/main/program/class_methods.rb', line 414

def output(*args, &block)
  first = args.first
  args.push(:output) unless(first.is_a?(Symbol) or first.is_a?(String))
  param = argument(*args, &block)
  param.cast(:output)
  param
end

#parameter(*a, &b) ⇒ Object



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

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

#paramsObject



105
106
107
# File 'lib/main/program/class_methods.rb', line 105

def params
  parameters
end

#run(&block) ⇒ Object



279
280
281
282
# File 'lib/main/program/class_methods.rb', line 279

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

#set_default_options!Object



113
114
115
# File 'lib/main/program/class_methods.rb', line 113

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

#setup_finalizers(instance) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/main/program/class_methods.rb', line 97

def setup_finalizers(instance)
  instance.finalizers = (finalizers = [])

  ObjectSpace.define_finalizer(instance) do
    while((f = finalizers.pop)); f.call; end
  end
end

#state_basename(*args) ⇒ Object



322
323
324
325
326
327
# File 'lib/main/program/class_methods.rb', line 322

def state_basename(*args)
  @state_basename = File.join(*args) unless args.empty? 
  @state_basename ||= main_env('STATE_BASENAME')
  @state_basename ||= ".#{ name }"
  @state_basename
end

#state_dirname(*args) ⇒ Object



315
316
317
318
319
320
# File 'lib/main/program/class_methods.rb', line 315

def state_dirname(*args)
  @state_dirname = File.join(*args) unless args.empty? 
  @state_dirname ||= main_env('STATE_DIRNAME')
  @state_dirname ||= Util.home
  @state_dirname
end

#state_path(*state_path, &block) ⇒ Object Also known as: dotdir



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/main/program/class_methods.rb', line 329

def state_path(*state_path, &block)
  unless defined?(@state_path)
    if main_env('STATE')
      @state_path = File.expand_path(main_env('STATE'))
      @state_dirname = File.dirname(@state_path)
      @state_basename = File.basename(@state_path)
    else
      @state_path = File.join(state_dirname, state_basename)
    end
  end

  if block
    require 'fileutils' unless defined?(FileUtils)
    FileUtils.mkdir_p(@state_path) unless test(?d, @state_path)
    Dir.chdir(@state_path, &block)
  else
    @state_path
  end
end

#usage(*args, &block) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/main/program/class_methods.rb', line 197

def usage(*args, &block)
  usage! unless defined? @usage 
  return @usage if args.empty? and block.nil?
  key, value, * = 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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/main/program/class_methods.rb', line 151

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

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

            if help?
              puts(usage.to_s)
              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