Module: Lookup

Defined in:
lib/lookup.rb,
lib/lookup/models.rb,
lib/lookup/version.rb

Defined Under Namespace

Classes: APINotFound, Api, Constant, Entry, LookupBase, MissingHome

Constant Summary collapse

APIS =
[]
VERSION =
"1.1.4"

Class Method Summary collapse

Class Method Details

.apisObject



34
35
36
# File 'lib/lookup.rb', line 34

def apis
  apis = config["apis"].stringify_keys!
end

.build_regex_from_constant(name) ⇒ Object

this uses a regex to lock down our SQL finds even more so that things like AR::Base will not match ActiveRecord::ConnectionAdapters::DatabaseStatements



86
87
88
89
90
91
# File 'lib/lookup.rb', line 86

def build_regex_from_constant(name)
  parts=name.split("::").map do |c|
    c.split("").join("[^:]*")+"[^:]*"
  end
  /#{parts.join("::")}/i
end

.configObject



30
31
32
# File 'lib/lookup.rb', line 30

def config
  YAML.load_file(home + "config")
end

.find_constant(name, entry = nil, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lookup.rb', line 63

def find_constant(name, entry=nil, options={})
  scope = options[:api].constants || Constant
  # Find by specific name.
  constants = scope.find_all_by_name(name, :include => "entries")
  # search for class methods, which is prolly what we want if we can find it
  constants = scope.find_all_by_name("#{name}::ClassMethods", :include => "entries") if constants.empty?
  # Find by name beginning with <blah>.
  constants = scope.all(:conditions => ["constants.name LIKE ?", name + "%"], :include => "entries") if constants.empty?
  # Find by fuzzy.
  match="%#{name.split("").join("%")}%"
  constants = scope.find_by_sql("select * from constants where name LIKE '#{match}'") if constants.empty?
  regex=build_regex_from_constant(name)
  constants = constants.select { |x| x.name =~ regex }
  # Narrow it down to the constants that only contain the entry we are looking for.
  if entry
    constants = constants.select { |constant| !constant.entries.find_by_name(entry).nil? }
  end
  constants
end

.find_method(name, constant = nil, options = {}) ⇒ Object

Find an entry. If the constant argument is passed, look it up within the scope of the constant.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lookup.rb', line 114

def find_method(name, constant=nil, options = {})
  scope = options[:api].entries || Entry
  methods = []
  # Full match
  methods = scope.find_all_by_name(name.to_s)
  # Start match
  methods = scope.all(:conditions => ["entries.name LIKE ?", name.to_s + "%"]) if methods.empty?
  # Wildcard substitution
  methods = scope.find_by_sql("select * from entries where name LIKE '#{name.to_s.gsub("*", "%")}'") if methods.empty?
  # Fuzzy match
  methods = scope.find_by_sql("select * from entries where name LIKE '%#{name.to_s.split("").join("%")}%'") if methods.empty?
  
  # Weight the results, last result is the first one we want shown first
  methods = methods.sort_by(&:weighting)

  if constant
    constants = find_constant(constant, nil, options)
    methods = methods.select { |m| constants.include?(m.constant) }
  end
  methods
end

.homeObject



26
27
28
# File 'lib/lookup.rb', line 26

def home
  Pathname.new(ENV["HOME"]) + ".lookup"
end

.search(msg, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lookup.rb', line 136

def search(msg, options={})
  first_word = msg.split.first
  options[:api] ||= first_word if apis.keys.include?(first_word)
  api_check = lambda { |options| (!apis.keys.map(&:to_s).include?(options[:api]) && !options[:api].is_a?(Api)) }
  # to_s because yaml interprets "1.8" as a literal 1.8
  # And because I'm super, super lazy
  if api_check.call(options)
    # Attempt a current Ruby lookup
    api = case RUBY_VERSION
      when /^1.8/
        "1.8"
      when /^1.9/
        "1.9"
    end
    search(msg, options.merge!(:api => api)) if api 
  end
  
  options[:api] = Api.find_by_name!(apis[options[:api]]["name"]) if (options[:api] && apis[options[:api]].try(:[], "name")) && !options[:api].is_a?(Api)
  
  raise Lookup::APINotFound, "Could not determine what API the lookup is for" unless options[:api]
  
  # We want to retain message.
  msg = msg.gsub(/^(.*?)\s/, "")
  
  splitter = options[:splitter] || "#"
  parts = msg.split(" ")[0..-1].flatten.map { |a| a.split(splitter) }.flatten!
  # It's a constant! Oh... and there's nothing else in the string!
  first = smart_rails_constant_substitutions(parts.first)
  
  output = if /^[A-Z]/.match(first) && parts.size == 1
    find_constant(first, nil, options)
   # It's a method!
  else
    # Right, so they only specified one argument. Therefore, we look everywhere.
    if parts.size == 1
      o = find_method(parts.last, nil, options)
    # Left, so they specified two arguments. First is probably a constant, so let's find that!
    else
      o = find_method(parts.last, first, options)
    end
    o
  end

  output = search(msg, options.merge(:splitter => ".")) if output.empty? && splitter != "."
  selected_output = output.select { |m| options[:api].name == m.api.name }
  selected_output = output if selected_output.empty?

  return selected_output
end

.smart_rails_constant_substitutions(name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lookup.rb', line 93

def smart_rails_constant_substitutions(name)
  parts = name.split("::").map { |x| x.split(":")}.flatten
  if parts.first
    rep = case parts.first.downcase
      # so it falls back on fuzzy and matches AR as well as ActiveResource
      when "ar" then "ActiveRecord" 
      when "ares" then "ActiveResource" 
      when "am" then "ActionMailer"
      when "as" then "ActiveSupport"
      when "ac" then "ActionController"
      when "av" then "ActionView"
      else 
        parts.first
    end
    return ([rep] + parts[1..-1]).join("::")
  end
  name
end

.update!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lookup.rb', line 38

def update!
  FileUtils.mkdir_p(home)
  if !File.exists?(home + "config")
    FileUtils.cp(File.dirname(__FILE__) + "/../config/lookup", home + "config")
  end
  
  puts "Updating API, this may take a minute or two. Please be patient!"
  [Constant, Entry, Api].map { |klass| klass.delete_all }
  puts "Updating #{apis.size} APIs."
  for api in apis.values
    update_api!(api["name"], api["url"])
  end
end

.update_api!(name, url) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/lookup.rb', line 52

def update_api!(name, url)
  puts "Updating API for #{name}..."
  api = Api.find_or_create_by_name_and_url(name, url)
  APIS << api
  puts "Updating methods for #{name}"
  api.update_methods!
  puts "Updating classes for #{name}"
  api.update_classes!
  puts "The #{name} API is done."
end