Class: Labrat::Options
- Inherits:
-
Object
- Object
- Labrat::Options
- 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
-
#bottom_pad ⇒ Object
Returns the value of attribute bottom_pad.
-
#bottom_page_margin ⇒ Object
Returns the value of attribute bottom_page_margin.
-
#column_gap ⇒ Object
Returns the value of attribute column_gap.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#copies ⇒ Object
Returns the value of attribute copies.
-
#delta_x ⇒ Object
Returns the value of attribute delta_x.
-
#delta_y ⇒ Object
Returns the value of attribute delta_y.
-
#font_name ⇒ Object
Returns the value of attribute font_name.
-
#font_size ⇒ Object
Returns the value of attribute font_size.
-
#font_style ⇒ Object
Returns the value of attribute font_style.
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#h_align ⇒ Object
Returns the value of attribute h_align.
-
#in_file ⇒ Object
Returns the value of attribute in_file.
-
#label ⇒ Object
Returns the value of attribute label.
-
#label_sep ⇒ Object
Returns the value of attribute label_sep.
-
#landscape ⇒ Object
Returns the value of attribute landscape.
-
#left_pad ⇒ Object
Returns the value of attribute left_pad.
-
#left_page_margin ⇒ Object
Returns the value of attribute left_page_margin.
-
#msg ⇒ Object
Returns the value of attribute msg.
-
#nl_sep ⇒ Object
Returns the value of attribute nl_sep.
-
#out_file ⇒ Object
Returns the value of attribute out_file.
-
#page_height ⇒ Object
Returns the value of attribute page_height.
-
#page_width ⇒ Object
Returns the value of attribute page_width.
-
#print_command ⇒ Object
Returns the value of attribute print_command.
-
#printer ⇒ Object
Returns the value of attribute printer.
-
#raw_label ⇒ Object
Returns the value of attribute raw_label.
-
#right_pad ⇒ Object
Returns the value of attribute right_pad.
-
#right_page_margin ⇒ Object
Returns the value of attribute right_page_margin.
-
#row_gap ⇒ Object
Returns the value of attribute row_gap.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#start_label ⇒ Object
Returns the value of attribute start_label.
-
#template ⇒ Object
Returns the value of attribute template.
-
#top_pad ⇒ Object
Returns the value of attribute top_pad.
-
#top_page_margin ⇒ Object
Returns the value of attribute top_page_margin.
-
#v_align ⇒ Object
Returns the value of attribute v_align.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#view ⇒ Object
Returns the value of attribute view.
-
#view_command ⇒ Object
Returns the value of attribute view_command.
Class Method Summary collapse
-
.attrs ⇒ Object
For testing, return an Array of the attributes as symbols.
- .default_out_file ⇒ Object
-
.flags ⇒ Object
For testing, return an Array of the flags-form of the attributes, i.e., with the underscores, _, replaced with hyphens.
-
.set_options(args, verbose: false) ⇒ Object
High-level setting of options from config files, and given command-line args.
Instance Method Summary collapse
-
#[](att) ⇒ Object
Allow hash-like access to attributes.
-
#[]=(att, val) ⇒ Object
Allow hash-like assignment to attributes.
-
#initialize(**init) ⇒ Options
constructor
Initialize with an optional hash of default values for the attributes.
-
#merge!(hsh) ⇒ Object
Update the fields of this Option instance by merging in the values in hsh into self.
-
#to_hash ⇒ Object
Return a hash of the values in this Options object.
-
#to_s ⇒ Object
Return any string in msg, e.g., the usage help or error.
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_pad ⇒ Object
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_margin ⇒ Object
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_gap ⇒ Object
Returns the value of attribute column_gap.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def column_gap @column_gap end |
#columns ⇒ Object
Returns the value of attribute columns.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def columns @columns end |
#copies ⇒ Object
Returns the value of attribute copies.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def copies @copies end |
#delta_x ⇒ Object
Returns the value of attribute delta_x.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def delta_x @delta_x end |
#delta_y ⇒ Object
Returns the value of attribute delta_y.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def delta_y @delta_y end |
#font_name ⇒ Object
Returns the value of attribute font_name.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def font_name @font_name end |
#font_size ⇒ Object
Returns the value of attribute font_size.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def font_size @font_size end |
#font_style ⇒ Object
Returns the value of attribute font_style.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def font_style @font_style end |
#grid ⇒ Object
Returns the value of attribute grid.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def grid @grid end |
#h_align ⇒ Object
Returns the value of attribute h_align.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def h_align @h_align end |
#in_file ⇒ Object
Returns the value of attribute in_file.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def in_file @in_file end |
#label ⇒ Object
Returns the value of attribute label.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def label @label end |
#label_sep ⇒ Object
Returns the value of attribute label_sep.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def label_sep @label_sep end |
#landscape ⇒ Object
Returns the value of attribute landscape.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def landscape @landscape end |
#left_pad ⇒ Object
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_margin ⇒ Object
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 |
#msg ⇒ Object
Returns the value of attribute msg.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def msg @msg end |
#nl_sep ⇒ Object
Returns the value of attribute nl_sep.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def nl_sep @nl_sep end |
#out_file ⇒ Object
Returns the value of attribute out_file.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def out_file @out_file end |
#page_height ⇒ Object
Returns the value of attribute page_height.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def page_height @page_height end |
#page_width ⇒ Object
Returns the value of attribute page_width.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def page_width @page_width end |
#print_command ⇒ Object
Returns the value of attribute print_command.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def print_command @print_command end |
#printer ⇒ Object
Returns the value of attribute printer.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def printer @printer end |
#raw_label ⇒ Object
Returns the value of attribute raw_label.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def raw_label @raw_label end |
#right_pad ⇒ Object
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_margin ⇒ Object
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_gap ⇒ Object
Returns the value of attribute row_gap.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def row_gap @row_gap end |
#rows ⇒ Object
Returns the value of attribute rows.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def rows @rows end |
#start_label ⇒ Object
Returns the value of attribute start_label.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def start_label @start_label end |
#template ⇒ Object
Returns the value of attribute template.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def template @template end |
#top_pad ⇒ Object
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_margin ⇒ Object
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_align ⇒ Object
Returns the value of attribute v_align.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def v_align @v_align end |
#verbose ⇒ Object
Returns the value of attribute verbose.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def verbose @verbose end |
#view ⇒ Object
Returns the value of attribute view.
9 10 11 |
# File 'lib/labrat/options.rb', line 9 def view @view end |
#view_command ⇒ Object
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
.attrs ⇒ Object
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_file ⇒ Object
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.(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 |
.flags ⇒ Object
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.(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) = 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 .raw_label = nil Labrat::ArgParser.new.parse(args, prior: , 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.(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_hash ⇒ Object
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_s ⇒ Object
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 |