Class: Btape::Settings
- Inherits:
-
Object
- Object
- Btape::Settings
- Defined in:
- lib/btape/settings.rb
Overview
The knobs a script turns with Set NAME VALUE, along with their defaults,
their coercions, and the validation the parser calls while reading a tape.
Values arrive from three places, in increasing order of precedence: the
defaults here, the Set lines in the tape, and whatever the caller passes
to Runner#run or the CLI passes from its flags.
Constant Summary collapse
- CAPTURE_MODES =
%w[interval manual].freeze
- QUANTIZERS =
%w[adaptive rgb332].freeze
- URL_SCHEMES =
%w[ws:// wss:// http:// https://].freeze
- DEFINITIONS =
Script name => [attribute, coercion, default]. Defaults are already coerced, so durations are seconds and not "100ms".
{ 'WsUrl' => [:ws_url, :url, nil], 'CaptureMode' => [:capture_mode, CAPTURE_MODES, 'interval'], 'Framerate' => [:framerate, :positive_float, 10.0], 'FrameDelay' => [:frame_delay, :duration, 0.1], 'Loop' => [:loop_count, :count, 0], 'Scale' => [:scale, :positive_float, 1.0], 'OutputWidth' => [:output_width, :positive_integer, nil], 'Quantizer' => [:quantizer, QUANTIZERS, 'adaptive'], 'Timeout' => [:timeout, :duration, 120.0], 'WaitTimeout' => [:wait_timeout, :duration, 10.0], 'WaitInterval' => [:wait_interval, :duration, 0.1], 'WaitStable' => [:wait_stable, :positive_integer, 1], 'MaxFrames' => [:max_frames, :positive_integer, 600] }.freeze
- ATTRIBUTES =
DEFINITIONS.each_value.map(&:first).freeze
Class Method Summary collapse
- .defaults ⇒ Object
- .from_commands(commands) ⇒ Object
-
.validate!(name, value) ⇒ Object
Validates one
Setline and returns the coerced value.
Instance Method Summary collapse
-
#initialize(values = {}) ⇒ Settings
constructor
A new instance of Settings.
- #merge(overrides) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(values = {}) ⇒ Settings
Returns a new instance of Settings.
115 116 117 118 |
# File 'lib/btape/settings.rb', line 115 def initialize(values = {}) @values = defaults_merged_with(values).freeze freeze end |
Class Method Details
.defaults ⇒ Object
40 41 42 |
# File 'lib/btape/settings.rb', line 40 def defaults DEFINITIONS.each_value.to_h { |attribute, _coercion, default| [attribute, default] } end |
.from_commands(commands) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/btape/settings.rb', line 51 def from_commands(commands) values = commands.select { |command| command.name == 'Set' }.to_h do |command| validate!(*command.arguments) rescue ArgumentError => e raise ScriptError.new(command.line_number, e.) end new(values) end |
.validate!(name, value) ⇒ Object
Validates one Set line and returns the coerced value. Raises
ArgumentError, which Parser turns into a ScriptError carrying the line.
46 47 48 49 |
# File 'lib/btape/settings.rb', line 46 def validate!(name, value) attribute, coercion, = DEFINITIONS.fetch(name) { raise ArgumentError, "unknown setting #{name.inspect}" } [attribute, coerce(coercion, value, name)] end |
Instance Method Details
#merge(overrides) ⇒ Object
120 121 122 123 124 |
# File 'lib/btape/settings.rb', line 120 def merge(overrides) return self if overrides.nil? || (overrides.respond_to?(:empty?) && overrides.empty?) self.class.new(@values.merge(normalize(overrides))) end |
#to_h ⇒ Object
126 127 128 |
# File 'lib/btape/settings.rb', line 126 def to_h @values.dup end |