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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method 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



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

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

Class Attribute Details

.configurationObject

Configuration accessor.

Since:

  • 0.1.5



37
38
39
# File 'lib/usps_flags.rb', line 37

def configuration
  @configuration
end

Instance Attribute Details

#fieldObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def field
  @field
end

#png_fileObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def png_file
  @png_file
end

#scaleObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def scale
  @scale
end

#svg_fileObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def svg_file
  @svg_file
end

#trimObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def trim
  @trim
end

#typeObject

Constructor accessors.

Parameters:

  • type (String)

    The type of flag to generate.

  • svg_file (String)

    The SVG file output path.

  • png_file (String)

    The PNG file output path.

  • scale (Integer, Float)

    The scale divisor factor.

  • field (Boolean)

    The field setting.

  • trim (Boolean)

    The trim setting.

Since:

  • 0.1.5



122
123
124
# File 'lib/usps_flags.rb', line 122

def type
  @type
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Configuration constructor.

Yields:

Since:

  • 0.1.5



42
43
44
45
46
# File 'lib/usps_flags.rb', line 42

def self.configure
  yield(configuration) if block_given?
  self.ensure_directories
  @configuration
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



137
138
139
140
141
142
143
144
# File 'lib/usps_flags.rb', line 137

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



127
128
129
130
# File 'lib/usps_flags.rb', line 127

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