Class: ImageOptim::OptionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/option_definition.rb

Overview

Hold information about an option

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, type_or_description, description = nil, &proc) ⇒ OptionDefinition

Returns a new instance of OptionDefinition.


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/image_optim/option_definition.rb', line 6

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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default


4
5
6
# File 'lib/image_optim/option_definition.rb', line 4

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description


4
5
6
# File 'lib/image_optim/option_definition.rb', line 4

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name


4
5
6
# File 'lib/image_optim/option_definition.rb', line 4

def name
  @name
end

#procObject (readonly)

Returns the value of attribute proc


4
5
6
# File 'lib/image_optim/option_definition.rb', line 4

def proc
  @proc
end

#typeObject (readonly)

Returns the value of attribute type


4
5
6
# File 'lib/image_optim/option_definition.rb', line 4

def type
  @type
end

Instance Method Details

#default_descriptionObject

Describe default value, returns string as is otherwise surrounds inspected value with backticks


34
35
36
# File 'lib/image_optim/option_definition.rb', line 34

def default_description
  default.is_a?(String) ? default : "`#{default.inspect}`"
end

#value(worker, options) ⇒ Object

Get value for worker from options


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/image_optim/option_definition.rb', line 19

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