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



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

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



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

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



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

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



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

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



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

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



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

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



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

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.

Since:

  • 0.1.5



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

def png
  raise "Error: png_file must be set." 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



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

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