Module: Ramaze::Option::Merger
- Included in:
- Holder
- Defined in:
- lib/ramaze/option/merger.rb
Instance Method Summary collapse
-
#merge!(obj, &block) ⇒ Object
Pass it anything that may work as option: ENV, hashes, ARGV…
-
#merge_cli!(obj, &block) ⇒ Object
Only destructive for now, should non-destructive keep both Global and the obj original?.
-
#merge_prefixed!(obj, prefix = 'ramaze_') ⇒ Object
Prefixed options, mostly useful for filtering out from ENV.
- #option_parser(bin = 'ramaze') ⇒ Object
Instance Method Details
#merge!(obj, &block) ⇒ Object
Pass it anything that may work as option: ENV, hashes, ARGV… it will figure out how to get config from it and modify Global.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ramaze/option/merger.rb', line 7 def merge!(obj, &block) case obj when ENV merge_prefixed!(obj) else if obj.respond_to?(:each_pair) obj.each_pair do |key, value| self[key] = value end elsif obj.respond_to?(:each) merge_cli!(obj, &block) end end self end |
#merge_cli!(obj, &block) ⇒ Object
Only destructive for now, should non-destructive keep both Global and the obj original?
42 43 44 |
# File 'lib/ramaze/option/merger.rb', line 42 def merge_cli!(obj, &block) option_parser(&block).parse!(obj) end |
#merge_prefixed!(obj, prefix = 'ramaze_') ⇒ Object
Prefixed options, mostly useful for filtering out from ENV. by default the prefix is case-insensitive and set to ramaze_ - that works for keys like:
$ RAMAZE_PORT=80 ruby start.rb
$ ramaze_port=80 ruby start.rb
$ Ramaze_Port=80 ruby start.rb
30 31 32 33 34 35 36 37 |
# File 'lib/ramaze/option/merger.rb', line 30 def merge_prefixed!(obj, prefix = 'ramaze_') opts = {} obj.each do |key, value| next unless key =~ /^#{prefix}(.*)/i opts[$1.downcase] = value end merge! opts end |
#option_parser(bin = 'ramaze') ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 |
# File 'lib/ramaze/option/merger.rb', line 46 def option_parser(bin = 'ramaze') require 'optparse' require 'abbrev' OptionParser.new{|opt| ruby_version = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" ramaze_version = "Ramaze Version #{VERSION}" version = "#{ramaze_version}, on #{ruby_version}" opt. = "Usage: #{bin} start.rb [OPTIONS]" opt.define_head version yield(opt) if block_given? opt.separator '' opt.separator 'Global options, value in [] shows default.' Global.each do |key, value| long_option = "--#{key}" = Global.trait.fetch(key, {}) doc, cli, short = .values_at(:doc, :cli, :short) short_option = "-#{short}" if short next unless cli or short case cli || value when Integer option = [short_option, "#{long_option} NUM", Integer, doc] when String, Symbol option = [short_option, "#{long_option} STRING", String, doc] when Array next unless cli cli = cli.map{|c| c.to_s } list = " " << cli.join(', ') doc = "[#{value}] #{doc}" aliases = cli.abbrev option = [short_option, "#{long_option} CHOICE", aliases, doc, list] else option = [short_option, long_option, doc] end opt.on(*option){|o| Ramaze::Global[key] = o } end opt.separator '' opt.separator 'Common options:' opt.on('-h', '--help', 'Show this message') do puts opt exit end opt.on('-v', '--version', "Show version") do puts version exit end } end |