Class: Laser::Cutter::Configuration

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/laser-cutter/configuration.rb

Constant Summary collapse

DEFAULTS =
{
    units: 'in',
    page_layout: 'portrait',
    metadata: true
}
UNIT_SPECIFIC_DEFAULTS =
{
    'in' => {
        kerf: 0.0024, # smallest kerf for thin material, usually it's more than that.
        margin: 0.125,
        padding: 0.1,
        stroke: 0.001,
    }
}
SIZE_REGEXP =
/[\d\.]+x[\d\.]+x[\d\.]+\/[\d\.]+(\/[\d\.]+)?/
FLOATS =
%w(width height depth thickness notch margin padding stroke kerf)
NON_ZERO =
%w(width height depth thickness stroke)
REQUIRED =
%w(width height depth thickness notch file)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/laser-cutter/configuration.rb', line 43

def initialize(options = {})
  options.delete_if { |k, v| v.nil? }
  if options['units'] && !UNIT_SPECIFIC_DEFAULTS.keys.include?(options['units'])
    options.delete('units')
  end
  self.merge!(DEFAULTS)
  self.merge!(options)
  if self['size'] && self['size'] =~ SIZE_REGEXP
    dim, self['thickness'], self['notch'] = self['size'].split('/')
    self['width'], self['height'], self['depth'] = dim.split('x')
    delete('size')
  end
  FLOATS.each do |k|
    self[k] = self[k].to_f if (self[k] && self[k].is_a?(String))
  end
  self.merge!(UNIT_SPECIFIC_DEFAULTS[self['units']].merge(self))
  self['notch'] = (self['thickness'] * 3.0).round(5) if self['thickness'] && self['notch'].nil?
end

Instance Method Details

#change_units(new_units) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/laser-cutter/configuration.rb', line 72

def change_units(new_units)
  return if (self.units.eql?(new_units) || !UNIT_SPECIFIC_DEFAULTS.keys.include?(new_units))
  k = (self.units == 'in') ? UnitsConverter.in2mm(1.0) : UnitsConverter.mm2in(1.0)
  FLOATS.each do |field|
    next if self.send(field.to_sym).nil?
    self.send("#{field}=".to_sym, (self.send(field.to_sym) * k).round(5))
  end
  self.units = new_units
end

#validate!Object

Raises:



62
63
64
65
66
67
68
69
70
# File 'lib/laser-cutter/configuration.rb', line 62

def validate!
  missing = []
  REQUIRED.each { |k| missing << k if self[k].nil? }
  raise MissingOption.new("#{missing.join(', ')} #{missing.size > 1 ? 'are' : 'is'} required, but missing.") unless missing.empty?

  zeros = []
  NON_ZERO.each { |k| zeros << k if self[k] == 0 }
  raise ZeroValueNotAllowed.new("#{zeros.join(', ')} #{zeros.size > 1 ? 'are' : 'is'} required, but is zero.") unless zeros.empty?
end