Class: Locator

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/locator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_empty(wizard) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/locator.rb', line 39

def self.first_empty(wizard)
  return unless wizard

  locs = where(wizard_id: wizard.id, item_id: nil)
  if !locs.empty?
    locs.first
  else
    m = Locator.largest wizard
    loc = Locator.new(wizard_id: wizard.id, number: m ? m.number + 1 : 0)
    loc.save
    loc
  end
end

.largest(wizard) ⇒ Object



57
58
59
60
# File 'app/models/locator.rb', line 57

def self.largest(wizard)
  # find greatest locator for this wizard, should always be the most recent
  wizard.locators.order('id desc').first
end

.port(wizard) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/locator.rb', line 62

def self.port(wizard)

  # e.g. Locator.port Wizard.find_by_name("M20")

  ots = ObjectType.where(prefix: wizard.name)
  items = ots.collect(&:items).flatten.select { |i| wizard.has_correct_form i.primitive_location }
  maxitem = items.max_by { |i| puts i.id; wizard.location_to_int(i.primitive_location) }
  max = wizard.location_to_int maxitem.primitive_location

  collisions = []

  # insert block of new locators
  (0..max).each do |n|
    Locator.new(
      number: n,
      wizard_id: wizard.id
    ).save
  end

  # insert locators
  items.each do |i|

    n = wizard.location_to_int(i.primitive_location)
    l = Locator.find_by(wizard_id: wizard.id, number: n)

    if l.item_id
      collisions.push([l.item_id, i.id])
    else
      l.item_id = i.id
      l.save
      i.locator_id = l.id
      i.save
    end

  end

  collisions

end

.port_allObject



102
103
104
105
106
107
108
109
# File 'app/models/locator.rb', line 102

def self.port_all

  port Wizard.find_by(name: 'M20')
  port Wizard.find_by(name: 'M80')
  port Wizard.find_by(name: 'SF2')
  port Wizard.find_by(name: 'DFP')

end

Instance Method Details

#clearObject



111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/locator.rb', line 111

def clear
  r1 = false
  r2 = false
  transaction do
    item.locator_id = nil
    r1 = item.save
    self.item_id = nil
    r2 = save
  end
  r1 && r2
end

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/locator.rb', line 53

def empty?
  item_id.nil?
end

#has_wizardObject



16
17
18
19
# File 'app/models/locator.rb', line 16

def has_wizard
  errors.add(:no_wizard, 'no wizard') unless
    wizard_id && wizard_id >= 0
end

#no_collisionsObject



21
22
23
24
25
26
27
28
29
# File 'app/models/locator.rb', line 21

def no_collisions
  puts "Checking for collisions for #{id}"
  c = Locator.where(wizard_id: wizard_id, number: number)
  if c.length == 1 && c.first != self
    errors.add(:locator_collision, "Locator #{c.first.id} already has number #{number}.")
  elsif c.length > 1
    errors.add(:locator_collision, "Multiple Locators have number #{number}.")
  end
end

#to_sObject



31
32
33
34
35
36
37
# File 'app/models/locator.rb', line 31

def to_s
  if wizard
    wizard.int_to_location number
  else
    'ERROR'
  end
end