Module: Party::Proxy::ClassMethods
- Defined in:
- lib/proxy_party.rb
Overview
Define class methods here.
Instance Attribute Summary collapse
-
#proxies ⇒ Object
Returns the value of attribute proxies.
-
#proxy_factories ⇒ Object
Returns the value of attribute proxy_factories.
Instance Method Summary collapse
- #add_proxy_factories(hash) ⇒ Object (also: #add_proxy_factory)
- #proxy_accessor_for(obj, method = nil) ⇒ Object
- #proxy_accessors_for(obj, *methods) ⇒ Object
- #proxy_factory ⇒ Object
- #proxy_for(obj, *methods) ⇒ Object
- #remove_proxy(name) ⇒ Object
- #remove_proxy_factories ⇒ Object
- #remove_proxy_factory(name) ⇒ Object
Instance Attribute Details
#proxies ⇒ Object
Returns the value of attribute proxies.
228 229 230 |
# File 'lib/proxy_party.rb', line 228 def proxies @proxies end |
#proxy_factories ⇒ Object
Returns the value of attribute proxy_factories.
229 230 231 |
# File 'lib/proxy_party.rb', line 229 def proxy_factories @proxy_factories end |
Instance Method Details
#add_proxy_factories(hash) ⇒ Object Also known as: add_proxy_factory
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/proxy_party.rb', line 243 def add_proxy_factories hash hash.each_pair do |name, factory| factory = if factory.kind_of?(Class) Party::Proxy::Factory.new(factory) else case factory when Array fac = factory.first meth = factory.last if factory.size == 2 raise ArgumentError, "Factory must be a Class, was #{fac}" if !fac.kind_of?(Class) raise ArgumentError, "Factory method be a label, was #{meth}" if meth && !meth.kind_of_label? Party::Proxy::Factory.new(fac, meth) else raise ArgumentError, "Factory must be a Class or have a #create method: #{factory}" if !factory.respond_to?(:create) factory end end self.proxy_factories ||= {} self.proxy_factories.merge!(name.to_sym => factory) if name.kind_of_label? end end |
#proxy_accessor_for(obj, method = nil) ⇒ Object
315 316 317 318 319 |
# File 'lib/proxy_party.rb', line 315 def proxy_accessor_for obj, method = nil method ||= obj[1] if obj.kind_of?(Hash) raise ArgumentError, "Takes only a single accessor to proxy" if method.kind_of? Array proxy_accessors_for obj, [method].flatten end |
#proxy_accessors_for(obj, *methods) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/proxy_party.rb', line 290 def proxy_accessors_for obj, *methods proxy_for obj, methods rename_methods = last_arg_value({:rename => {}}, methods) methods.delete(:rename) if rename_methods methods.flat_uniq.each do |meth| name = meth.to_sym obj_name = obj.to_sym define_method name do |arg| send(obj_name).send('||=', create_in_factory(obj_name)) send(obj_name).send("#{name}=", arg) if send(obj) end end rename_methods.each_pair do |meth, new_meth| name = meth.to_sym obj_name = obj.to_sym define_method new_meth.to_sym do |arg| send(obj_name).send('||=', create_in_factory(obj_name)) send(obj_name).send("#{name}=", arg) if send(obj) end end end |
#proxy_factory ⇒ Object
231 232 233 |
# File 'lib/proxy_party.rb', line 231 def proxy_factory proxy_factories end |
#proxy_for(obj, *methods) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/proxy_party.rb', line 271 def proxy_for obj, *methods rename_methods = last_arg_value({:rename => {}}, methods) methods.delete(:rename) if rename_methods methods.flat_uniq.each do |meth| name = meth.to_sym define_method name do send(obj).send(name) if send(obj) end end rename_methods.each_pair do |meth, new_meth| name = meth.to_sym define_method new_meth.to_sym do send(obj).send(name) if send(obj) end end end |
#remove_proxy(name) ⇒ Object
267 268 269 |
# File 'lib/proxy_party.rb', line 267 def remove_proxy name proxies.delete(name) end |
#remove_proxy_factories ⇒ Object
239 240 241 |
# File 'lib/proxy_party.rb', line 239 def remove_proxy_factories self.proxy_factories = nil end |
#remove_proxy_factory(name) ⇒ Object
235 236 237 |
# File 'lib/proxy_party.rb', line 235 def remove_proxy_factory name @proxy_factories[name] = nil end |