lib/lookup.rb in lookup-1.0.0.beta7 vs lib/lookup.rb in lookup-1.0.0.beta8

- old
+ new

@@ -4,53 +4,60 @@ gemfile = File.expand_path('../../Gemfile', __FILE__) require 'bundler' ENV['BUNDLE_GEMFILE'] = gemfile Bundler.require(:default) +# Because some of you don't have ActiveSupport 3.0 +# And I'm too lazy to require all the shit for 2.3.8 just for this. + +class Hash + def stringify_keys! + keys.each do |key| + self[key.to_s] = delete(key) + end + self + end +end + module Lookup - VERSION = "1.0.0.beta7" + VERSION = "1.0.0.beta8" APIS = [] class APINotFound < StandardError; end class << self + + def home + Pathname.new(ENV["HOME"]) + ".lookup" + end + def config - YAML::load_file(ENV["HOME"] + "/.lookup/config") + YAML.load_file(home + "config") end + + def apis + apis = config["apis"].stringify_keys! + end def update! - puts "Updating API listings this may take a minute or two. Please be patient!" - puts "Big APIs (like Ruby + Rails) can take some time." - 2.times { puts " " } + puts "Updating API, this may take a minute or two. Please be patient!" [Constant, Entry, Api].map { |klass| klass.delete_all } - 2.times { puts " " } - puts "Loading the following APIs:" - apis = config["apis"].values - for api in apis - puts " " + api["name"] - end - - for api in apis + puts "Updating #{apis.size} APIs." + for api in apis.values update_api!(api["name"], api["url"]) end - end def update_api!(name, url) - puts " Loading API for #{name}" + puts "Updating API for #{name}..." api = Api.find_or_create_by_name_and_url(name, url) APIS << api - - print " methods..." + puts "Updating methods for #{name}" api.update_methods! - puts " ✓" - - print " classes..." + puts "Updating classes for #{name}" api.update_classes! - puts " ✓" - - puts " ✓ #{name}" + puts "The #{name} API is done." end def find_constant(name, entry=nil, options={}) scope = options[:api].constants || Constant # Find by specific name. @@ -123,32 +130,31 @@ end methods end def search(msg, options={}) - options[:api] ||= if /^1\.9/.match(msg) - "Ruby 1.9" - elsif /^1\.8/.match(msg) - "Ruby 1.8.7" - elsif /^v([\d\.]{5})/i.match(msg) - "Rails v#{$1}" + options[:api] ||= msg.split.first + 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 !@attempted_ruby && api_check.call(options) + # Attempt a current Ruby lookup + @attempted_ruby = true + api = case RUBY_VERSION + when /^1.8/ + "1.8" + when /^1.9/ + "1.9" + end + search(msg, options.merge!(:api => api)) if api end - raise Lookup::APINotFound, %Q{You must specify a valid API as the first keyword. Included APIs: - v2.3.8 - Rails 2.3.8 - v3.0.0 - Rails 3.0.0 - 1.9 - Ruby 1.9 - 1.8 - Ruby 1.8 - - Example usage: lookup v2.3.8 ActiveRecord::Base - } if options[:api].blank? - + options[:api] = Api.find_by_name!(apis[options[:api]]["name"]) unless options[:api].is_a?(Api) - options[:api] = Api.find_by_name!(options[:api]) unless options[:api].is_a?(Api) + # We want to retain message. + msg = msg.gsub(/^(.*?)\s/, "") - msg = msg.gsub(/^(.*?)\s/, "") if options[:api] - 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) @@ -170,11 +176,19 @@ selected_output = output.select { |m| options[:api].name == m.api.name } selected_output = output if selected_output.empty? return selected_output end + + private + def api_not_found + raise Lookup::APINotFound, %Q{You must specify a valid API as the first keyword. Included APIs:\n} + + (apis.map do |short, info| + "#{short} - #{info["name"]}" + end.join("\n")) + + end end end - -require File.join(File.dirname(__FILE__), 'models') \ No newline at end of file +require File.join(File.dirname(__FILE__), 'models')