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
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
137
138
139
140
141
142
143
|
# File 'lib/consul/power.rb', line 137
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
145
146
147
148
|
# File 'lib/consul/power.rb', line 145
def define_main_method(name, &block)
define_method(name, &block)
memoize name
end
|
#define_power(name, &block) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/consul/power.rb', line 150
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/consul/power.rb', line 117
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
172
173
174
175
176
177
178
179
180
|
# File 'lib/consul/power.rb', line 172
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
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/consul/power.rb', line 102
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
113
114
115
|
# File 'lib/consul/power.rb', line 113
def without_power(&block)
with_power(nil, &block)
end
|