Class: USPSFlags

Inherits:
Object
  • Object
show all
Defined in:
lib/usps_flags.rb

Overview

Base class for the namespace. Provides a constructor DSL.

Author:

  • Julian Fiander

Since:

  • 0.1.5

Defined Under Namespace

Classes: Config, Generate, Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ USPSFlags

Constructor for individual flags.

Examples:

Generate insignia at default scale for Lt/C

flag = USPSFlags.new do |f|
  f.type = "LtC"
  f.scale = 3
  f.field = false
  f.trim = true
  f.svg_file = "/path/to/svg/output.svg"
  f.png_file = "/path/to/png/output.png"
end

flag.svg #=> Generates SVG file at "/path/to/svg/output.svg"
flag.png #=> Generates PNG file at "/path/to/png/output.png"

Yields:

  • (_self)

Yield Parameters:

  • _self (USPSFlags)

    the object that the method was called on

Since:

  • 0.1.5



46
47
48
49
50
51
52
53
54
# File 'lib/usps_flags.rb', line 46

def initialize
  @type = nil
  @svg_file = nil
  @png_file = nil
  @scale = nil
  @field = nil
  @trim = nil
  yield self if block_given?
end

Instance Attribute Details

#fieldBoolean

Constructor accessor for whether to generate the flag field (including any border).

Parameters:

  • field (Boolean)

    The field setting.

Returns:

  • (Boolean)

    Returns the current (or updated) setting.

Since:

  • 0.1.5



86
87
88
# File 'lib/usps_flags.rb', line 86

def field
  @field
end

#png_fileString

Constructor accessor for the PNG file output path.

Parameters:

  • png_file (String)

    The PNG file output path.

Returns:

  • (String)

    Returns the current (or updated) PNG file output path.

Since:

  • 0.1.5



72
73
74
# File 'lib/usps_flags.rb', line 72

def png_file
  @png_file
end

#scaleInteger, Float

Constructor accessor for the image scale divisor factor.

Available options are Float between 0 and 1, or Integer above 1.

Parameters:

  • scale (Integer, Float)

    The scale divisor factor.

Returns:

  • (Integer, Float)

    Returns the current (or updated) scaling factor.

Since:

  • 0.1.5



80
81
82
# File 'lib/usps_flags.rb', line 80

def scale
  @scale
end

#svg_fileString

Constructor accessor for the SVG file output path.

Parameters:

  • svg_file (String)

    The SVG file output path.

Returns:

  • (String)

    Returns the current (or updated) SVG file output path.

Since:

  • 0.1.5



66
67
68
# File 'lib/usps_flags.rb', line 66

def svg_file
  @svg_file
end

#trimString

Constructor accessor for whether to trim the generated PNG file of excess transparency.

Parameters:

  • trim (Boolean)

    The trim setting.

Returns:

  • (String)

    Returns the current (or updated) setting.

Since:

  • 0.1.5



92
93
94
# File 'lib/usps_flags.rb', line 92

def trim
  @trim
end

#typeString

Constructor accessor for the flag type.

Parameters:

  • type (String)

    The type of flag to generate.

Returns:

  • (String)

Since:

  • 0.1.5



60
61
62
# File 'lib/usps_flags.rb', line 60

def type
  @type
end

Instance Method Details

#pngString

Generates the constructed file as PNG.

Requires the constructor to have a value for png_file.

Returns:

  • (String)

    Returns the SVG file output path.

Raises:

  • (USPSFlags::Errors::PNGGenerationError)

Since:

  • 0.1.5



107
108
109
110
111
112
113
114
# File 'lib/usps_flags.rb', line 107

def png
  raise USPSFlags::Errors::PNGGenerationError, "A path must be set with png_file." if self.png_file.nil?
  svg_file_storage = self.svg_file
  self.svg_file = ""
  USPSFlags::Generate.png(self.svg, outfile: self.png_file, trim: self.trim)
  self.svg_file = svg_file_storage
  self.png_file
end

#svgString

Generates the constructed file as SVG.

Returns:

  • (String)

    Returns the SVG file output path, or the svg data if no path was specified.

Since:

  • 0.1.5



97
98
99
100
# File 'lib/usps_flags.rb', line 97

def svg
  svg = USPSFlags::Generate.svg(self.type, outfile: self.svg_file, scale: self.scale, field: self.field)
  (self.svg_file.nil? || self.svg_file == "") ? svg : self.svg_file
end