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



29
30
31
32
33
34
35
36
37
# File 'lib/usps_flags.rb', line 29

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



97
98
99
100
101
102
103
104
# File 'lib/usps_flags.rb', line 97

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



132
133
134
135
136
137
138
139
# File 'lib/usps_flags.rb', line 132

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



69
70
71
72
73
74
75
76
# File 'lib/usps_flags.rb', line 69

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



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

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



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

def svg
  svg = USPSFlags::Generate.get(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



56
57
58
59
60
61
62
63
# File 'lib/usps_flags.rb', line 56

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



110
111
112
113
114
115
116
117
# File 'lib/usps_flags.rb', line 110

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



43
44
45
46
47
48
49
50
# File 'lib/usps_flags.rb', line 43

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