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) }
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. -
#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.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/clive/argument.rb', line 82 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] end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
40 41 42 |
# File 'lib/clive/argument.rb', line 40 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
40 41 42 |
# File 'lib/clive/argument.rb', line 40 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
40 41 42 |
# File 'lib/clive/argument.rb', line 40 def type @type end |
Instance Method Details
#choice_str ⇒ String
Returns Choices or range of choices that can be made, for the help string.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/clive/argument.rb', line 108 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.
164 165 166 167 |
# File 'lib/clive/argument.rb', line 164 def coerce(str) return str unless str.is_a?(String) @type.typecast(str) end |
#inspect ⇒ Object
123 124 125 |
# File 'lib/clive/argument.rb', line 123 def inspect "#<#{self.class} #{to_s}>" end |
#optional? ⇒ Boolean
Returns Whether the argument is optional.
97 98 99 |
# File 'lib/clive/argument.rb', line 97 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/clive/argument.rb', line 139 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.
102 103 104 |
# File 'lib/clive/argument.rb', line 102 def to_s optional? ? "[<#@name>]" : "<#@name>" end |