Module: Consul::Power::ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
#for_model, #include_model!, #include_model?, #include_record!, #include_record?
Class Method Details
.thread_key(klass) ⇒ Object
90
91
92
|
# File 'lib/consul/power.rb', line 90
def self.thread_key(klass)
"consul|#{klass.to_s}.current"
end
|
Instance Method Details
#current=(power) ⇒ Object
98
99
100
|
# File 'lib/consul/power.rb', line 98
def current=(power)
Thread.current[ClassMethods.thread_key(self)] = power
end
|
#define_ids_method(name) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/consul/power.rb', line 144
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
152
153
154
155
|
# File 'lib/consul/power.rb', line 152
def define_main_method(name, &block)
define_method(name, &block)
memoize name
end
|
#define_power(name, &block) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/consul/power.rb', line 157
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, :is_plural => false, &block)
else
define_main_method(name, &block)
define_ids_method(name)
define_query_and_bang_methods(name, :is_plural => true) { |*args| default_include_power?(name, *args) }
begin
singular = singularize_power_name(name)
define_query_and_bang_methods(singular, :is_plural => false) { |*args| default_include_object?(name, *args) }
rescue Consul::PowerNotSingularizable
end
end
name
end
|
#define_query_and_bang_methods(name, options, &query) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/consul/power.rb', line 124
def define_query_and_bang_methods(name, options, &query)
is_plural = options.fetch(:is_plural)
query_method = "#{name}?"
bang_method = "#{name}!"
define_method(query_method, &query)
memoize query_method
define_method(bang_method) do |*args|
if is_plural
if send(query_method, *args)
send(name, *args)
else
powerless!(name, *args)
end
else
send(query_method, *args) or powerless!(name, *args)
end
end
end
|
#power(*names, &block) ⇒ Object
80
81
82
83
84
|
# File 'lib/consul/power.rb', line 80
def power(*names, &block)
names.each do |name|
define_power(name, &block)
end
end
|
#power_ids_name(name) ⇒ Object
86
87
88
|
# File 'lib/consul/power.rb', line 86
def power_ids_name(name)
"#{name.to_s.singularize}_ids"
end
|
#singularize_power_name(name) ⇒ Object
179
180
181
182
183
184
185
186
187
|
# File 'lib/consul/power.rb', line 179
def singularize_power_name(name)
name = name.to_s
singularized = name.singularize
if singularized == name
raise Consul::PowerNotSingularizable, "Power name can not have a singular form: #{name}"
else
singularized
end
end
|
#with_power(*args, **kwargs, &block) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/consul/power.rb', line 102
def with_power(*args, **kwargs , &block)
inner_power = if args.first.is_a?(self)
args.first
elsif args.length == 1 && args.first.nil?
nil
elsif kwargs.empty?
new(*args)
else
new(*args, **kwargs)
end
old_power = current
self.current = inner_power
block.call
ensure
self.current = old_power
end
|
#without_power(&block) ⇒ Object
120
121
122
|
# File 'lib/consul/power.rb', line 120
def without_power(&block)
with_power(nil, &block)
end
|