Class: Clive::Argument
- Inherits:
-
Object
- Object
- Clive::Argument
- Defined in:
- lib/clive/argument.rb
Overview
An Argument represents an argument for an Option or Command, it can be optional and can also be constricted by various other values, see #initialize.
Defined Under Namespace
Classes: AlwaysTrue
Constant Summary collapse
- DEFAULTS =
An Argument will have these traits by default.
{ :optional => false, :type => Type::Object, :match => AlwaysTrue.for(:match), :within => AlwaysTrue.for(:include?), :default => nil, :constraint => AlwaysTrue.for(:call), :infinite => false }
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#choice_str ⇒ String
Choices or range of choices that can be made, for the help string.
-
#coerce(str) ⇒ Object
Converts the given String argument to the correct type determined by the
:type
passed to #initialize. -
#infinite? ⇒ Boolean
Whether the argument is infinite.
-
#initialize(name, opts = {}) ⇒ Argument
constructor
A new instance of Argument.
- #inspect ⇒ Object
-
#optional? ⇒ Boolean
Whether the argument is optional.
-
#possible?(obj) ⇒ Boolean
Determines whether the object given can be this argument.
-
#to_s ⇒ String
String representation for the argument.
Constructor Details
#initialize(name, opts = {}) ⇒ Argument
A new instance of Argument.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/clive/argument.rb', line 83 def initialize(name, opts={}) @name = name.to_sym opts[:constraint] = opts[:constraint].to_proc if opts[:constraint].respond_to?(:to_proc) opts = DEFAULTS.merge(opts) @optional = opts[:optional] @type = Type.find_class(opts[:type].to_s) rescue opts[:type] @match = opts[:match] @within = opts[:within] @default = opts[:default] @constraint = opts[:constraint] @infinite = opts[:infinite] end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
41 42 43 |
# File 'lib/clive/argument.rb', line 41 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
41 42 43 |
# File 'lib/clive/argument.rb', line 41 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
41 42 43 |
# File 'lib/clive/argument.rb', line 41 def type @type end |
Instance Method Details
#choice_str ⇒ String
Returns Choices or range of choices that can be made, for the help string.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/clive/argument.rb', line 115 def choice_str if @within case @within when Array '(' + @within.join(', ') + ')' when Range '(' + @within.to_s + ')' else '' end else '' end end |
#coerce(str) ⇒ Object
Converts the given String argument to the correct type determined by the :type
passed to #initialize.
171 172 173 174 |
# File 'lib/clive/argument.rb', line 171 def coerce(str) return str unless str.is_a?(String) @type.typecast(str) end |
#infinite? ⇒ Boolean
Returns Whether the argument is infinite.
104 105 106 |
# File 'lib/clive/argument.rb', line 104 def infinite? @infinite end |
#inspect ⇒ Object
130 131 132 |
# File 'lib/clive/argument.rb', line 130 def inspect "#<#{self.class} #{to_s}>" end |
#optional? ⇒ Boolean
Returns Whether the argument is optional.
99 100 101 |
# File 'lib/clive/argument.rb', line 99 def optional? @optional end |
#possible?(obj) ⇒ Boolean
Determines whether the object given can be this argument. Checks whether it is valid based on the options passed to #initialize.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/clive/argument.rb', line 146 def possible?(obj) return false if obj.is_a?(String) && !@type.valid?(obj) return false unless @match.match(obj.to_s) coerced = coerce(obj) unless @within.include?(obj.to_s) || @within.include?(coerced) return false end begin return false unless @constraint.call(obj.to_s) rescue begin return false unless @constraint.call(coerced) rescue return false end end true end |
#to_s ⇒ String
Returns String representation for the argument.
109 110 111 |
# File 'lib/clive/argument.rb', line 109 def to_s (optional? ? "[<#@name>]" : "<#@name>") + (infinite? ? '...' : '') end |