Class: Configurable
- Inherits:
-
Object
- Object
- Configurable
- Defined in:
- lib/distil/configurable.rb
Direct Known Subclasses
Distil::Product, Distil::Project, Distil::Target, Distil::Task
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].
Instance Method Summary collapse
- #get_option(name) ⇒ Object
- #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.
127 128 129 130 131 132 133 |
# File 'lib/distil/configurable.rb', line 127 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.
16 17 18 |
# File 'lib/distil/configurable.rb', line 16 def @options end |
Class Method Details
.option(name, *rest) ⇒ Object
option name, [type], [default], [options]
68 69 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/distil/configurable.rb', line 68 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= rest.shift end if (!arg.nil? && !arg.is_a?(Hash)) info[:value]= arg info[:type]= arg.class if !info.has_key?(:type) arg= rest.shift end # handle named arguments if (arg.is_a?(Hash)) if arg.has_key?(:aliases) info[:aliases].concat(arg[:aliases]).uniq! arg.delete(:aliases) end info.merge!(arg) end if @@options.has_key?(name) orig= @@options[name] if orig.has_key?(:type) && info.has_key?(:type) && info[:type]!=orig[:type] raise ArgumentError, "Redefinition of option #{self}##{name} changes type" end if orig.has_key?(:value) && !info[:value].nil? && info[:value]!=orig[:value] raise ArgumentError, "Redefinition of option #{name} changes value" end orig[:type]||=info[:type] orig[:value]||=info[:value] orig[:aliases].concat(info[:aliases]).uniq! else @@options[name]= info end self.send :define_method, name do value=@options[name] value.respond_to?(:value_of) ? value.value_of(self) : value end self.send :define_method, "#{name}=" do |value| @options[name]= convert_type(@@options[name][:type], value) end # self.send :protected, "#{name}=".to_s end |
Instance Method Details
#get_option(name) ⇒ Object
20 21 22 23 24 |
# File 'lib/distil/configurable.rb', line 20 def get_option(name) return nil if !@options.respond_to?(name) value=@options[name] value.respond_to?(:value_of) ? value.value_of(self) : value end |
#get_options(settings = nil, parent = nil) ⇒ Object
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 |
# File 'lib/distil/configurable.rb', line 26 def (settings=nil, parent=nil) keys= @@options.keys values= @@options.map { |k,v| convert_type(v[:type], 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... setting_value= convert_type(value[:type], setting_value) if (value.has_key?(:valid_values) && !value[:valid_values].include?(setting_value)) raise ValidationError, "Invalid value for '#{setting_key}': #{setting_value}" end s[key]= setting_value } s end |