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 Method Summary collapse

Constructor Details

#initialize(&block) ⇒ USPSFlags

Constructor for individual flags.

Examples:

Generate insignia at default scale for Lt/C

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

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

Since:

  • 0.1.5



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

def initialize(&block)
  @type = nil
  @svg_file = nil
  @png_file = nil
  @scale = nil
  @field = nil
  @trim = nil
  instance_eval(&block) if block_given?
end

Instance Method Details

#field(bool = nil) ⇒ Boolean

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

Parameters:

  • field (Boolean)

    If set, updates the constructor’s field setting.

Returns:

  • (Boolean)

    Returns the current (or updated) setting.

Since:

  • 0.1.5



113
114
115
116
117
118
119
120
# File 'lib/usps_flags.rb', line 113

def field(bool = nil)
  if bool.nil?
    @field
  else
    @field = bool
    self
  end
end

#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



148
149
150
151
152
153
154
155
# File 'lib/usps_flags.rb', line 148

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

#png_file(string = nil) ⇒ String

Constructor accessor for the PNG file output path.

Parameters:

  • png_file (String)

    If set, updates the constructor’s PNG file output path.

Returns:

  • (String)

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

Since:

  • 0.1.5



85
86
87
88
89
90
91
92
# File 'lib/usps_flags.rb', line 85

def png_file(string = nil)
  if string.nil?
    @png_file
  else
    @png_file = string
    self
  end
end

#scale(num = nil) ⇒ Integer, 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)

    If set, updates the constructor’s scale divisor factor.

Returns:

  • (Integer, Float)

    Returns the current (or updated) scaling factor.

Since:

  • 0.1.5



100
101
102
103
104
105
106
107
# File 'lib/usps_flags.rb', line 100

def scale(num = nil)
  if num.nil?
    @scale
  else
    @scale = num
    self
  end
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



138
139
140
141
# File 'lib/usps_flags.rb', line 138

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

#svg_file(string = nil) ⇒ String

Constructor accessor for the SVG file output path.

Parameters:

  • svg_file (String)

    If set, updates the constructor’s SVG file output path.

Returns:

  • (String)

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

Since:

  • 0.1.5



72
73
74
75
76
77
78
79
# File 'lib/usps_flags.rb', line 72

def svg_file(string = nil)
  if string.nil?
    @svg_file
  else
    @svg_file = string
    self
  end
end

#trim(bool = nil) ⇒ String

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

Parameters:

  • trim (Boolean)

    If set, updates the constructor’s trim setting.

Returns:

  • (String)

    Returns the current (or updated) setting.

Since:

  • 0.1.5



126
127
128
129
130
131
132
133
# File 'lib/usps_flags.rb', line 126

def trim(bool = nil)
  if bool.nil?
    @trim
  else
    @trim = bool
    self
  end
end

#type(string = nil) ⇒ String

Constructor accessor for the flag type.

Parameters:

  • type (String)

    If set, updates the constructor’s flag type.

Returns:

  • (String)

    Returns the current (or updated) flag type.

Since:

  • 0.1.5



59
60
61
62
63
64
65
66
# File 'lib/usps_flags.rb', line 59

def type(string = nil)
  if string.nil?
    @type
  else
    @type = string
    self
  end
end