Module: Party::Proxy::InstanceMethods

Defined in:
lib/proxy_party.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#proxy_factoriesObject

Returns the value of attribute proxy_factories.



26
27
28
# File 'lib/proxy_party.rb', line 26

def proxy_factories
  @proxy_factories
end

Instance Method Details

#add_proxy_factories(hash) ⇒ Object Also known as: add_proxy_factory



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/proxy_party.rb', line 32

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

#instance_proxy_accessor_for(obj, method = nil) ⇒ Object

Raises:

  • (ArgumentError)


204
205
206
207
208
# File 'lib/proxy_party.rb', line 204

def instance_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
  instance_proxy_accessors_for obj, [method].flatten
end

#instance_proxy_accessors_for(obj, *methods) ⇒ Object

Add proxy methods only to the instance object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/proxy_party.rb', line 164

def instance_proxy_accessors_for obj, *methods
  instance_proxy_for obj, methods
  check = last_arg_value({:check => false}, methods)

  rename_methods = last_arg_value({:rename => {}}, methods)
  methods.delete(:rename) if rename_methods
  
  methods.to_symbols.flat_uniq.each do |meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method #{meth} to proxy for #{obj}" if !self.send(obj).respond_to?(:"#{meth}=")
    end
    instance_eval %{
    class << self
      define_method :#{meth}= do |arg|
        self.#{obj} ||= create_in_factory(:#{obj})
        self.#{obj} ||= self.class.send(:create_in_factory, :#{obj})
        #{obj}.send(:#{meth}=, arg) if #{obj}
      end
    end
    }
  end 

  rename_methods.each_pair do |meth, new_meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method to proxy for #{obj}" if !self.send(obj).respond_to?(meth)
    end
    instance_eval %{
      class << self
        define_method :#{new_meth}= do |arg|
          self.#{obj} ||= create_in_factory(:#{obj})
          self.#{obj} ||= self.class.send(:create_in_factory, :#{obj})
          #{obj}.send(:#{meth}=, arg) if #{obj}
        end
      end
    }
  end
end

#instance_proxy_for(obj, *methods) ⇒ Object

Add proxy methods only to the instance object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/proxy_party.rb', line 129

def instance_proxy_for obj, *methods
  check = last_arg_value({:check => false}, methods) 
  rename_methods = last_arg_value({:rename => {}}, methods)
  methods.delete(:rename) if rename_methods
  
  methods.to_symbols.flat_uniq.each do |meth|       
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method to proxy for #{obj}" if !self.send(obj).respond_to?(meth)
    end
    instance_eval %{
    class << self
      define_method :#{meth} do
        #{obj}.send(:#{meth}) if #{obj}
      end
    end
    }
  end 
  
  rename_methods.each_pair do |meth, new_meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method to proxy for #{obj}" if !self.send(obj).respond_to?(meth)
    end
    instance_eval %{
    class << self
      define_method :#{new_meth} do
        #{obj}.send(:#{meth}) if #{obj}
      end
    end
    }
  end        
end

#named_proxies(hash) ⇒ Object

Raises:

  • (ArgumentError)


210
211
212
213
214
215
216
# File 'lib/proxy_party.rb', line 210

def named_proxies hash
  raise ArgumentError, "Argument must be a hash" if !hash.kind_of? Hash
  self.class.send :include, Party::Proxy
  hash.each_pair do |proxy, methods|
    instance_proxy_accessors_for proxy, methods
  end
end

#proxy_accessor_for(obj, method = nil) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
# File 'lib/proxy_party.rb', line 107

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_accessor_for obj, [method].flatten
end

#proxy_accessors_for(obj, *methods) ⇒ Object

Add instance proxy methods



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/proxy_party.rb', line 87

def proxy_accessors_for obj, *methods
  proxy_for obj, methods
  check = last_arg_value({:check => false}, methods)
  rename_methods = last_arg_value({:rename => {}}, methods)
  methods.delete(:rename) if rename_methods

  methods.to_symbols.flat_uniq.each do |meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method #{meth} to proxy for #{obj}" if !self.send(obj).respond_to?(:"#{meth}=")
    end
    class_eval %{
      define_method :#{meth}= do |arg|
        self.#{obj} ||= create_in_factory(:#{obj})
        self.#{obj} ||= self.class.send(:create_in_factory, :#{obj})
        #{obj}.send(:#{meth}=, arg) if #{obj}
      end
    }
  end

  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_accessor_for obj, [method].flatten
  end

  rename_methods.each_pair do |meth, new_meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method #{meth} to proxy for #{obj}" if !self.send(obj).respond_to?(:"#{meth}=")
    end
    class_eval %{
      define_method :#{new_meth}= do |arg|
        self.#{obj} ||= create_in_factory(:#{obj})
        self.#{obj} ||= self.class.send(:create_in_factory, :#{obj})
        #{obj}.send(:#{meth}=, arg) if #{obj}
      end
    }
  end
end

#proxy_factoryObject



28
29
30
# File 'lib/proxy_party.rb', line 28

def proxy_factory
  proxy_factories
end

#proxy_for(obj, *methods) ⇒ Object

Add instance proxy methods



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/proxy_party.rb', line 56

def proxy_for obj, *methods
  check   = last_arg_value({:check => false}, methods)
  rename_methods = last_arg_value({:rename => {}}, methods)
  methods.delete(:rename) if rename_methods

  methods.to_symbols.flat_uniq.each do |meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method to proxy for #{obj}" if !self.send(obj).respond_to?(meth)
    end
    class_eval %{
      define_method :#{meth} do
        #{obj}.send(:#{meth}) if #{obj}
      end
    }
  end
  
  rename_methods.each_pair do |meth, new_meth|
    if check
      raise ArgumentError, "No such object to proxy #{obj}" if !self.respond_to?(obj)
      raise ArgumentError, "No such method to proxy for #{obj}" if !self.send(obj).respond_to?(meth)
    end
    class_eval %{
      define_method :#{new_meth} do
        #{obj}.send(:#{meth}) if #{obj}
      end
    }
  end
end