From e6bdc3ca32f86454d4d1239e54e1a1d350d98d0e Mon Sep 17 00:00:00 2001 From: david942j Date: Wed, 6 Mar 2019 19:55:41 -0800 Subject: [PATCH 1/2] Speed up - 0.3s -> 0.2s --- lib/one_gadget/gadget.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 From dfd1e893bde4128992d7ba538a713b21cc5a509c Mon Sep 17 00:00:00 2001 From: david942j Date: Wed, 6 Mar 2019 20:55:25 -0800 Subject: [PATCH 2/2] Move rspec options to .rspec --- .rspec | 4 ++++ Rakefile | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 .rspec 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']