Module: Filigree::Configuration
- Includes:
- ClassMethodsModule
- Defined in:
- lib/filigree/configuration.rb
Defined Under Namespace
Modules: ClassMethods Classes: Option
Constant Summary collapse
- HELP_OPTION =
The default help option. This can be added to your class via add_option.
Option.new('help', 'h', 'Prints this help message.', nil, Proc.new do puts "Usage: #{self.class.usage}" puts puts 'Options:' = self.class..values.sort { |a, b| a.long <=> b.long } puts Option.to_s(, 2) # Quit the application after printing the help message. exit end)
Instance Attribute Summary collapse
-
#rest ⇒ Array<String>
Remaining strings that weren’t used in configuration.
Instance Method Summary collapse
-
#dump(io = nil, *fields) ⇒ void
(also: #serialize)
Dump the state of the Configuration object.
-
#find_option(str) ⇒ Option?
Find the appropriate option object given a string.
-
#handle_array_options(argv, set_opts) ⇒ void
Configure the object from an array of strings.
-
#handle_serialized_options(overloaded, set_opts) ⇒ void
Configure the object from a serialization source.
-
#initialize(overloaded = ARGV.clone) ⇒ void
Configures the object based on the overloaded parameter.
Methods included from ClassMethodsModule
Instance Attribute Details
Instance Method Details
#dump(io, *fields) ⇒ void #dump(str, *fields) ⇒ void #dump(io, *fields) ⇒ void Also known as: serialize
This method returns an undefined value.
Dump the state of the Configuration object. This will dump the state, encoded in YAML, to different destinations depending on the io parameter.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/filigree/configuration.rb', line 55 def dump(io = nil, *fields) require 'yaml' vals = if fields.empty? then self.class..keys else fields end.inject(Hash.new) do |hash, field| hash.tap { hash[field.to_s] = self.send(field) } end case io when nil YAML.dump vals when String File.open(io, 'w') { |file| YAML.dump vals, file } when IO YAML.dump vals, io end end |
#find_option(str) ⇒ Option?
Find the appropriate option object given a string.
119 120 121 122 123 124 125 126 |
# File 'lib/filigree/configuration.rb', line 119 def find_option(str) if str[0,2] == '--' self.class.[str[2..-1]] elsif str[0,1] == '-' self.class.[str[1..-1]] end end |
#handle_array_options(argv, set_opts) ⇒ void
This method returns an undefined value.
Configure the object from an array of strings.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/filigree/configuration.rb', line 134 def (argv, set_opts) while str = argv.shift break if str == '--' if option = find_option(str) args = argv.shift(option.arity == -1 ? argv.index { |str| str[0,1] == '-' } : option.arity) case option.handler when Array tmp = args.zip(option.handler).map { |arg, sym| arg.send sym } self.send("#{option.long}=", (option.arity == 1 and tmp.length == 1) ? tmp.first : tmp) when Proc self.send("#{option.long}=", self.instance_exec(*args, &option.handler)) end set_opts << option.long end end # Save the rest of the command line for later. self.rest = argv end |
#handle_serialized_options(overloaded, set_opts) ⇒ void
This method returns an undefined value.
Configure the object from a serialization source.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/filigree/configuration.rb', line 165 def (overloaded, set_opts) = if overloaded.is_a? String if File.exists? overloaded YAML.load_file overloaded else YAML.load overloaded end else YAML.load overloaded end .each do |option, val| set_opts << option self.send "#{option}=", val end end |
#initialize(args) ⇒ void #initialize(source) ⇒ void
Configures the object based on the overloaded parameter.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/filigree/configuration.rb', line 90 def initialize(overloaded = ARGV.clone) set_opts = Array.new case overloaded when Array (overloaded, set_opts) when String, IO (overloaded, set_opts) end (self.class..keys - set_opts).each do |opt_name| default = self.class.[opt_name].default default = self.instance_exec(&default) if default.is_a? Proc self.send("#{opt_name}=", default) end # Check to make sure all the required options are set. self.class..each do |option| raise ArgumentError, "Option #{option} not set." if self.send(option).nil? end end |