Class: Laser::Cutter::Configuration

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

Overview

The settings of one box, as the renderers take them: a Hash with Symbol keys, read as methods (config.width). It expands the --box WxHxD/T shorthand, casts numeric Strings to Floats, and fills in the kerf, margin, padding and stroke for the units, and a notch of three times the thickness.

Constant Summary collapse

SIZE_REGEXP =

The --box shorthand: WxHxD/T, or WxHxD/T/N with a notch.

%r{[\d.]+x[\d.]+x[\d.]+/[\d.]+(/[\d.]+)?}
FLOATS =

Distances, in the units of the box: cast to floats, and converted with the units.

%i(width height depth front_height thickness notch margin padding stroke kerf).freeze
ANGLES =

Angles, in degrees: cast to floats, and left alone when the units change.

%i(slope).freeze
NON_ZERO =

Settings that must not be zero.

%i(width height depth thickness stroke).freeze
REQUIRED =

Settings that must be given.

%i(width height depth thickness notch file).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • options (Hash) (defaults to: {}) —

    the settings, by String or Symbol key; nil values, a blank lid and units it does not know are dropped



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
# File 'lib/laser/cutter/configuration.rb', line 72

def initialize(options = {})
  ::Hashie::Extensions::SymbolizeKeys.symbolize_keys!(options)

  options.delete_if { |_k, v| v.nil? }
  options.delete(:lid) if options[:lid].to_s.empty? # a blank form field
  if options[:units]
    unit = options[:units].to_sym
    unless self.class.defaults.key?(unit) || self.class.defaults.key?(unit.to_s)
      options.delete(:units)
    end
  end

  super(self.class.defaults.merge(options))

  if box =~ SIZE_REGEXP
    dim, self[:thickness], self[:notch]       = self[:box].split('/')
    self[:width], self[:height], self[:depth] = dim.split('x')
    delete(:box)
  end
  (FLOATS + ANGLES).each do |k|
    self[k] = self[k].to_f if self[k].is_a?(String)
  end
  merge!(self.class.defaults[self[:units].to_sym].merge(self))
  self[:notch] = (self[:thickness] * 3.0).round(5) if self[:thickness] && self[:notch].nil?
end

Class Attribute Details

.defaults ⇒ Hashie::Mash

Returns the defaults, with the distances for :in and :mm.

Returns:

  • (Hashie::Mash) —

    the defaults, with the distances for :in and :mm



32
# File 'lib/laser/cutter/configuration.rb', line 32

attr_accessor :mutex, :defaults

.mutex ⇒ Mutex

Returns guards the computing of defaults.

Returns:

  • (Mutex) —

    guards the computing of defaults



32
33
34
# File 'lib/laser/cutter/configuration.rb', line 32

def mutex
  @mutex
end

Class Method Details

.compute_defaults! ⇒ Hashie::Mash

Computes the defaults once.

Returns:

  • (Hashie::Mash) —

    a copy of the defaults



37
38
39
40
41
42
43
44
45
# File 'lib/laser/cutter/configuration.rb', line 37

def compute_defaults!
  mutex.synchronize do
    if defined?(@defaults)
      @defaults
    else
      @defaults = compute_metric_units!
    end
  end.dup
end

.compute_metric_units! ⇒ Hashie::Mash

Returns the defaults, with the distances for inches and the same converted to millimetres.

Returns:

  • (Hashie::Mash) —

    the defaults, with the distances for inches and the same converted to millimetres



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/laser/cutter/configuration.rb', line 49

def compute_metric_units!
  defaults = Hashie::Mash.new({
                                units:       :in,
                                page_layout: 'portrait',
                                lid:         'full',
                                metadata:    true,
                                in:          {
                                  kerf:    0.0024, # smallest kerf for thin material, usually it's more than that.
                                  margin:  0.125,
                                  padding: 0.1,
                                  stroke:  0.001,
                                }
                              })

  defaults[:mm] = defaults[:in].map { |k, v| [k, UnitsConverter.in2mm(v)] }.to_h
  defaults
end

Instance Method Details

#change_units(new_units) ⇒ Symbol?

Converts every distance to other units, and leaves angles alone.

Parameters:

  • new_units (String, Symbol) —

    "in" or "mm"

Returns:

  • (Symbol, nil) —

    the new units, or nil when nothing changed



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/laser/cutter/configuration.rb', line 124

def change_units(new_units)
  new_units = new_units.to_sym
  return if new_units == units.to_sym
  return unless self.class.defaults.key?(new_units)

  k = units.to_sym == :in ? UnitsConverter.in2mm(1.0) : UnitsConverter.mm2in(1.0)

  FLOATS.each do |field|
    next if send(field).nil?

    send("#{field}=", (send(field) * k).round(5))
  end

  self.units = new_units
end

#validate! ⇒ void

This method returns an undefined value.

Checks that the settings make a box.

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/laser/cutter/configuration.rb', line 104

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

  unless Box::LIDS.include?(lid.to_s.to_sym)
    raise InvalidOption, "lid is #{lid.to_s.inspect}, but must be one of: #{Box::LIDS.join(', ')}."
  end

  Sloped::Request.validate!(self)
end