Class: Spider::Model::MapperTask

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/model/mappers/mapper.rb

Overview

The MapperTask is used by the UnitOfWork. It represents an action that needs to be done, and allows to specify dependences between tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, action, params = {}) ⇒ MapperTask

Returns a new instance of MapperTask.

Parameters:

  • object (BaseModel)

    The task’s subject

  • action (Symbol)
  • params (Hash) (defaults to: {})


1347
1348
1349
1350
1351
1352
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1347

def initialize(object, action, params={})
    @object = object
    @action = action
    @params = params
    @dependencies = []
end

Instance Attribute Details

#actionSymbol (readonly)

Returns The task’s action.

Returns:

  • (Symbol)

    The task’s action



1340
1341
1342
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1340

def action
  @action
end

#dependenciesArray (readonly)

Returns Array of MapperTasks this one depends on.

Returns:

  • (Array)

    Array of MapperTasks this one depends on



1336
1337
1338
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1336

def dependencies
  @dependencies
end

#objectBaseModel (readonly)

Returns The task’s subject.

Returns:



1338
1339
1340
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1338

def object
  @object
end

#paramsHash (readonly)

Returns Params for the task.

Returns:

  • (Hash)

    Params for the task



1342
1343
1344
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1342

def params
  @params
end

Instance Method Details

#<<(task) ⇒ void

This method returns an undefined value.

Addes a dependency to the Task

Parameters:



1357
1358
1359
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1357

def <<(task)
    @dependencies << task
end

#===(task) ⇒ bool

Returns Same as #eql?.

Returns:

  • (bool)

    Same as #eql?



1386
1387
1388
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1386

def ===(task)
    return eql?(task)
end

#eql?(task) ⇒ bool

Returns True if the other task has the same object, action and params, false otherwise.

Returns:

  • (bool)

    True if the other task has the same object, action and params, false otherwise



1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1371

def eql?(task)
    return false unless task.class == self.class
    return false unless (task.object == self.object && task.action == self.action)
    @params.each do |k, v|
        return false unless task.params[k] == v
    end
    return true
end

#executevoid

This method returns an undefined value.

Makes the objects’ mapper run the task



1363
1364
1365
1366
1367
1368
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1363

def execute
    debug_str = "Executing #{@action} on #{@object.class}(#{@object.primary_keys.inspect})"
    debug_str += " (#{@params.inspect})" unless @params.empty?
    Spider::Logger.debug debug_str
    @object.mapper.execute_action(@action, @object, @params)
end

#hashString

Returns Hash for keying.

Returns:

  • (String)

    Hash for keying



1381
1382
1383
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1381

def hash
    return @object.hash + @action.hash
end

#inspectString

Returns A textual representation of the Task.

Returns:

  • (String)

    A textual representation of the Task



1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/spiderfw/model/mappers/mapper.rb', line 1395

def inspect
    if (@action && @object)
        str = "#{@action} on #{@object}##{@object.object_id} (#{object.class})"
        str += " (#{@params.inspect})" unless @params.empty?
        if (@dependencies.length > 0)
            str += " (dependencies: #{@dependencies.map{ |dep| "#{dep.action} on #{dep.object.class} #{dep.object}##{dep.object.object_id}"}.join(', ')})"
            # str += "-dependencies:\n"
            #                    @dependencies.each do |dep|
            #                        str += "---#{dep.action} on #{dep.object}\n"
            #                    end
        end
    else
        str = "Root Task"
    end
    return str
end