Module: Newstile::Options
- Defined in:
- lib/newstile/options.rb
Overview
This module defines all options that are used by parsers and/or converters as well as providing methods to deal with the options.
Defined Under Namespace
Classes: Boolean, Definition
Constant Summary collapse
- ALLOWED_TYPES =
Allowed option types.
[String, Integer, Float, Symbol, Boolean, Array, Object]
Class Method Summary collapse
-
.defaults ⇒ Object
Return a Hash with the default values for all options.
-
.define(name, type, default, desc) ⇒ Object
Define a new option called
name
(a Symbol) with the giventype
(String, Integer, Float, Symbol, Boolean, Array, Object), default valuedefault
and the descriptiondesc
. -
.defined?(name) ⇒ Boolean
Return
true
if an option calledname
is defined. -
.definitions ⇒ Object
Return all option definitions.
-
.merge(hash) ⇒ Object
Merge the #defaults Hash with the parsed options from the given Hash, i.e.
-
.parse(name, data) ⇒ Object
Parse the given value
data
as if it was a value for the optionname
and return the parsed value with the correct type.
Class Method Details
.defaults ⇒ Object
Return a Hash with the default values for all options.
77 78 79 80 81 |
# File 'lib/newstile/options.rb', line 77 def self.defaults temp = {} @options.each {|n, o| temp[o.name] = o.default} temp end |
.define(name, type, default, desc) ⇒ Object
Define a new option called name
(a Symbol) with the given type
(String, Integer, Float, Symbol, Boolean, Array, Object), default value default
and the description desc
.
The type ‘Object’ should only be used if none of the other types suffices because such an option will be opaque and cannot be used, for example, by CLI command!
59 60 61 62 63 64 |
# File 'lib/newstile/options.rb', line 59 def self.define(name, type, default, desc) raise ArgumentError, "Option name #{name} is already used" if @options.has_key?(name) raise ArgumentError, "Invalid option type #{type} specified" if !ALLOWED_TYPES.include?(type) raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil? @options[name] = Definition.new(name, type, default, desc) end |
.defined?(name) ⇒ Boolean
Return true
if an option called name
is defined.
72 73 74 |
# File 'lib/newstile/options.rb', line 72 def self.defined?(name) @options.has_key?(name) end |
.definitions ⇒ Object
Return all option definitions.
67 68 69 |
# File 'lib/newstile/options.rb', line 67 def self.definitions @options end |
.merge(hash) ⇒ Object
Merge the #defaults Hash with the parsed options from the given Hash, i.e. only valid option names are considered and their value is run through the #parse method.
85 86 87 88 89 90 91 92 |
# File 'lib/newstile/options.rb', line 85 def self.merge(hash) temp = defaults hash.each do |k,v| next unless @options.has_key?(k) temp[k] = parse(k, v) end temp end |
.parse(name, data) ⇒ Object
Parse the given value data
as if it was a value for the option name
and return the parsed value with the correct type.
If data
already has the correct type, it is just returned. Otherwise it is converted to a String and then to the correct type.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/newstile/options.rb', line 99 def self.parse(name, data) raise ArgumentError, "No option named #{name} defined" if !@options.has_key?(name) return data if @options[name].type === data data = data.to_s if @options[name].type == String data elsif @options[name].type == Integer Integer(data) elsif @options[name].type == Float Float(data) elsif @options[name].type == Symbol (data.strip.empty? ? nil : data.to_sym) elsif @options[name].type == Boolean data.downcase.strip != 'false' && !data.empty? elsif @options[name].type == Array data.split(/\s+/) end end |