103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/model.rb', line 103
def self.create_interactively opts={}
o = self.new
args = opts[:args] || []
@fields.each do |name, field_opts|
val = if opts[:with] && opts[:with][name]
opts[:with][name]
elsif field_opts[:generator].is_a? Proc
field_opts[:generator].call(*args)
elsif field_opts[:generator]
o.send field_opts[:generator], *args
elsif field_opts[:ask] == false field_opts[:default] || (field_opts[:multi] ? [] : nil)
else
q = field_opts[:prompt] || name.to_s.ucfirst
if field_opts[:multiline]
ask_multiline q
else
default = if field_opts[:default_generator].is_a? Proc
field_opts[:default_generator].call(*args)
elsif field_opts[:default_generator]
o.send field_opts[:default_generator], *args
elsif field_opts[:default]
field_opts[:default]
end
ask q, :default => default
end
end
o.send("#{name}=", val)
end
o
end
|