Class: Samovar::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option

Returns a new instance of Option.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/samovar/option.rb', line 10

def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block)
	@flags = Flags.new(flags)
	@description = description
	
	if key
		@key = key
	else
		@key = @flags.first.key
	end
	
	@default = default
	
	# If the value is given, it overrides the user specified input.
	@value = value
	@value ||= true if @flags.boolean?
	
	@type = type
	@required = required
	@block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



40
41
42
# File 'lib/samovar/option.rb', line 40

def block
  @block
end

#defaultObject (readonly)

Returns the value of attribute default.



34
35
36
# File 'lib/samovar/option.rb', line 34

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



32
33
34
# File 'lib/samovar/option.rb', line 32

def description
  @description
end

#flagsObject (readonly)

Returns the value of attribute flags.



31
32
33
# File 'lib/samovar/option.rb', line 31

def flags
  @flags
end

#keyObject (readonly)

Returns the value of attribute key.



33
34
35
# File 'lib/samovar/option.rb', line 33

def key
  @key
end

#requiredObject (readonly)

Returns the value of attribute required.



39
40
41
# File 'lib/samovar/option.rb', line 39

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/samovar/option.rb', line 38

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



36
37
38
# File 'lib/samovar/option.rb', line 36

def value
  @value
end

Instance Method Details

#coerce(result) ⇒ Object



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

def coerce(result)
	if @type
		result = coerce_type(result)
	end
	
	if @block
		result = @block.call(result)
	end
	
	return result
end

#coerce_type(result) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/samovar/option.rb', line 42

def coerce_type(result)
	if @type == Integer
		Integer(result)
	elsif @type == Float
		Float(result)
	elsif @type == Symbol
		result.to_sym
	elsif @type.respond_to? :call
		@type.call(result)
	elsif @type.respond_to? :new
		@type.new(result)
	end
end

#parse(input, parent = nil, default = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/samovar/option.rb', line 68

def parse(input, parent = nil, default = nil)
	result = @flags.parse(input)
	if result != nil
		@value.nil? ? coerce(result) : @value
	elsif default ||= @default
		return default
	elsif @required
		raise MissingValueError.new(parent, self)
	end
end

#to_aObject



83
84
85
86
87
88
89
90
91
# File 'lib/samovar/option.rb', line 83

def to_a
	if @default
		[@flags, @description, "(default: #{@default})"]
	elsif @required
		[@flags, @description, "(required)"]
	else
		[@flags, @description]
	end
end

#to_sObject



79
80
81
# File 'lib/samovar/option.rb', line 79

def to_s
	@flags
end