Class: Configurable
- Inherits:
-
Object
- Object
- Configurable
- Defined in:
- lib/configurable.rb
Constant Summary collapse
- @@options =
{}
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.option(name, *rest) ⇒ Object
option name, [type], [default], [options].
- .option_alias(name, name_alias) ⇒ Object
Instance Method Summary collapse
- #get_options(settings = nil, parent = nil) ⇒ Object
-
#initialize(options = {}, parent = nil) ⇒ Configurable
constructor
A new instance of Configurable.
Constructor Details
#initialize(options = {}, parent = nil) ⇒ Configurable
Returns a new instance of Configurable.
117 118 119 120 121 122 123 |
# File 'lib/configurable.rb', line 117 def initialize(={}, parent=nil) if (parent.is_a?(Configurable)) = parent. end @options= (, ) @extras= end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/configurable.rb', line 14 def @options end |
Class Method Details
.option(name, *rest) ⇒ Object
option name, [type], [default], [options]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/configurable.rb', line 70 def self.option(name, *rest) name_string= name.to_s info= { :aliases=>[name_string, name_string.gsub('_', '-'), name_string.gsub('_', ' ')].uniq } arg= rest.shift if (arg.is_a?(Class)) info[:type]= arg info[:value]= nil #arg.new arg= rest.shift end if (!arg.is_a?(Hash)) info[:value]= arg info[:type]= arg.class if !info.key?(:type) arg= rest.shift end # handle named arguments if (arg.is_a?(Hash)) info.merge!(arg) end @@options[name]= info Configurable.send :define_method, name do @options[name] end end |
.option_alias(name, name_alias) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/configurable.rb', line 105 def self.option_alias(name, name_alias) if (!@@options.key?(name)) raise ArgumentError, "No such option: #{name}" end name_alias= name_alias.to_s @@options[name][:aliases].concat([name_alias, name_alias.to_s.gsub('_', '-'), name_alias.to_s.gsub('_', ' ')].uniq) end |
Instance Method Details
#get_options(settings = nil, parent = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/configurable.rb', line 18 def (settings=nil, parent=nil) keys= @@options.keys values= @@options.map { |k,v| v[:value] } s= Struct.new(*keys).new(*values) return s if !settings setting_keys= settings.keys.map { |key| key.to_s } @@options.each { |key, value| intersect= value[:aliases] & setting_keys next if !parent && intersect.empty? if (intersect.empty?) s[key]= parent[key] next end if (intersect.length>1) raise ArgumentError, "Multiple variants for #{key.to_s} defined: #{intersect.join(", ")}" end setting_key= intersect[0] setting_value= settings[setting_key] settings.delete(setting_key) # decide if any type conversions are needed... type= value[:type] setting_value= case when FalseClass==type || TrueClass==type Boolean(setting_value) when Array==type setting_value.is_a?(String) ? setting_value.split(/\s*,\s*/) : setting_value when Fixnum==type setting_value.to_i when NilClass==type setting_value when String==type setting_value.to_s else type.new(setting_value) end s[key]= setting_value } s end |