Class: Reap::Task

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/reap/task.rb

Overview

Task base class.

Constant Summary collapse

RUBY =

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Task

Returns a new instance of Task.



187
188
189
190
# File 'lib/reap/task.rb', line 187

def initialize( *args )
  #@master = CascadingOpenObject.new( $PROJECT_INFO )
  @args = args
end

Class Method Details

.available?Boolean

Is the task available for use? By default a task is only made available if the ProjectInfo is present and the task’s section is present.

Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/reap/task.rb', line 152

def available?
  return false unless Reap.projectfile?
  #if section_required?
    return has_section?
  #end
  #true
end

.has_section?Boolean

Project file exists and has task’s section?

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/reap/task.rb', line 161

def has_section?
  return false unless Reap.projectfile?
  ProjectInfo.instance.info.key?( task_name )
end

.inherited(base) ⇒ Object

When this class is inherited the new task is registered by adding it to the task_list hash.



84
85
86
# File 'lib/reap/task.rb', line 84

def inherited( base )
  task_list[base.task_name] = base
end

.masterObject

Class-level access to Project file’s master data provided via a CascadingOpenObject.



168
169
170
# File 'lib/reap/task.rb', line 168

def master
  @master ||= ProjectInfo.instance.to_cascading_open_object
end

.task_attr(name) ⇒ Object

Set alias for ‘task’ attribute, which provides inherited (from master) access to section data.



121
122
123
# File 'lib/reap/task.rb', line 121

def task_attr( name )
  define_method(name) { @task }
end

.task_available(bool = false, &block) ⇒ Object

How to determine if a task is available for use. By default a taks is only available if the Project file exists and the task’s section also exists. Common was to loosen this resriction are to only require the file, not the seciton:

task_available { Reap.projectfile? }

Or have no restrictions:

task_available true


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/reap/task.rb', line 137

def task_available( bool=false, &block )
  if bool
    (class << self; self; end).class_eval {
      define_method( :available? ){ true }
    }
  else
    (class << self; self; end).class_eval {
      define_method( :available?, &block )
    }
  end
end

.task_desc(text = nil, &block) ⇒ Object

Short one-line description of the task.



105
106
107
108
109
# File 'lib/reap/task.rb', line 105

def task_desc( text=nil, &block )
  return @task_desc = proc { text } if text
  return @task_desc = block if block_given?
  return @task_desc.call
end

.task_help(text = nil, &block) ⇒ Object

Takes a string or a block the evaluates to a string providing detailed help information about the task.



113
114
115
116
117
# File 'lib/reap/task.rb', line 113

def task_help( text=nil, &block )
  return @task_help = proc { text } if text
  return @task_help = block if block_given?
  return @task_help.call
end

.task_listObject

Task list hash.



89
90
91
# File 'lib/reap/task.rb', line 89

def task_list
  @task_list ||= {}
end

.task_nameObject

Provided the tasks name. This defualt’s the task’s class’ name downcased, so it is rarely needed.



100
101
102
# File 'lib/reap/task.rb', line 100

def task_name
  basename.downcase
end

Instance Method Details

#ask(question, answers = nil) ⇒ Object



258
259
260
261
262
263
# File 'lib/reap/task.rb', line 258

def ask( question, answers=nil )
  print "#{question}"
  print " [#{answers}] " if answers
  until inp = $stdin.gets[0,1] ; sleep 1 ; end ; puts
  inp
end

#execute(section = nil) ⇒ Object

Run task for each section entires given.



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/reap/task.rb', line 194

def execute( section=nil )
  section = section || master[task_name]
  case section
  when Array
    section.each do |s|
      initiate( s )
    end
  else
    initiate( section )
  end
end

#initiate(section) ⇒ Object

Per section entry execution of task.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/reap/task.rb', line 208

def initiate( section )
  @section = CascadingOpenObject.new( section )

  task_properties = {}  # needed?
  #self.class.task_only_properties.each { |t| section[t] ||= nil }
  task_properties = CascadingOpenObject.new( section )
  task_properties.__parent__ = master
  @task = task_properties

  # deprecate init ?
  if respond_to?( :init )
    if method(:init).arity == 0
      init
    else
      init( *@args )
    end
  end

  if method(:run).arity == 0
    run
  else
    run( *@args )
  end
end

#masterObject

def master ; ::ProjectInfo.info ; end



183
# File 'lib/reap/task.rb', line 183

def master  ; self.class.master  ; end

#provide_setup_rbObject



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/reap/task.rb', line 269

def provide_setup_rb
  return true if File.exists?( 'setup.rb')
  # copy from data dir to current directory (Won't work with Gem!)
  f = File.join( Config::CONFIG['datadir'], 'reap', 'setup_rb', 'setup.rb' )
  if File.exists?(f)
    File.cp( f, '.' )
    true
  else
    nil
  end
end

#runObject

def init

raise "not implemented for '#{task_name}' task"

end



239
240
241
# File 'lib/reap/task.rb', line 239

def run
  raise "no action defined for task #{task_name}"
end

#sectionObject



184
# File 'lib/reap/task.rb', line 184

def section ; @section ; end

#sh(arg) ⇒ Object



253
254
255
256
# File 'lib/reap/task.rb', line 253

def sh( arg )
  puts arg
  system arg unless $PRETEND
end

#taskObject



185
# File 'lib/reap/task.rb', line 185

def task    ; @task    ; end

#task_descObject



177
# File 'lib/reap/task.rb', line 177

def task_desc ; self.class.task_desc ; end

#task_helpObject



178
# File 'lib/reap/task.rb', line 178

def task_help ; self.class.task_help ; end

#task_nameObject

instance methods



176
# File 'lib/reap/task.rb', line 176

def task_name ; self.class.task_name ; end

#tell(statement) ⇒ Object



265
266
267
# File 'lib/reap/task.rb', line 265

def tell( statement )
  puts statement
end

#use_subsection(name) ⇒ Object

Task support methods



245
246
247
248
249
250
251
# File 'lib/reap/task.rb', line 245

def use_subsection( name )
  subsection = @section.__send__(name)
  if subsection
    subsection.__parent__ = @section
    @task = subsection
  end
end