Class: Command::OgArgument
Instance Attribute Summary
Attributes inherited from Argument
#name
Instance Method Summary
collapse
Methods inherited from Argument
#basis, #check_present, #consume, #consume_hash, #match_terms, #names, #omittable?, register, #required?, #subject_requirements
Constructor Details
#initialize(name, klass, find_by = "name", options = {}) ⇒ OgArgument
Returns a new instance of OgArgument.
7
8
9
10
11
12
|
# File 'lib/command-set/og.rb', line 7
def initialize(name, klass, find_by="name", options={})
super(name)
@klass = klass
@key = find_by
@options = options
end
|
Instance Method Details
#complete(prefix, subject) ⇒ Object
14
15
16
17
|
# File 'lib/command-set/og.rb', line 14
def complete(prefix, subject)
entities = @klass.find(@options.merge({:condition => ["#{@key} like ?", prefix + "%" ]}))
return entities.map{|entity| entity.__send__(@key)}
end
|
#parse(subject, term) ⇒ Object
38
39
40
41
42
|
# File 'lib/command-set/og.rb', line 38
def parse(subject, term)
found = @klass.find(@options.merge({:condition => ["#{@key} = ?", term]}))
raise RuntimeError, "Couldn't find item on parse!" if found.empty?
found[0]
end
|
#validate(term, subject) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/command-set/og.rb', line 19
def validate(term, subject)
found = @klass.find(@options.merge({:condition => ["#{@key} = ?", term]}))
if found.empty?
if(@options[:accept_missing])
return true
elsif(@options[:create_missing])
new_attributes = @options[:default_values] || {}
new_attributes.merge!( @key => term )
@klass.create_with(new_attributes)
return true
else
return false
end
end
return true
end
|