Class: Tap::Support::Configuration
- Defined in:
- lib/tap/support/configuration.rb
Overview
Represents a configuration declared by a Configurable class.
Constant Summary collapse
- SHORT_OPTION =
Matches a short option
/^-[A-z]$/
- LONG_OPTION =
Matches a long option
/^--(\[no-\])?([A-z][\w-]*)$/
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
An array of optional metadata for self.
-
#duplicable ⇒ Object
readonly
True if the default value may be duplicated.
-
#name ⇒ Object
readonly
The name of the configuration.
-
#reader ⇒ Object
The reader method, by default name.
-
#writer ⇒ Object
The writer method, by default name=.
Class Method Summary collapse
-
.longify(str, switch_notation = false, hyphenize = true) ⇒ Object
Turns the input string into a long-format option.
-
.shortify(str) ⇒ Object
Turns the input string into a short-format option.
Instance Method Summary collapse
-
#==(another) ⇒ Object
True if another is a kind of Configuration with the same name, default value, reader and writer; other attributes are NOT taken into account.
-
#arg_name ⇒ Object
The argument name for self: either attributes or name.to_s.upcase.
-
#arg_type ⇒ Object
or :mandatory.
-
#default(duplicate = true) ⇒ Object
Returns the default value, or a duplicate of the default value if specified and the default value is duplicable.
-
#default=(value) ⇒ Object
Sets the default value for self and determines if the default is duplicable.
-
#desc ⇒ Object
The description for self: attributes.
-
#initialize(name, default = nil, options = {}) ⇒ Configuration
constructor
Initializes a new Configuration with the specified name and default value.
-
#long(switch_notation = false, hyphenize = true) ⇒ Object
The long version of name.
-
#short ⇒ Object
The short version of name.
-
#to_optparse_argv ⇒ Object
Returns self as an argv that can be used to register an option with OptionParser.
Constructor Details
#initialize(name, default = nil, options = {}) ⇒ Configuration
Initializes a new Configuration with the specified name and default value. Options may specify an alternate reader/writer; any additional options are set as attributes.
69 70 71 72 73 74 75 76 |
# File 'lib/tap/support/configuration.rb', line 69 def initialize(name, default=nil, ={}) @name = name self.default = default self.reader = .has_key?(:reader) ? .delete(:reader) : name self.writer = .has_key?(:writer) ? .delete(:writer) : "#{name}=" @attributes = end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
An array of optional metadata for self
64 65 66 |
# File 'lib/tap/support/configuration.rb', line 64 def attributes @attributes end |
#duplicable ⇒ Object (readonly)
True if the default value may be duplicated
61 62 63 |
# File 'lib/tap/support/configuration.rb', line 61 def duplicable @duplicable end |
#name ⇒ Object (readonly)
The name of the configuration
52 53 54 |
# File 'lib/tap/support/configuration.rb', line 52 def name @name end |
#reader ⇒ Object
The reader method, by default name
55 56 57 |
# File 'lib/tap/support/configuration.rb', line 55 def reader @reader end |
#writer ⇒ Object
The writer method, by default name=
58 59 60 |
# File 'lib/tap/support/configuration.rb', line 58 def writer @writer end |
Class Method Details
.longify(str, switch_notation = false, hyphenize = true) ⇒ Object
Turns the input string into a long-format option. Raises an error if the option does not match LONG_REGEXP.
Configuration.longify("--opt") # => '--opt'
Configuration.longify(:opt) # => '--opt'
Configuration.longify(:opt, true) # => '--[no-]opt'
Configuration.longify(:opt_ion) # => '--opt-ion'
Configuration.longify(:opt_ion, false, false) # => '--opt_ion'
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tap/support/configuration.rb', line 36 def longify(str, switch_notation=false, hyphenize=true) str = str.to_s str = "--#{str}" unless str.index("--") str.gsub!(/_/, '-') if hyphenize raise "invalid long option: #{str}" unless str =~ LONG_OPTION if switch_notation && $1.nil? str = "--[no-]#{$2}" end str end |
.shortify(str) ⇒ Object
Turns the input string into a short-format option. Raises an error if the option does not match SHORT_REGEXP.
Configuration.shortify("-o") # => '-o'
Configuration.shortify(:o) # => '-o'
17 18 19 20 21 22 |
# File 'lib/tap/support/configuration.rb', line 17 def shortify(str) str = str.to_s str = "-#{str}" unless str[0] == ?- raise "invalid short option: #{str}" unless str =~ SHORT_OPTION str end |
Instance Method Details
#==(another) ⇒ Object
True if another is a kind of Configuration with the same name, default value, reader and writer; other attributes are NOT taken into account.
139 140 141 142 143 144 145 |
# File 'lib/tap/support/configuration.rb', line 139 def ==(another) another.kind_of?(Configuration) && self.name == another.name && self.reader == another.reader && self.writer == another.writer && self.default(false) == another.default(false) end |
#arg_name ⇒ Object
The argument name for self: either attributes or name.to_s.upcase
111 112 113 |
# File 'lib/tap/support/configuration.rb', line 111 def arg_name attributes[:arg_name] || name.to_s.upcase end |
#arg_type ⇒ Object
or :mandatory
117 118 119 |
# File 'lib/tap/support/configuration.rb', line 117 def arg_type attributes[:arg_type] || :mandatory end |
#default(duplicate = true) ⇒ Object
Returns the default value, or a duplicate of the default value if specified and the default value is duplicable.
93 94 95 |
# File 'lib/tap/support/configuration.rb', line 93 def default(duplicate=true) duplicate && duplicable ? @default.dup : @default end |
#default=(value) ⇒ Object
Sets the default value for self and determines if the default is duplicable. Non-duplicable values include nil, true, false, Symbol, Numeric, and any object that does not respond to dup.
82 83 84 85 86 87 88 89 |
# File 'lib/tap/support/configuration.rb', line 82 def default=(value) @duplicable = case value when nil, true, false, Symbol, Numeric, Method then false else value.respond_to?(:dup) end @default = value.freeze end |
#desc ⇒ Object
The description for self: attributes
132 133 134 |
# File 'lib/tap/support/configuration.rb', line 132 def desc attributes[:desc] end |
#long(switch_notation = false, hyphenize = true) ⇒ Object
The long version of name.
122 123 124 |
# File 'lib/tap/support/configuration.rb', line 122 def long(switch_notation=false, hyphenize=true) Configuration.longify(attributes[:long] || name.to_s, switch_notation, hyphenize) end |
#short ⇒ Object
The short version of name.
127 128 129 |
# File 'lib/tap/support/configuration.rb', line 127 def short attributes[:short] ? Configuration.shortify(attributes[:short]) : nil end |
#to_optparse_argv ⇒ Object
Returns self as an argv that can be used to register an option with OptionParser.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/tap/support/configuration.rb', line 149 def to_optparse_argv argtype = case arg_type when :optional "#{long} [#{arg_name}]" when :switch long(true) when :flag long when :list "#{long} a,b,c" when :mandatory, nil "#{long} #{arg_name}" else raise "unknown arg_type: #{arg_type}" end [short, argtype, desc].compact end |