Sha256: f7a9beaa0a39d328c513f96f90f960594983427792e5dbf744718d2cdb3e553e

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'grover/utils'

class Grover
  #
  # Convert string option values to boolean, numeric, and array literals
  #
  class OptionsFixer
    FALSE_VALUES = [nil, false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze

    def initialize(options)
      @options = options
    end

    def run
      fix_boolean_options!
      fix_integer_options!
      fix_float_options!
      fix_array_options!
      @options
    end

    private

    def fix_options!(*option_paths)
      option_paths.each do |option_path|
        keys = option_path.split '.'
        value = @options.dig(*keys)
        Utils.deep_assign(@options, keys, yield(value)) if value
      end
    end

    def fix_boolean_options!
      fix_options!(
        'display_header_footer', 'full_page', 'landscape', 'omit_background', 'prefer_css_page_size',
        'print_background', 'viewport.has_touch', 'viewport.is_landscape', 'viewport.is_mobile'
      ) { |value| !FALSE_VALUES.include?(value) }
    end

    def fix_integer_options!
      fix_options!(
        'viewport.height', 'viewport.width',
        &:to_i
      )
    end

    def fix_float_options!
      fix_options!(
        'clip.height', 'clip.width', 'clip.x', 'clip.y', 'quality', 'scale', 'viewport.device_scale_factor',
        &:to_f
      )
    end

    def fix_array_options!
      fix_options!('launch_args') do |value|
        value.is_a?(String) ? YAML.safe_load(value) : value
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
grover-0.14.2 lib/grover/options_fixer.rb
grover-0.14.1 lib/grover/options_fixer.rb
grover-0.13.3 lib/grover/options_fixer.rb
grover-0.13.2 lib/grover/options_fixer.rb
grover-0.13.1 lib/grover/options_fixer.rb
grover-0.12.3 lib/grover/options_fixer.rb
grover-0.12.2 lib/grover/options_fixer.rb
grover-0.12.1 lib/grover/options_fixer.rb
grover-0.11.4 lib/grover/options_fixer.rb
grover-0.11.3 lib/grover/options_fixer.rb