Class: Coupler::Models::Scenario

Inherits:
Sequel::Model
  • Object
show all
Includes:
CommonModel, Jobify
Defined in:
lib/coupler/models/scenario.rb,
lib/coupler/models/scenario/runner.rb

Defined Under Namespace

Classes: NoMatcherError, ResourcesOutOfDateError, Runner

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jobify

included

Methods included from CommonModel

#after_destroy, #after_save, #before_save, #before_update, included, #save!, #touch!

Instance Attribute Details

#resource_ids=(value) ⇒ Object (writeonly)

Sets the attribute resource_ids

Parameters:

  • value

    the value to set the attribute resource_ids to.



10
11
12
# File 'lib/coupler/models/scenario.rb', line 10

def resource_ids=(value)
  @resource_ids = value
end

Class Method Details

.count_by_projectObject



17
18
19
# File 'lib/coupler/models/scenario.rb', line 17

def self.count_by_project
  dataset.naked.group_and_count(:project_id).to_hash(:project_id, :count)
end

Instance Method Details

#local_database(&block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/coupler/models/scenario.rb', line 39

def local_database(&block)
  Sequel.connect(local_connection_string, {
    :loggers => [Coupler::Logger.instance],
    :max_connections => 50,
    :pool_timeout => 60
  }, &block)
end

#resourcesObject



31
32
33
34
35
36
37
# File 'lib/coupler/models/scenario.rb', line 31

def resources
  if resource_1
    resource_2 ? [resource_1, resource_2] : [resource_1]
  else
    []
  end
end

#run!(&progress) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/coupler/models/scenario.rb', line 47

def run!(&progress)
  case status
  when 'no_matcher'             then raise NoMatcherError
  when 'resources_out_of_date'  then raise ResourcesOutOfDateError
  end

  runner = Runner.new(self, &progress)
  runner.run!

  update(:run_count => run_count + 1, :last_run_at => Time.now)
  result = Result.new(:scenario => self, :run_number => run_count)
  result.save
end

#set_linkage_typeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coupler/models/scenario.rb', line 61

def set_linkage_type
  if resource_1
    if resource_2
      self.linkage_type = "dual-linkage"
    elsif matcher && matcher.cross_match?
      self.linkage_type = "cross-linkage"
    else
      self.linkage_type = "self-linkage"
    end
  else
    self.linkage_type = "N/A"
  end
end

#statusObject



21
22
23
24
25
26
27
28
29
# File 'lib/coupler/models/scenario.rb', line 21

def status
  if matcher.nil?
    "no_matcher"
  elsif resources.any? { |r| r.status == "out_of_date" }
    "resources_out_of_date"
  else
    "ok"
  end
end