Class: Jobshop::Inspection::DeviationCriterion

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/jobshop/inspection/deviation_criterion.rb

Instance Method Summary collapse

Instance Method Details

#criterion_with_buildObject Also known as: criterion



16
17
18
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 16

def criterion_with_build
  criterion_without_build || build_criterion
end

#lowerObject



36
37
38
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 36

def lower
  self[:lower] && Unitwise(self[:lower].truncate(4), self[:unit])
end

#lower=(value) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 40

def lower=(value)
  self[:lower] = if value.respond_to?(:unit)
    value.convert_to(self[:unit]).value
  else
     value
  end
end

#lower_less_than_upperObject



60
61
62
63
64
65
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 60

def lower_less_than_upper
  return unless lower && upper
  if lower > upper
    errors[:base] << "minimum must be less than maximum"
  end
end

#meanObject



28
29
30
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 28

def mean
  ((nominal + lower) + (nominal + upper)) / 2
end

#nominalObject



32
33
34
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 32

def nominal
  self[:nominal] && Unitwise(self[:nominal].truncate(4), self[:unit])
end

#pass?(value) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 79

def pass?(value)
  !undersize?(value) && !oversize?(value)
end

#randomObject

Generate a random value that has a 90% chance of being in spec. TODO: This needs to go in the tests somewhere, not really in the model.



85
86
87
88
89
90
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 85

def random
  rand_lower = nominal.to_f + lower.to_f * 1.1
  rand_upper = nominal.to_f + upper.to_f * 1.1
  value = rand(rand_lower..rand_upper)
  Unitwise(value.truncate(4), self[:unit])
end

#specificationObject



67
68
69
70
71
72
73
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 67

def specification
  @specfication ||= if symmetric?
    "#{nominal}±#{abs(upper)}"
  else
    "#{nominal}\n#{lower}/#{upper}"
  end
end

#symmetric?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 75

def symmetric?
  @symmetrical ||= lower == upper * -1
end

#upperObject



48
49
50
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 48

def upper
  self[:upper] && Unitwise(self[:upper].truncate(4), self[:unit])
end

#upper=(value) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/jobshop/inspection/deviation_criterion.rb', line 52

def upper=(value)
  self[:upper] = if value.respond_to?(:unit)
    value.convert_to(self[:unit]).value
  else
    value
  end
end