Class: Labrat::Options

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

Overview

The Options class is a glorified Hash, a container for the options settings gathered from the defaults, the config files, the command line, and perhaps environment. An Options instance can be handed off to the label-printing objects to inform its formatting, printing, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**init) ⇒ Options

Initialize with an optional hash of default values for the attributes.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/labrat/options.rb', line 49

def initialize(**init)
  self.label = init[:label] || nil
  self.raw_label = nil
  # Per-page attributes
  self.page_width = init[:page_width] || 24.mm
  self.page_height = init[:page_height] || 87.mm
  self.left_page_margin = init[:left_page_margin] || 5.mm
  self.right_page_margin = init[:right_page_margin] || 5.mm
  self.top_page_margin = init[:top_page_margin] ||  0.mm
  self.bottom_page_margin = init[:bottom_page_margin] || 0.mm
  self.rows = init[:rows] || 1
  self.columns = init[:columns] || 1
  self.row_gap = init[:row_gap] || 0.mm
  self.column_gap = init[:column_gap] || 0.mm
  self.start_label = init[:start_label] || 1
  self.landscape = init.fetch(:landscape, false)
  # Per-label attributes
  self.h_align = init[:h_align]&.to_sym || :center
  self.v_align = init[:v_align]&.to_sym || :center
  self.left_pad = init[:left_pad] || 4.5.mm
  self.right_pad = init[:right_pad] || 4.5.mm
  self.top_pad = init[:top_pad] ||  0
  self.bottom_pad = init[:bottom_pad] || 0
  self.delta_x = init[:delta_x] ||  0
  self.delta_y = init[:delta_y] ||  0
  self.font_name = init[:font_name] || 'Helvetica'
  self.font_style = init[:font_style]&.to_sym || :normal
  self.font_size = init[:font_size] || 12
  # Input attributes
  self.in_file = init[:in_file] || nil
  self.nl_sep = init[:nl_sep] || '~~'
  self.label_sep = init[:label_sep] || '@@'
  self.copies = init[:copies] || 1
  # Output attributes
  self.printer = init[:printer] || ENV['LABRAT_PRINTER'] || ENV['PRINTER'] || 'dymo'
  self.out_file = init[:out_file] || self.class.default_out_file
  self.print_command = init[:print_command] || 'lpr -P %p %o'
  self.view_command = init[:view_command] || 'qpdfview --unique --instance labrat %o'
  self.view = init.fetch(:view, false)
  self.template = init.fetch(:landscape, false)
  self.grid = init.fetch(:gid, false)
  self.verbose = init.fetch(:verbose, false)
  self.msg = init[:msg] || nil
end

Instance Attribute Details

#bottom_padObject

Returns the value of attribute bottom_pad.



9
10
11
# File 'lib/labrat/options.rb', line 9

def bottom_pad
  @bottom_pad
end

#bottom_page_marginObject

Returns the value of attribute bottom_page_margin.



9
10
11
# File 'lib/labrat/options.rb', line 9

def bottom_page_margin
  @bottom_page_margin
end

#column_gapObject

Returns the value of attribute column_gap.



9
10
11
# File 'lib/labrat/options.rb', line 9

def column_gap
  @column_gap
end

#columnsObject

Returns the value of attribute columns.



9
10
11
# File 'lib/labrat/options.rb', line 9

def columns
  @columns
end

#copiesObject

Returns the value of attribute copies.



9
10
11
# File 'lib/labrat/options.rb', line 9

def copies
  @copies
end

#delta_xObject

Returns the value of attribute delta_x.



9
10
11
# File 'lib/labrat/options.rb', line 9

def delta_x
  @delta_x
end

#delta_yObject

Returns the value of attribute delta_y.



9
10
11
# File 'lib/labrat/options.rb', line 9

def delta_y
  @delta_y
end

#font_nameObject

Returns the value of attribute font_name.



9
10
11
# File 'lib/labrat/options.rb', line 9

def font_name
  @font_name
end

#font_sizeObject

Returns the value of attribute font_size.



9
10
11
# File 'lib/labrat/options.rb', line 9

def font_size
  @font_size
end

#font_styleObject

Returns the value of attribute font_style.



9
10
11
# File 'lib/labrat/options.rb', line 9

def font_style
  @font_style
end

#gridObject

Returns the value of attribute grid.



9
10
11
# File 'lib/labrat/options.rb', line 9

def grid
  @grid
end

#h_alignObject

Returns the value of attribute h_align.



9
10
11
# File 'lib/labrat/options.rb', line 9

def h_align
  @h_align
end

#in_fileObject

Returns the value of attribute in_file.



9
10
11
# File 'lib/labrat/options.rb', line 9

def in_file
  @in_file
end

#labelObject

Returns the value of attribute label.



9
10
11
# File 'lib/labrat/options.rb', line 9

def label
  @label
end

#label_sepObject

Returns the value of attribute label_sep.



9
10
11
# File 'lib/labrat/options.rb', line 9

def label_sep
  @label_sep
end

#landscapeObject

Returns the value of attribute landscape.



9
10
11
# File 'lib/labrat/options.rb', line 9

def landscape
  @landscape
end

#left_padObject

Returns the value of attribute left_pad.



9
10
11
# File 'lib/labrat/options.rb', line 9

def left_pad
  @left_pad
end

#left_page_marginObject

Returns the value of attribute left_page_margin.



9
10
11
# File 'lib/labrat/options.rb', line 9

def left_page_margin
  @left_page_margin
end

#msgObject

Returns the value of attribute msg.



9
10
11
# File 'lib/labrat/options.rb', line 9

def msg
  @msg
end

#nl_sepObject

Returns the value of attribute nl_sep.



9
10
11
# File 'lib/labrat/options.rb', line 9

def nl_sep
  @nl_sep
end

#out_fileObject

Returns the value of attribute out_file.



9
10
11
# File 'lib/labrat/options.rb', line 9

def out_file
  @out_file
end

#page_heightObject

Returns the value of attribute page_height.



9
10
11
# File 'lib/labrat/options.rb', line 9

def page_height
  @page_height
end

#page_widthObject

Returns the value of attribute page_width.



9
10
11
# File 'lib/labrat/options.rb', line 9

def page_width
  @page_width
end

Returns the value of attribute print_command.



9
10
11
# File 'lib/labrat/options.rb', line 9

def print_command
  @print_command
end

#printerObject

Returns the value of attribute printer.



9
10
11
# File 'lib/labrat/options.rb', line 9

def printer
  @printer
end

#raw_labelObject

Returns the value of attribute raw_label.



9
10
11
# File 'lib/labrat/options.rb', line 9

def raw_label
  @raw_label
end

#right_padObject

Returns the value of attribute right_pad.



9
10
11
# File 'lib/labrat/options.rb', line 9

def right_pad
  @right_pad
end

#right_page_marginObject

Returns the value of attribute right_page_margin.



9
10
11
# File 'lib/labrat/options.rb', line 9

def right_page_margin
  @right_page_margin
end

#row_gapObject

Returns the value of attribute row_gap.



9
10
11
# File 'lib/labrat/options.rb', line 9

def row_gap
  @row_gap
end

#rowsObject

Returns the value of attribute rows.



9
10
11
# File 'lib/labrat/options.rb', line 9

def rows
  @rows
end

#start_labelObject

Returns the value of attribute start_label.



9
10
11
# File 'lib/labrat/options.rb', line 9

def start_label
  @start_label
end

#templateObject

Returns the value of attribute template.



9
10
11
# File 'lib/labrat/options.rb', line 9

def template
  @template
end

#top_padObject

Returns the value of attribute top_pad.



9
10
11
# File 'lib/labrat/options.rb', line 9

def top_pad
  @top_pad
end

#top_page_marginObject

Returns the value of attribute top_page_margin.



9
10
11
# File 'lib/labrat/options.rb', line 9

def top_page_margin
  @top_page_margin
end

#v_alignObject

Returns the value of attribute v_align.



9
10
11
# File 'lib/labrat/options.rb', line 9

def v_align
  @v_align
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/labrat/options.rb', line 9

def verbose
  @verbose
end

#viewObject

Returns the value of attribute view.



9
10
11
# File 'lib/labrat/options.rb', line 9

def view
  @view
end

#view_commandObject

Returns the value of attribute view_command.



9
10
11
# File 'lib/labrat/options.rb', line 9

def view_command
  @view_command
end

Class Method Details

.attrsObject

For testing, return an Array of the attributes as symbols.



150
151
152
153
# File 'lib/labrat/options.rb', line 150

def self.attrs
  instance_methods(false).grep(/\A[a-z_]+=\Z/)
    .map { |a| a.to_s.delete_suffix('=').to_sym }
end

.default_out_fileObject



119
120
121
122
123
124
125
# File 'lib/labrat/options.rb', line 119

def self.default_out_file
  base = ENV["XDG_DATA_HOME"] || "~/.local/share"
  dir = File.expand_path(File.join(base, "labrat"))
  FileUtils.mkdir_p(dir)
  stamp = Time.now.strftime("%Y-%m-%dT%H:%M:%S.%3N")
  File.join(dir, "#{stamp}-#{$$}.pdf")
end

.flagsObject

For testing, return an Array of the flags-form of the attributes, i.e., with the underscores, _, replaced with hyphens.



157
158
159
# File 'lib/labrat/options.rb', line 157

def self.flags
  attrs.map { |a| a.tr('_', '-') }
end

.set_options(args, verbose: false) ⇒ Object

High-level setting of options from config files, and given command-line args.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/labrat/options.rb', line 96

def self.set_options(args, verbose: false)
  # Default, built-in config; set verbose to param.
  default_config = Labrat::Options.new(verbose: verbose).to_hash
  default_config.report("Default settings") if verbose

  # Config files
  config_reader = FatConfig::Reader.new('labrat', xdg: true)
  file_config = config_reader.read('labrat', verbose: verbose)
  file_options = Labrat::ArgParser.new.parse(file_config, prior: default_config, verbose: verbose)

  # Command-line
  if verbose
    warn "\nCommand-line:"
    args.each do |arg|
      warn arg
    end
    warn ""
  end
  # We want raw_label to reflect what the user typed on the command-line
  file_options.raw_label = nil
  Labrat::ArgParser.new.parse(args, prior: file_options, verbose: verbose)
end

Instance Method Details

#[](att) ⇒ Object

Allow hash-like access to attributes. This allows an Options object to be used, for example, in the OptionParser#parse :into parameter.



144
145
146
147
# File 'lib/labrat/options.rb', line 144

def [](att)
  att = att.to_s.tr('-', '_')
  send(att.to_s)
end

#[]=(att, val) ⇒ Object

Allow hash-like assignment to attributes. This allows an Options object to be used, for example, in the OptionParser#parse :into parameter.



134
135
136
137
138
139
140
# File 'lib/labrat/options.rb', line 134

def []=(att, val)
  if att == :out_file
    val = File.expand_path(val)
  end
  att = att.to_s.tr('-', '_')
  send(:"#{att}=", val)
end

#merge!(hsh) ⇒ Object

Update the fields of this Option instance by merging in the values in hsh into self. Ignore any keys in hsh not corresponding to a setter for an Options object.



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/labrat/options.rb', line 212

def merge!(hsh)
  # Convert any separator hyphens in the hash keys to underscores
  hsh = hsh.to_hash.transform_keys { |key| key.to_s.tr('-', '_').to_sym }
  new_hash = to_hash.merge(hsh)
  new_hash.each_pair do |k, val|
    setter = "#{k}=".to_sym
    next unless respond_to?(setter)

    send(setter, val)
  end
  self
end

#to_hashObject

Return a hash of the values in this Options object. This is the canonical form of a Hash for Labrat, i.e., symbolic keys with any hyphens translated into underscores. Don't include the msg attribute.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/labrat/options.rb', line 164

def to_hash
  {
    label: label,
    raw_label: raw_label,
    page_width: page_width,
    page_height: page_height,
    left_page_margin: left_page_margin,
    right_page_margin: right_page_margin,
    top_page_margin: top_page_margin,
    bottom_page_margin: bottom_page_margin,
    rows: rows,
    columns: columns,
    row_gap: row_gap,
    column_gap: column_gap,
    grid: grid,
    start_label: start_label,
    landscape: landscape,
    # Per-label attributes
    h_align: h_align,
    v_align: v_align,
    left_pad: left_pad,
    right_pad: right_pad,
    top_pad: top_pad,
    bottom_pad: bottom_pad,
    delta_x: delta_x,
    delta_y: delta_y,
    font_name: font_name,
    font_style: font_style,
    font_size: font_size,
    # Input attributes
    in_file: in_file,
    nl_sep: nl_sep,
    label_sep: label_sep,
    copies: copies,
    # Output attributes
    printer: printer,
    out_file: out_file,
    print_command: print_command,
    view_command: view_command,
    view: view,
    template: template,
    verbose: verbose,
  }
end

#to_sObject

Return any string in msg, e.g., the usage help or error.



128
129
130
# File 'lib/labrat/options.rb', line 128

def to_s
  msg
end