Method: Ditz::ModelObject.create_interactively

Defined in:
lib/ditz/model.rb

.create_interactively(opts = {}) ⇒ Object

creates the object, prompting the user when necessary. can take a :with => { hash } parameter for pre-filling model fields.

can also take a :defaults_from => obj parameter for pre-filling model fields from another object with (some of) those fields. kinda like a bizarre interactive copy constructor.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ditz/model.rb', line 242

def create_interactively opts={}
  o = self.new
  generator_args = opts[:args] || []
  @fields.each do |name, field_opts|
    val = if opts[:with] && opts[:with][name]
      opts[:with][name]
    elsif(found, v = generate_field_value(o, field_opts, generator_args, :interactive => true)) && found
      v
    else
      q = field_opts[:prompt] || name.to_s.capitalize
      if field_opts[:multiline]
        ## multiline options currently aren't allowed to have a default
        ## value, so just ask.
        ask_multiline_smartly q
      else
        default = if opts[:defaults_from] && opts[:defaults_from].respond_to?(name) && (x = opts[:defaults_from].send(name))
          x
        else
          default = generate_field_default o, field_opts, generator_args
        end
        ask q, :default => default
      end
    end
    o.send "#{name}=", val
  end
  o
end