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:'

  options = self.class.options_long.values.sort { |a, b| a.long <=> b.long }
  puts Option.to_s(options, 2)

  # Quit the application after printing the help message.
  exit
end)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethodsModule

included

Instance Attribute Details

#restArray<String>



39
40
41
# File 'lib/filigree/configuration.rb', line 39

def rest
  @rest
end

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.

Overloads:

  • #dump(io, *fields) ⇒ void

    Dump the state to stdout.

  • #dump(str, *fields) ⇒ void

    Dump the state to a file.

  • #dump(io, *fields) ⇒ void

    Dump the state to the provided IO instance.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/filigree/configuration.rb', line 61

def dump(io = nil, *fields)
  require 'yaml'

  vals =
  if fields.empty? then self.class.options_long.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.



132
133
134
135
136
137
138
139
# File 'lib/filigree/configuration.rb', line 132

def find_option(str)
  if str[0,2] == '--'
    self.class.options_long[str[2..-1]]

  elsif str[0,1] == '-'
    self.class.options_short[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.

TODO: Improve the way arguments are pulled out of the ARGV array.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/filigree/configuration.rb', line 149

def handle_array_options(argv, set_opts)
  while str = argv.shift

    break if str == '--'

    if option = find_option(str)
      args = argv.shift(option.arity == -1 ? argv.index { |sub_str| sub_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.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/filigree/configuration.rb', line 180

def handle_serialized_options(overloaded, set_opts)
  options =
  if overloaded.is_a? String
    if File.exist? overloaded
      YAML.load_file overloaded
    else
      YAML.load overloaded
    end
  else
    YAML.load overloaded
  end

  options.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.

Overloads:

  • #initialize(args) ⇒ void

    Configure the object from an array of strings.

  • #initialize(source) ⇒ void

    Configure the object from a serialized source. If source is a string then it will be treated as a file name and the configuration will be loaded from the specified string. If it an IO object then that will be used as the source.



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/filigree/configuration.rb', line 96

def initialize(overloaded = ARGV.clone)
  set_opts = Array.new

  case overloaded
  when Array
    handle_array_options(overloaded.clone, set_opts)

  when String, IO
    handle_serialized_options(overloaded, set_opts)
  end

  (self.class.options_long.keys - set_opts).each do |opt_name|
    default = self.class.options_long[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.required_options.each do |option|
    raise ArgumentError, "Option #{option} not set." if self.send(option).nil?
  end

  # Initialize the auto options
  self.class.auto_blocks.each do |name, block|
    result = self.instance_exec(&block)

    self.define_singleton_method(name) { result }
  end
end