Class: Samovar::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Option.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/samovar/options.rb', line 25

def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, &block)
	@flags = Flags.new(flags)
	@description = description
	
	if key
		@key = key
	else
		@key = @flags.first.key
	end
	
	@default = default
	
	@value = value
	@value ||= true if @flags.boolean?
	
	@type = block_given? ? block : type
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



47
48
49
# File 'lib/samovar/options.rb', line 47

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



44
45
46
# File 'lib/samovar/options.rb', line 44

def description
  @description
end

#flagsObject (readonly)

Returns the value of attribute flags.



43
44
45
# File 'lib/samovar/options.rb', line 43

def flags
  @flags
end

#keyObject (readonly)

Returns the value of attribute key.



49
50
51
# File 'lib/samovar/options.rb', line 49

def key
  @key
end

#typeObject (readonly)

Returns the value of attribute type.



45
46
47
# File 'lib/samovar/options.rb', line 45

def type
  @type
end

Instance Method Details

#coerce(result) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/samovar/options.rb', line 51

def coerce(result)
	if @type
		@type.call(result)
	else
		result
	end
end

#parse(input) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/samovar/options.rb', line 59

def parse(input)
	if result = @flags.parse(input)
		@value.nil? ? coerce(result) : @value
	else
		@default
	end
end

#to_aObject



71
72
73
74
75
76
77
# File 'lib/samovar/options.rb', line 71

def to_a
	unless @default.nil?
		[@flags, @description, "Default: #{@default}"]
	else
		[@flags, @description]
	end
end

#to_sObject



67
68
69
# File 'lib/samovar/options.rb', line 67

def to_s
	@flags
end