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_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.
216 217 218 |
# File 'lib/proxy_party.rb', line 216 def proxies @proxies end |
#proxy_factories ⇒ Object
Returns the value of attribute proxy_factories.
217 218 219 |
# File 'lib/proxy_party.rb', line 217 def proxy_factories @proxy_factories end |
Instance Method Details
#add_proxy_factories(hash) ⇒ Object Also known as: add_proxy_factory
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/proxy_party.rb', line 231 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_accessors_for(obj, *methods) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/proxy_party.rb', line 278 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
219 220 221 |
# File 'lib/proxy_party.rb', line 219 def proxy_factory proxy_factories end |
#proxy_for(obj, *methods) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/proxy_party.rb', line 259 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
255 256 257 |
# File 'lib/proxy_party.rb', line 255 def remove_proxy name proxies.delete(name) end |
#remove_proxy_factories ⇒ Object
227 228 229 |
# File 'lib/proxy_party.rb', line 227 def remove_proxy_factories self.proxy_factories = nil end |
#remove_proxy_factory(name) ⇒ Object
223 224 225 |
# File 'lib/proxy_party.rb', line 223 def remove_proxy_factory name @proxy_factories[name] = nil end |