Class: Sevgi::Graphics::Paper

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/sevgi/graphics/auxiliary/paper.rb

Overview

Paper size and unit profile.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, unit: "mm", name: :custom) ⇒ void

Creates a paper profile.

Parameters:

  • width (Numeric)

    paper width

  • height (Numeric)

    paper height

  • unit (Symbol, String) (defaults to: "mm")

    SVG unit

  • name (Symbol, String) (defaults to: :custom)

    profile name

Raises:

  • (Sevgi::ArgumentError)

    when dimensions, unit, or profile name are invalid



25
26
27
28
29
30
31
32
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 25

def initialize(width:, height:, unit: "mm", name: :custom)
  super(
    width: self.class.send(:dimension!, :width, width),
    height: self.class.send(:dimension!, :height, height),
    unit: self.class.send(:symbol!, :unit, unit),
    name: self.class.send(:symbol!, :name, name)
  )
end

Instance Attribute Details

#heightFloat (readonly)

Returns paper height.

Returns:

  • (Float)

    paper height



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def height
  @height
end

#nameSymbol (readonly)

Returns profile name.

Returns:

  • (Symbol)

    profile name



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def name
  @name
end

#unitSymbol (readonly)

Returns SVG unit.

Returns:

  • (Symbol)

    SVG unit



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def unit
  @unit
end

#widthFloat (readonly)

Returns paper width.

Returns:

  • (Float)

    paper width



6
7
8
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 6

def width
  @width
end

Class Method Details

.define(name, **spec) ⇒ Sevgi::Graphics::Paper

Defines or replaces a named paper profile.

Parameters:

  • name (Symbol, String)

    profile name

  • spec (Hash)

    paper dimensions and unit

Options Hash (**spec):

  • :width (Numeric)

    paper width

  • :height (Numeric)

    paper height

  • :unit (Symbol, String)

    SVG unit

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the profile name is reserved or invalid



73
74
75
76
77
78
79
80
81
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 73

def self.define(name, **spec)
  name = symbol!(:name, name)

  ArgumentError.("Paper name is reserved: #{name}") if reserved?(name) && !exist?(name)

  singleton_class.remove_method(name) if singleton_class.method_defined?(name, false)
  singleton_class.attr_reader(name)
  profiles[name] = instance_variable_set("@#{name}", new(name:, **spec))
end

.exist?(name) ⇒ Boolean

Reports whether a named paper profile exists.

Parameters:

  • name (Object)

    profile name

Returns:

  • (Boolean)


63
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 63

def self.exist?(name) = name.respond_to?(:to_sym) && profiles.key?(name.to_sym)

Instance Method Details

#<=>(other) ⇒ Integer?

Compares papers by width, height, unit, then name.

Parameters:

Returns:

  • (Integer, nil)


37
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 37

def <=>(other) = deconstruct <=> other.deconstruct

#eql?(other) ⇒ Boolean Also known as: ==

Reports strict paper equality.

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Boolean)


42
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 42

def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


46
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 46

def hash = [self.class, *deconstruct].hash

#longestFloat

Returns the longer side.

Returns:

  • (Float)


50
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 50

def longest = [width, height].max

#shortestFloat

Returns the shorter side.

Returns:

  • (Float)


54
# File 'lib/sevgi/graphics/auxiliary/paper.rb', line 54

def shortest = [width, height].min