Skip to content

Commit

Permalink
Faster class lookup
Browse files Browse the repository at this point in the history
Change class variable to instance variable (it's already on the Module itself)
  • Loading branch information
fbacall authored and stuzart committed Jul 5, 2023
1 parent 8625ef7 commit 30e1618
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 14 additions & 4 deletions lib/seek/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.remove_rails_special_params_from(params, additional_to_remove = [])
end

def self.clear_cached
@@cache = {}
@cache = {}
end

def self.ensure_models_loaded
Expand Down Expand Up @@ -150,19 +150,29 @@ def self.python_exec(cmd)
end

def self.lookup_class(class_name, raise: true)
c = Seek::Util.persistent_classes.detect { |klass| klass.name == class_name }
c = persistent_class_lookup[class_name]
raise NameError "#{class_name} not an appropriate class" if c.nil? && raise
c
end

private

def self.persistent_class_lookup
cache('persistent_class_lookup') do
lookup = {}
persistent_classes.each do |klass|
lookup[klass.name] = klass
end
lookup
end
end

def self.cache(name, &block)
@@cache ||= {}
@cache ||= {}
if Rails.env.development? # Don't use caching in development mode
block.call
else
@@cache[name] ||= block.call
@cache[name] ||= block.call
end
end

Expand Down
22 changes: 20 additions & 2 deletions test/unit/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,31 @@ def teardown
assert Seek::Util.asset_types.include?(Workflow)
assert Seek::Util.user_creatable_types.include?(Workflow)
assert Seek::Util.searchable_types.include?(Workflow)
assert Seek::Util.lookup_class('Workflow', raise: false)

assert Seek::Util.persistent_classes.include?(Event)
assert Seek::Util.authorized_types.include?(Event)
assert Seek::Util.user_creatable_types.include?(Event)
assert Seek::Util.searchable_types.include?(Event)
assert Seek::Util.lookup_class('Event', raise: false)

assert Seek::Util.persistent_classes.include?(Sample)
assert Seek::Util.authorized_types.include?(Sample)
assert Seek::Util.asset_types.include?(Sample)
assert Seek::Util.user_creatable_types.include?(Sample)
assert Seek::Util.searchable_types.include?(Sample)
assert Seek::Util.lookup_class('Sample', raise: false)

assert Seek::Util.persistent_classes.include?(Programme)
assert Seek::Util.searchable_types.include?(Programme)
assert Seek::Util.lookup_class('Programme', raise: false)

assert Seek::Util.persistent_classes.include?(Publication)
assert Seek::Util.authorized_types.include?(Publication)
assert Seek::Util.asset_types.include?(Publication)
assert Seek::Util.user_creatable_types.include?(Publication)
assert Seek::Util.searchable_types.include?(Publication)
assert Seek::Util.lookup_class('Publication', raise: false)

with_config_value :workflows_enabled, false do
Seek::Util.clear_cached
Expand All @@ -121,6 +126,7 @@ def teardown
refute Seek::Util.asset_types.include?(Workflow)
refute Seek::Util.user_creatable_types.include?(Workflow)
refute Seek::Util.searchable_types.include?(Workflow)
assert_nil Seek::Util.lookup_class('Workflow', raise: false)
end

with_config_value :events_enabled, false do
Expand All @@ -129,6 +135,7 @@ def teardown
refute Seek::Util.authorized_types.include?(Event)
refute Seek::Util.user_creatable_types.include?(Event)
refute Seek::Util.searchable_types.include?(Event)
assert_nil Seek::Util.lookup_class('Event', raise: false)
end

with_config_value :samples_enabled, false do
Expand All @@ -138,12 +145,14 @@ def teardown
refute Seek::Util.asset_types.include?(Sample)
refute Seek::Util.user_creatable_types.include?(Sample)
refute Seek::Util.searchable_types.include?(Sample)
assert_nil Seek::Util.lookup_class('Sample', raise: false)
end

with_config_value :programmes_enabled, false do
Seek::Util.clear_cached
refute Seek::Util.persistent_classes.include?(Programme)
refute Seek::Util.searchable_types.include?(Programme)
assert_nil Seek::Util.lookup_class('Programme', raise: false)
end

with_config_value :publications_enabled, false do
Expand All @@ -153,15 +162,24 @@ def teardown
refute Seek::Util.asset_types.include?(Publication)
refute Seek::Util.user_creatable_types.include?(Publication)
refute Seek::Util.searchable_types.include?(Publication)
assert_nil Seek::Util.lookup_class('Publication', raise: false)
end

end
end
end
end
end
end



test 'lookup_class' do
assert Seek::Util.lookup_class('Publication', raise: false)
assert Seek::Util.lookup_class('Workflow', raise: false)
assert_nil Seek::Util.lookup_class('String', raise: false)
assert_nil Seek::Util.lookup_class('WorkflowInternals::Structure', raise: false)
assert_nil Seek::Util.lookup_class('gdfghdfhdfhdfhdfhdfh', raise: false)
assert_raises NameError do
Seek::Util.lookup_class('String')
end
end
end

0 comments on commit 30e1618

Please sign in to comment.