Class: Laser::Cutter::Options

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/laser/cutter/options.rb

Overview

Everything a box can be asked for, typed. This is what a Ruby program hands to render or write.

Values are coerced, so the Strings of a web form will do, and an instance cannot be changed: #new(lid: :plain) returns a changed copy. Keys may be Strings or Symbols. A key it does not know, a value of the wrong type or a dimension left out all raise, at once, before anything is drawn.

The distances left nil take the default for the units: a kerf of 0.0024in, a margin of 0.125in, a padding of 0.1in, a stroke of 0.001in, and a notch three times the thickness.

Examples:

A box in millimeters, with a plain lid, as an SVG

options = Laser::Cutter::Options.new(width: 70, height: 20, depth: 50, thickness: 4.3,
                                     units: :mm, lid: :plain, format: :svg)
options.lid                 # => "plain"
Laser::Cutter.render(options) # => "<svg ..."

A box whose front slopes back at 45 degrees, from 1 inch up

Laser::Cutter::Options.new(width: 4, height: 3, depth: 5, thickness: 0.125, slope: 45, front_height: 1)

Raises:

  • (MissingOption) —

    when width, height, depth or thickness is left out, or a slope has no front height

  • (InvalidOption) —

    when a value cannot be used, a key is not an option, or the numbers do not make a sloped box

Constant Summary collapse

REQUIRED =

The dimensions a box cannot be drawn without.

%i[width height depth thickness].freeze
MAXIMUM_CHOICES =

The longest list of allowed values an error spells out.

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#depth ⇒ Float (readonly)

Returns internal depth of the box.

Returns:

  • (Float) —

    internal depth of the box



49
# File 'lib/laser/cutter/options.rb', line 49

attribute :depth, Types::Length

#file ⇒ String? (readonly)

Returns the file Laser::Cutter.write writes.

Returns:



88
# File 'lib/laser/cutter/options.rb', line 88

attribute? :file, Types.optional(Types::Coercible::String)

#format ⇒ String? (readonly)

Returns "pdf" or "svg"; nil for a PDF, or for what the extension of the file says.

Returns:

  • (String, nil) —

    "pdf" or "svg"; nil for a PDF, or for what the extension of the file says



84
# File 'lib/laser/cutter/options.rb', line 84

attribute? :format, Types.optional(Types::Format)

#front_height ⇒ Float? (readonly)

Returns internal height at the front of a sloped box, where the slope begins; required with a slope, and 0 starts the slope at the bottom.

Returns:

  • (Float, nil) —

    internal height at the front of a sloped box, where the slope begins; required with a slope, and 0 starts the slope at the bottom



63
# File 'lib/laser/cutter/options.rb', line 63

attribute? :front_height, Types.optional(Types::Allowance)

#height ⇒ Float (readonly)

Returns internal height of the box.

Returns:

  • (Float) —

    internal height of the box



45
# File 'lib/laser/cutter/options.rb', line 45

attribute :height, Types::Length

#inside_box ⇒ Boolean (readonly)

Returns whether the box is also drawn without kerf, in red.

Returns:

  • (Boolean) —

    whether the box is also drawn without kerf, in red



116
# File 'lib/laser/cutter/options.rb', line 116

attribute :inside_box, Types::Params::Bool.default(false)

#kerf ⇒ Float? (readonly)

Returns width of the cut; nil for the default.

Returns:

  • (Float, nil) —

    width of the cut; nil for the default



71
# File 'lib/laser/cutter/options.rb', line 71

attribute? :kerf, Types.optional(Types::Allowance)

#lid ⇒ String (readonly)

Returns "full", "back" or "plain": notched on every side, into the back wall only, or on no side. On a sloped box it is the tilted panel: "full" notches it into the side walls, "plain" lays it on them.

Returns:

  • (String) —

    "full", "back" or "plain": notched on every side, into the back wall only, or on no side. On a sloped box it is the tilted panel: "full" notches it into the side walls, "plain" lays it on them.



80
# File 'lib/laser/cutter/options.rb', line 80

attribute :lid, Types::Lid

#margin ⇒ Float? (readonly)

Returns margin from the edge of the page; nil for the default.

Returns:

  • (Float, nil) —

    margin from the edge of the page; nil for the default



92
# File 'lib/laser/cutter/options.rb', line 92

attribute? :margin, Types.optional(Types::Allowance)

#metadata ⇒ Boolean (readonly)

Returns whether the settings of the box are printed on the page.

Returns:

  • (Boolean) —

    whether the settings of the box are printed on the page



112
# File 'lib/laser/cutter/options.rb', line 112

attribute :metadata, Types::Params::Bool.default(true)

#notch ⇒ Float? (readonly)

Returns notch length, a guide only; nil for three times the thickness.

Returns:

  • (Float, nil) —

    notch length, a guide only; nil for three times the thickness



67
# File 'lib/laser/cutter/options.rb', line 67

attribute? :notch, Types.optional(Types::Length)

#padding ⇒ Float? (readonly)

Returns space between the panels; nil for the default.

Returns:

  • (Float, nil) —

    space between the panels; nil for the default



96
# File 'lib/laser/cutter/options.rb', line 96

attribute? :padding, Types.optional(Types::Allowance)

#page_layout ⇒ String (readonly)

Returns "portrait" or "landscape", for a PDF.

Returns:

  • (String) —

    "portrait" or "landscape", for a PDF



108
# File 'lib/laser/cutter/options.rb', line 108

attribute :page_layout, Types::PageLayout

#page_size ⇒ String? (readonly)

Returns a page size such as "A4", for a PDF; nil to fit the box.

Returns:

  • (String, nil) —

    a page size such as "A4", for a PDF; nil to fit the box



104
# File 'lib/laser/cutter/options.rb', line 104

attribute? :page_size, Types.optional(Types::PageSize)

#slope ⇒ Float? (readonly)

Returns angle of a sloped front above horizontal, in degrees, above 0 and below 90; nil for the box with six panels.

Returns:

  • (Float, nil) —

    angle of a sloped front above horizontal, in degrees, above 0 and below 90; nil for the box with six panels



58
# File 'lib/laser/cutter/options.rb', line 58

attribute? :slope, Types.optional(Types::Slope)

#stroke ⇒ Float? (readonly)

Returns stroke width of the lines; nil for the default.

Returns:

  • (Float, nil) —

    stroke width of the lines; nil for the default



100
# File 'lib/laser/cutter/options.rb', line 100

attribute? :stroke, Types.optional(Types::Length)

#thickness ⇒ Float (readonly)

Returns thickness of the material.

Returns:

  • (Float) —

    thickness of the material



53
# File 'lib/laser/cutter/options.rb', line 53

attribute :thickness, Types::Length

#units ⇒ String (readonly)

Returns "in" or "mm", the units of every distance here.

Returns:

  • (String) —

    "in" or "mm", the units of every distance here



75
# File 'lib/laser/cutter/options.rb', line 75

attribute :units, Types::Units

#width ⇒ Float (readonly)

Returns internal width of the box.

Returns:

  • (Float) —

    internal width of the box



41
# File 'lib/laser/cutter/options.rb', line 41

attribute :width, Types::Length

Class Method Details

.new(attributes = {}) ⇒ Options

Parameters:

  • attributes (Hash, Options) (defaults to: {}) —

    the options, by name

Returns:

Raises:



122
123
124
125
126
127
128
129
# File 'lib/laser/cutter/options.rb', line 122

def new(attributes = {}, *)
  return attributes if attributes.is_a?(self)

  require_dimensions!(attributes) if attributes.respond_to?(:to_h)
  super.tap { |options| Sloped::Request.validate!(options.to_h) }
rescue Dry::Struct::Error => e
  raise InvalidOption, explain(e)
end

Instance Method Details

#to_configuration ⇒ Configuration

Returns the same settings, as the renderers take them.

Returns:

  • (Configuration) —

    the same settings, as the renderers take them



170
171
172
# File 'lib/laser/cutter/options.rb', line 170

def to_configuration
  Configuration.new(to_h.except(:format, :inside_box).merge(debug: inside_box))
end