Skip to content

Commit

Permalink
Memoize all powers, not just ActiveRecord relations
Browse files Browse the repository at this point in the history
  • Loading branch information
triskweline committed Sep 5, 2017
1 parent 828ee4a commit da379db
Show file tree
Hide file tree
Showing 4 changed files with 36,397 additions and 12 deletions.
25 changes: 19 additions & 6 deletions lib/consul/power.rb
Expand Up @@ -63,8 +63,6 @@ def singularize_power_name(name)
self.class.singularize_power_name(name)
end



module ClassMethods
include Consul::Power::DynamicAccess::ClassMethods

Expand Down Expand Up @@ -109,16 +107,34 @@ def define_query_and_bang_methods(name, &query)
query_method = "#{name}?"
bang_method = "#{name}!"
define_method(query_method, &query)
memoize query_method
define_method(bang_method) { |*args| send(query_method, *args) or powerless!(name, *args) }
# We don't memoize the bang method since memoizer can't memoize a thrown exception
end

def define_ids_method(name)
ids_method = power_ids_name(name)
define_method(ids_method) { |*args| default_power_ids(name, *args) }
# Memoize `ids_method` in addition to the collection method itself, since
# #default_include_object? directly accesses `ids_method`.
memoize ids_method
end

def define_main_method(name, &block)
define_method(name, &block)
memoize name
end

def define_power(name, &block)
name = name.to_s
if name.ends_with?('?')
# The developer is trying to register an optimized query method
# for singular object queries.
name_without_suffix = name.chop
define_query_and_bang_methods(name_without_suffix, &block)
else
define_method(name, &block)
define_main_method(name, &block)
define_ids_method(name)
define_query_and_bang_methods(name) { |*args| default_include_power?(name, *args) }
begin
singular = singularize_power_name(name)
Expand All @@ -127,9 +143,6 @@ def define_power(name, &block)
# We do not define singularized power methods if it would
# override the collection method
end
ids_method = power_ids_name(name)
define_method(ids_method) { |*args| default_power_ids(name, *args) }
memoize ids_method
end
name
end
Expand Down

0 comments on commit da379db

Please sign in to comment.