class Tempo::Controllers::Base
Public Class Methods
filter_projects_by_title(options, args)
click to toggle source
# File lib/tempo/controllers/base.rb, line 6 def filter_projects_by_title(options, args) if options[:exact] match = reassemble_the args match = [match] model_match @projects, match, "title", :exact else model_match @projects, args, "title", :fuzzy end end
fuzzy_match(haystack, matches, attribute="id")
click to toggle source
Takes an array of source strings and filters them down to the ones that match positively against every member of the matches array
# File lib/tempo/controllers/base.rb, line 21 def fuzzy_match(haystack, matches, attribute="id") matches = [matches] unless matches.is_a? Array if haystack.is_a? Array fuzzy_array_match( haystack, matches ) elsif haystack.superclass == Model::Base model_match( haystack, matches, attribute ) end end
reassemble_the(args, flag=nil)
click to toggle source
Gli default behavior: When args are sent in a command without quotes, they are broken into an array, and the first block is passed to a flag if present.
Here we reassemble the string, and add value stored in a flag in the front. The value is also added back intto the front of the original array
# File lib/tempo/controllers/base.rb, line 41 def reassemble_the(args, flag=nil) assembled = "" args.unshift flag if flag args.each { |a| assembled += " #{a}" } assembled.strip! end
Private Class Methods
fuzzy_array_match(haystack, matches)
click to toggle source
# File lib/tempo/controllers/base.rb, line 60 def fuzzy_array_match(haystack, matches) results = [] matches.each do |m| reg = match_to_regex m haystack.each do |h| results << h if reg.match h end haystack = results results = [] end haystack end
match_project(command, options, args)
click to toggle source
# File lib/tempo/controllers/base.rb, line 88 def match_project(command, options, args) if options[:id] match = @projects.find_by_id args[0] Views::no_match_error( "projects", "id=#{args[0]}" ) if not match else matches = filter_projects_by_title options, args request = reassemble_the args match = single_match matches, request, command end match end
match_to_regex(match, type=:fuzzy)
click to toggle source
TODO: escape regex characters ., (), etc.
# File lib/tempo/controllers/base.rb, line 51 def match_to_regex(match, type=:fuzzy) match.downcase! if type == :exact /^#{match}$/ else /#{match}/ end end
model_match(haystack, matches, attribute, type=:fuzzy)
click to toggle source
# File lib/tempo/controllers/base.rb, line 73 def model_match(haystack, matches, attribute, type=:fuzzy) attribute = "@#{attribute}".to_sym contenders = haystack.index results = [] matches.each do |m| reg = match_to_regex m, type contenders.each do |c| results << c if reg.match c.instance_variable_get(attribute).to_s.downcase end contenders = results results = [] end contenders end
single_match(matches, request, command)
click to toggle source
verify one and only one match returned in match array returns the single match
# File lib/tempo/controllers/base.rb, line 102 def single_match(matches, request, command) if matches.length == 0 Views::no_match_error "projects", request return false elsif matches.length > 1 Views::ambiguous_project matches, command return false else match = matches[0] end end