Skip to content

Commit

Permalink
Speed up (david942j#68)
Browse files Browse the repository at this point in the history
* Remove `require_all`
* Move rspec options to .rspec
  • Loading branch information
david942j authored and Guba Dániel Olivér committed Apr 2, 2019
1 parent d153b0e commit 813b76d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .rspec
@@ -0,0 +1,4 @@
--color
--order rand
--require spec_helper
--warning
5 changes: 1 addition & 4 deletions Rakefile
Expand Up @@ -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']
Expand Down
11 changes: 6 additions & 5 deletions lib/one_gadget/gadget.rb
Expand Up @@ -81,16 +81,16 @@ 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.
# @param [Boolean] remote
# When local not found, try search in latest version?
# @return [Array<Gadget::Gadget>?] 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 813b76d

Please sign in to comment.