diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..608d28f4 --- /dev/null +++ b/.rspec @@ -0,0 +1,4 @@ +--color +--order rand +--require spec_helper +--warning diff --git a/Rakefile b/Rakefile index c8f346b8..ab29abd3 100644 --- a/Rakefile +++ b/Rakefile @@ -14,10 +14,7 @@ RuboCop::RakeTask.new(:rubocop) do |task| task.options += ['--force-exclusion', 'lib/one_gadget/builds/*.rb'] end -RSpec::Core::RakeTask.new(:spec) do |task| - task.pattern = './spec/**/*_spec.rb' - task.rspec_opts = ['--color', '--require spec_helper', '--order rand', '--warning'] -end +RSpec::Core::RakeTask.new(:spec) YARD::Rake::YardocTask.new(:doc) do |t| t.files = Dir['lib/**/*.rb'] - Dir['lib/one_gadget/builds/*.rb'] diff --git a/lib/one_gadget/gadget.rb b/lib/one_gadget/gadget.rb index 8fcb4b5e..264861c7 100644 --- a/lib/one_gadget/gadget.rb +++ b/lib/one_gadget/gadget.rb @@ -81,7 +81,7 @@ def calculate_score(cons) module ClassMethods # Path to the pre-build files. BUILDS_PATH = File.join(__dir__, 'builds').freeze - # Cache. + # Record. BUILDS = Hash.new { |h, k| h[k] = [] } # Get gadgets from pre-defined corpus. # @param [String] build_id Desired build id. @@ -89,8 +89,8 @@ module ClassMethods # When local not found, try search in latest version? # @return [Array?] Gadgets. def builds(build_id, remote: true) - require_all if BUILDS.empty? - return BUILDS[build_id] if BUILDS.key?(build_id) + ret = find_build(build_id) + return ret unless ret.nil? return build_not_found unless remote # fetch remote builds @@ -145,10 +145,11 @@ def add(build_id, offset, **options) private - def require_all - Dir.glob(File.join(BUILDS_PATH, '**', '*.rb')).each do |dic| + def find_build(id) + Dir.glob(File.join(BUILDS_PATH, "*-#{id}.rb")).each do |dic| require dic end + BUILDS[id] if BUILDS.key?(id) end def build_not_found