Module: Laser::Cutter::Types

Defined in:
lib/laser/cutter/types.rb

Overview

The types of the attributes of Options. Each one coerces what a form or a command line sends, Strings mostly, and rejects what it cannot use.

Constant Summary collapse

Length =

A size of the box or of its material: a number above zero.

Coercible::Float.constrained(gt: 0)
Allowance =

A distance that may be zero, such as the kerf or a margin.

Coercible::Float.constrained(gteq: 0)
Slope =

The angle of a sloped front above horizontal, in degrees: flat and upright are both out.

Coercible::Float.constrained(gt: 0, lt: 90)
Units =

The units every dimension is in.

choice(%w[in mm], default: 'in')
Lid =

How the lid joins the walls, see Box::LIDS.

choice(Box::LIDS.map(&:to_s), default: 'full')
PageLayout =

The way the page is turned, for a PDF.

choice(%w[portrait landscape], default: 'portrait')
Format =

The kind of file to draw.

Coercible::String.constructor(&:downcase).enum(*Renderer::FORMATS.keys)
PageSize =

The name of a page size a PDF can take, such as A4 or LETTER.

Coercible::String.constructor(&:upcase).enum(*PageManager::SIZES.keys)

Class Method Summary collapse

Class Method Details

.blank?(value) ⇒ Boolean

Whether a value was left out: nil, or the empty String of a blank form field.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


14
15
16
# File 'lib/laser/cutter/types.rb', line 14

def self.blank?(value)
  value.nil? || Dry::Types::Undefined.equal?(value) || (value.respond_to?(:empty?) && value.empty?)
end

.choice(values, default:) ⇒ Dry::Types::Type

One of a few words, given as a String or a Symbol. Blank means the default.

Parameters:

  • values (Array<String>) —

    the words allowed

  • default (String) —

    the word a blank value stands for

Returns:

  • (Dry::Types::Type)


23
24
25
# File 'lib/laser/cutter/types.rb', line 23

def self.choice(values, default:)
  String.default(default).enum(*values).constructor { |value| blank?(value) ? Dry::Types::Undefined : value.to_s }
end

.optional(type) ⇒ Dry::Types::Type

A type that also takes nil, and reads a blank value as nil.

Parameters:

  • type (Dry::Types::Type)

Returns:

  • (Dry::Types::Type)


31
32
33
# File 'lib/laser/cutter/types.rb', line 31

def self.optional(type)
  type.optional.constructor { |value| blank?(value) ? nil : value }
end