Class: Choice::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/choice/option.rb

Overview

The Option class parses and stores all the information about a specific option.

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

CHOICES =

Since we define getters/setters on the fly, we need a white list of which to accept. Here’s the list.

%w[short long desc default filter action cast validate valid]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Option

You can instantiate an option on its own or by passing it a name and a block. If you give it a block, it will eval() the block and set itself up nicely.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/choice/option.rb', line 14

def initialize(options = {}, &block)
  # Here we store the definitions this option contains, to make to_a and
  # to_h easier.
  @choices = []      

  # If we got a block, eval it and set everything up.
  instance_eval(&block) if block_given?      

  # Is this option required?
  @required = options[:required] || false
  @choices << 'required'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This is the catch all for the getter/setter choices defined in CHOICES. It also gives us choice? methods.

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/choice/option.rb', line 29

def method_missing(method, *args, &block)
  # Get the name of the choice we want, as a class variable string.
  var = "@#{method.to_s.sub('?','')}"

  # To string, for regex purposes.
  method = method.to_s
  
  # Don't let in any choices not defined in our white list array.
  raise ParseError, "I don't know `#{method}'" unless CHOICES.include? method.sub('?','')
  
  # If we're asking a question, give an answer.  Like 'short?'.
  return !!instance_variable_get(var) if method =~ /\?/ 
  
  # If we were called with no arguments, we want a get.
  return instance_variable_get(var) unless args[0] || block_given?
  
  # If we were given a block or an argument, save it.
  instance_variable_set(var, args[0]) if args[0]
  instance_variable_set(var, block) if block_given?
  
  # Add the choice to the @choices array if we're setting it for the first
  # time.
  @choices << method if args[0] || block_given? unless @choices.index(method)
end

Instance Method Details

#desc(string = nil) ⇒ Object

The desc method is slightly special: it stores itself as an array and each subsequent call adds to that array, rather than overwriting it. This is so we can do multi-line descriptions easily.



57
58
59
60
61
62
63
64
65
# File 'lib/choice/option.rb', line 57

def desc(string = nil)
  return @desc if string.nil?

  @desc ||= []
  @desc.push(string)

  # Only add to @choices array if it's not already present.
  @choices << 'desc' unless @choices.index('desc')
end

#desc?Boolean

Simple, desc question method.

Returns:

  • (Boolean)


68
# File 'lib/choice/option.rb', line 68

def desc?() !!@desc end

#to_aObject

Returns Option converted to an array.



71
72
73
74
75
76
# File 'lib/choice/option.rb', line 71

def to_a
  @choices.inject([]) do |array, choice|
    return array unless @choices.include? choice
    array + [instance_variable_get("@#{choice}")]
  end
end

#to_hObject

Returns Option converted to a hash.



79
80
81
82
83
84
# File 'lib/choice/option.rb', line 79

def to_h
  @choices.inject({}) do |hash, choice|
    return hash unless @choices.include? choice
    hash.merge choice => instance_variable_get("@#{choice}") 
  end
end