Sha256: 2db3192bb3bbb020f203e37b2dfffa609a8f201b36218b89ca0903ed6843db0b

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

class ImageOptim
  # Hold information about an option
  class OptionDefinition
    attr_reader :name, :default, :type, :description, :proc

    def initialize(name, default, type_or_description, description = nil, &proc)
      if type_or_description.is_a?(Class)
        type = type_or_description
      else
        type, description = default.class, type_or_description
      end

      @name = name.to_sym
      @description = description.to_s
      @default, @type, @proc = default, type, proc
    end

    # Get value for worker from options
    def value(worker, options)
      value = options.key?(name) ? options[name] : default
      if proc
        if proc.arity == 2
          worker.instance_exec(value, self, &proc)
        else
          worker.instance_exec(value, &proc)
        end
      else
        value
      end
    end

    # Describe default value, returns string as is otherwise surrounds
    # inspected value with backticks
    def default_description
      default.is_a?(String) ? default : "`#{default.inspect}`"
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
image_optim-0.31.4 lib/image_optim/option_definition.rb
image_optim-0.31.3 lib/image_optim/option_definition.rb
image_optim-0.31.2 lib/image_optim/option_definition.rb
image_optim-0.31.1 lib/image_optim/option_definition.rb
image_optim-0.31.0 lib/image_optim/option_definition.rb
image_optim-0.30.0 lib/image_optim/option_definition.rb
image_optim-0.29.0 lib/image_optim/option_definition.rb
image_optim-0.28.0 lib/image_optim/option_definition.rb
image_optim-0.27.1 lib/image_optim/option_definition.rb
image_optim-0.27.0 lib/image_optim/option_definition.rb
image_optim-0.26.5 lib/image_optim/option_definition.rb
image_optim-0.26.4 lib/image_optim/option_definition.rb