Module: Consul::Power::ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
#for_model, #for_record, #include_model!, #include_model?, #include_record!, #include_record?
Class Method Details
.thread_key(klass) ⇒ Object
79
80
81
|
# File 'lib/consul/power.rb', line 79
def self.thread_key(klass)
"consul|#{klass.to_s}.current"
end
|
Instance Method Details
#current=(power) ⇒ Object
87
88
89
|
# File 'lib/consul/power.rb', line 87
def current=(power)
Thread.current[ClassMethods.thread_key(self)] = power
end
|
#define_ids_method(name) ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/consul/power.rb', line 115
def define_ids_method(name)
ids_method = power_ids_name(name)
define_method(ids_method) { |*args| default_power_ids(name, *args) }
memoize ids_method
end
|
#define_main_method(name, &block) ⇒ Object
123
124
125
126
|
# File 'lib/consul/power.rb', line 123
def define_main_method(name, &block)
define_method(name, &block)
memoize name
end
|
#define_power(name, &block) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/consul/power.rb', line 128
def define_power(name, &block)
name = name.to_s
if name.ends_with?('?')
name_without_suffix = name.chop
define_query_and_bang_methods(name_without_suffix, &block)
else
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)
define_query_and_bang_methods(singular) { |*args| default_include_object?(name, *args) }
rescue Consul::PowerNotSingularizable
end
end
name
end
|
#define_query_and_bang_methods(name, &query) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/consul/power.rb', line 106
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) }
end
|
#power(*names, &block) ⇒ Object
69
70
71
72
73
|
# File 'lib/consul/power.rb', line 69
def power(*names, &block)
names.each do |name|
define_power(name, &block)
end
end
|
#power_ids_name(name) ⇒ Object
75
76
77
|
# File 'lib/consul/power.rb', line 75
def power_ids_name(name)
"#{name.to_s.singularize}_ids"
end
|
#singularize_power_name(name) ⇒ Object
150
151
152
153
154
155
156
157
158
|
# File 'lib/consul/power.rb', line 150
def singularize_power_name(name)
name = name.to_s
singularized = name.singularize
if singularized == name
raise Consul::PowerNotSingularizable, "Power name can not have an singular form: #{name}"
else
singularized
end
end
|
#with_power(inner_power, &block) ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/consul/power.rb', line 91
def with_power(inner_power, &block)
unless inner_power.is_a?(self) || inner_power.nil?
inner_power = new(inner_power)
end
old_power = current
self.current = inner_power
block.call
ensure
self.current = old_power
end
|
#without_power(&block) ⇒ Object
102
103
104
|
# File 'lib/consul/power.rb', line 102
def without_power(&block)
with_power(nil, &block)
end
|