lib/consul/power.rb in consul-0.7.0 vs lib/consul/power.rb in consul-0.8.0
- old
+ new
@@ -1,17 +1,20 @@
module Consul
module Power
+ include Consul::Power::DynamicAccess::InstanceMethods
def self.included(base)
base.extend ClassMethods
base.send :include, Memoizer
end
def include?(name, *args)
args = args.dup
- record = args.shift
- power_value = send(name)
+
+ context, record = context_and_record_from_args(args, name)
+
+ power_value = send(name, *context)
if record.nil?
!!power_value
else
# record is given
if power_value.nil?
@@ -19,11 +22,11 @@
elsif Util.scope?(power_value)
if Util.scope_selects_all_records?(power_value)
true
else
power_ids_name = self.class.power_ids_name(name)
- send(power_ids_name, *args).include?(record.id)
+ send(power_ids_name, *context).include?(record.id)
end
elsif Util.collection?(power_value)
power_value.include?(record)
else
raise Consul::NoCollection, "can only call #include? on a collection, but power was of type #{power_value.class.name}"
@@ -33,65 +36,44 @@
def include!(*args)
include?(*args) or raise Consul::Powerless.new("No power to #{args.inspect}")
end
- def name_for_record(*args)
- adjective, record = Util.adjective_and_argument(*args)
- name_for_model(adjective, record.class)
- end
+ private
- def for_record(*args)
- send(name_for_record(*args))
+ def context_and_record_from_args(args, name)
+ context_count = send(self.class.context_count_name(name))
+ context = []
+ context_count.times do
+ arg = args.shift or raise Consul::InsufficientContext, "Insufficient context for parametrized power: #{name}"
+ context << arg
+ end
+ record = args.shift
+ [context, record]
end
- def include_record?(*args)
- adjective, record = Util.adjective_and_argument(*args)
- include?(name_for_record(*args), record)
- end
-
- def include_record!(*args)
- adjective, record = Util.adjective_and_argument(*args)
- include!(name_for_record(*args), record)
- end
-
- def name_for_model(*args)
- adjective, model_class = Util.adjective_and_argument(*args)
- collection_name = model_class.name.underscore.gsub('/', '_').pluralize
- [adjective, collection_name].select(&:present?).join('_')
- end
-
- def for_model(*args)
- send(name_for_model(*args))
- end
-
- def include_model?(*args)
- include?(name_for_model(*args))
- end
-
- def include_model!(*args)
- include!(name_for_model(*args))
- end
-
- private
-
def boolean_or_nil?(value)
[TrueClass, FalseClass, NilClass].include?(value.class)
end
def database_touched
# spy for tests
end
module ClassMethods
+ include Consul::Power::DynamicAccess::ClassMethods
def power(*names, &block)
names.each do |name|
define_power(name, &block)
end
end
+ def context_count_name(name)
+ "#{name}_context_count"
+ end
+
def power_ids_name(name)
"#{name.to_s.singularize}_ids"
end
attr_accessor :current
@@ -105,67 +87,20 @@
block.call
ensure
self.current = old_power
end
- def for_model(*args)
- if current
- current.for_model(*args)
- else
- adjective, model = Util.adjective_and_argument(*args)
- model
- end
- end
-
- def include_model?(*args)
- if current
- current.include_model?(*args)
- else
- true
- end
- end
-
- def include_model!(*args)
- if current
- current.include_model!(*args)
- else
- true
- end
- end
-
- def for_record(*args)
- if current
- current.for_record(*args)
- else
- adjective, record = Util.adjective_and_argument(*args)
- record.class
- end
- end
-
- def include_record?(*args)
- if current
- current.include_record?(*args)
- else
- true
- end
- end
-
- def include_record!(*args)
- if current
- current.include_record!(*args)
- else
- true
- end
- end
-
private
def define_power(name, &block)
define_method(name, &block)
define_method("#{name.to_s}?") { |*args| include?(name, *args) }
define_method("#{name.to_s}!") { |*args| include!(name, *args) }
define_method("#{name.to_s.singularize}?") { |*args| include?(name, *args) }
define_method("#{name.to_s.singularize}!") { |*args| include!(name, *args) }
+ context_count_method = context_count_name(name)
+ define_method(context_count_method) { block.arity >= 0 ? block.arity : 0 }
+ private context_count_method
ids_method = power_ids_name(name)
define_method(ids_method) do |*args|
scope = send(name, *args)
database_touched
scope.collect_ids