Class: AppKernel::Function::Options::Option
- Defined in:
- lib/appkernel/function.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #default? ⇒ Boolean
- #greedy? ⇒ Boolean
-
#initialize(name, modifiers = {}) ⇒ Option
constructor
A new instance of Option.
- #list? ⇒ Boolean
- #required? ⇒ Boolean
- #resolve(o, single = false) ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(name, modifiers = {}) ⇒ Option
Returns a new instance of Option.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/appkernel/function.rb', line 261 def initialize(name, modifiers = {}) if name.kind_of?(Option) modifiers = modifiers.merge(name.modifiers) end @name = name.to_sym @modifiers = modifiers @index = modifiers[:index] @required = modifiers[:required] == true @lookup = modifiers[:lookup] || modifiers[:parse] @type = modifiers[:type] @list = modifiers[:list] @greedy = modifiers[:greedy] begin @default = resolve(modifiers[:default]) rescue StandardError => e raise IllegalOptionError, "invalid default value for option '#{name}': #{e.}" end end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
259 260 261 |
# File 'lib/appkernel/function.rb', line 259 def default @default end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
259 260 261 |
# File 'lib/appkernel/function.rb', line 259 def index @index end |
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
259 260 261 |
# File 'lib/appkernel/function.rb', line 259 def modifiers @modifiers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
259 260 261 |
# File 'lib/appkernel/function.rb', line 259 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
259 260 261 |
# File 'lib/appkernel/function.rb', line 259 def type @type end |
Instance Method Details
#default? ⇒ Boolean
285 286 287 |
# File 'lib/appkernel/function.rb', line 285 def default? !@default.nil? end |
#required? ⇒ Boolean
281 282 283 |
# File 'lib/appkernel/function.rb', line 281 def required? @required end |
#resolve(o, single = false) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/appkernel/function.rb', line 301 def resolve(o, single = false) if o.nil? then nil elsif @list && !single o.kind_of?(Array) ? o.map {|v| resolve(v,true)} : [resolve(o, true)] elsif @type if @type.kind_of?(Class) && o.kind_of?(@type) then o elsif @type.kind_of?(Enumerable) && @type.detect {|t| o.kind_of?(t)} then o elsif @lookup @lookup.call(o) elsif @type.respond_to?(:to_option) begin @type.to_option(o) rescue StandardError => e raise ArgumentError, "don't know how to convert #{o} into #{@type}: #{e}" end else raise ArgumentError, "don't know how to convert #{o} into #{@type}" end else @lookup ? @lookup.call(o) : o end end |
#to_sym ⇒ Object
297 298 299 |
# File 'lib/appkernel/function.rb', line 297 def to_sym @name end |