Hi guys! I''m writing a small bridge app now, and I have following class: class Board belongs_to :user_n, ... belongs_to :user_e, ... belongs_to :user_s, ... belongs_to :user_w, ... def users [user_n, user_e, user_s, user_w] end end What I''m trying to do is to return something similar to AssociationProxy in the method users. I see no point of creating many- to-many association here - the only thing that I want to achieve is: board.users.owner(card) # returns user that owns given card I''ve spent some time on analysing AssociationProxy class, and I think it''s possible to decouple general Proxy class from it. It''d be really nice to implement the "users" method like: def users ActiveSupport::Proxy.new([user_n, user_e, user_s, user_w]) do def owner(card) ... end end end I can try to write something, but I''d like to know if it''s worth spending few hours on it. What do you think about it? Cheers, Kuba. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
On Feb 25, 2010, at 12:07 PM, Jakub Kuźma wrote:> Hi guys! > > I''m writing a small bridge app now, and I have following class: > > class Board > belongs_to :user_n, ... > belongs_to :user_e, ... > belongs_to :user_s, ... > belongs_to :user_w, ... > > def users > [user_n, user_e, user_s, user_w] > end > end > > What I''m trying to do is to return something similar to > AssociationProxy in the method users. I see no point of creating many- > to-many association here - the only thing that I want to achieve is: > > board.users.owner(card) # returns user that owns given card > > I''ve spent some time on analysing AssociationProxy class, and I think > it''s possible to decouple general Proxy class from it. It''d be really > nice to implement the "users" method like: > > def users > ActiveSupport::Proxy.new([user_n, user_e, user_s, user_w]) do > def owner(card) > ... > end > end > end > > I can try to write something, but I''d like to know if it''s worth > spending few hours on it. What do you think about it?Note that arrays have a metaclass, just like everything else: def users result = [user_n, user_e, user_s, user_w] def result.owner(card) # define something; ''self'' will be the array end result end But I''m not really sure why writing @board.users.owner(card) is in any way preferable to just defining ''owner'' on Board - @board.owner(card)... --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
On Feb 25, 10:53 pm, Matt Jones <al2o...@gmail.com> wrote:> Note that arrays have a metaclass, just like everything else: > > def users > result = [user_n, user_e, user_s, user_w] > def result.owner(card) > # define something; ''self'' will be the array > end > result > endThat''s what I did: http://codaset.com/qoobaa/libre/source/master/blob/app/models/board.rb But I need to access the parent (Board) class from the extension method. Probably I could use the JS version: to define that = self before the definition. Anyway - it''d be really great to have Proxy class in ActiveSupport I think. Thanks, Kuba. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Hi I was also looking for similar ''proxy'' mechanism. What I came up with was: module Proxy def Proxy.included(base) @@b = base end def proxy(o, &class_body) def o.parent() @@b end o.extend(Module.new(&class_body)) end end class Foo include Proxy def initialize @user = [''john'', ''doe''] end def user_proxy proxy(@user) do def hi "Hello #{self.join('' '')} from class #{parent}" end end end end puts Foo.new.user_proxy.hi # => Hello john doe from class Foo However, it would make more sense to use logic from AssociationProxy, if it''s already there. On 25 Lut, 23:00, Jakub Kuźma <qoo...@gmail.com> wrote:> On Feb 25, 10:53 pm, Matt Jones <al2o...@gmail.com> wrote: > > > Note that arrays have a metaclass, just like everything else: > > > def users > > result = [user_n, user_e, user_s, user_w] > > def result.owner(card) > > # define something; ''self'' will be the array > > end > > result > > end > > That''s what I did:http://codaset.com/qoobaa/libre/source/master/blob/app/models/board.rb > > But I need to access the parent (Board) class from the extension > method. Probably I could use the JS version: to define that = self > before the definition. Anyway - it''d be really great to have Proxy > class in ActiveSupport I think. > > Thanks, > Kuba.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
I don''t think this is a good place to discuss about this. Try here: http://groups.google.com/group/rubyonrails-talk On Mon, Mar 1, 2010 at 6:57 AM, Marek Kowalcze <marek.kowalcze@gmail.com>wrote:> Hi > > I was also looking for similar ''proxy'' mechanism. What I came up with > was: > > module Proxy > def Proxy.included(base) > @@b = base > end > def proxy(o, &class_body) > def o.parent() @@b end > o.extend(Module.new(&class_body)) > end > end > > class Foo > include Proxy > > def initialize > @user = [''john'', ''doe''] > end > > def user_proxy > proxy(@user) do > def hi > "Hello #{self.join('' '')} from class #{parent}" > end > end > end > > end > > puts Foo.new.user_proxy.hi # => Hello john doe from class Foo > > However, it would make more sense to use logic from AssociationProxy, > if it''s already there. > > > On 25 Lut, 23:00, Jakub Kuźma <qoo...@gmail.com> wrote: > > On Feb 25, 10:53 pm, Matt Jones <al2o...@gmail.com> wrote: > > > > > Note that arrays have a metaclass, just like everything else: > > > > > def users > > > result = [user_n, user_e, user_s, user_w] > > > def result.owner(card) > > > # define something; ''self'' will be the array > > > end > > > result > > > end > > > > That''s what I did: > http://codaset.com/qoobaa/libre/source/master/blob/app/models/board.rb > > > > But I need to access the parent (Board) class from the extension > > method. Probably I could use the JS version: to define that = self > > before the definition. Anyway - it''d be really great to have Proxy > > class in ActiveSupport I think. > > > > Thanks, > > Kuba. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.