Permalink
Browse files

adds association wrapper objects

  • Loading branch information...
jeremytregunna committed Feb 13, 2012
1 parent 26cd8e4 commit 0533f3c6c6807c7f024799acc393716dbf4158f9
Showing with 82 additions and 16 deletions.
  1. +19 −16 lib/trello.rb
  2. +11 −0 lib/trello/association.rb
  3. +42 −0 lib/trello/association_proxy.rb
  4. +10 −0 lib/trello/multi_association.rb
View
@@ -26,22 +26,25 @@
#
# Feel free to {peruse and participate in our Trello board}[https://trello.com/board/ruby-trello/4f092b2ee23cb6fe6d1aaabd]. It's completely open to the public.
module Trello
- autoload :Action, 'trello/action'
- autoload :BasicData, 'trello/basic_data'
- autoload :Board, 'trello/board'
- autoload :Card, 'trello/card'
- autoload :Checklist, 'trello/checklist'
- autoload :Client, 'trello/client'
- autoload :HasActions, 'trello/has_actions'
- autoload :Item, 'trello/item'
- autoload :ItemState, 'trello/item_state'
- autoload :List, 'trello/list'
- autoload :Member, 'trello/member'
- autoload :Notification, 'trello/notification'
- autoload :Organization, 'trello/organization'
- autoload :Request, 'trello/net'
- autoload :TInternet, 'trello/net'
- autoload :Token, 'trello/token'
+ autoload :Action, 'trello/action'
+ autoload :Association, 'trello/association'
+ autoload :AssociationProxy, 'trello/association_proxy'
+ autoload :BasicData, 'trello/basic_data'
+ autoload :Board, 'trello/board'
+ autoload :Card, 'trello/card'
+ autoload :Checklist, 'trello/checklist'
+ autoload :Client, 'trello/client'
+ autoload :HasActions, 'trello/has_actions'
+ autoload :Item, 'trello/item'
+ autoload :ItemState, 'trello/item_state'
+ autoload :List, 'trello/list'
+ autoload :Member, 'trello/member'
+ autoload :MultiAssociation, 'trello/multi_association'
+ autoload :Notification, 'trello/notification'
+ autoload :Organization, 'trello/organization'
+ autoload :Request, 'trello/net'
+ autoload :TInternet, 'trello/net'
+ autoload :Token, 'trello/token'
module Authorization
autoload :AuthPolicy, 'trello/authorization'
View
@@ -0,0 +1,11 @@
+module Trello
+ class Association
+ attr_reader :owner, :target, :options, :proxy
+
+ def initialize(owner, target)
+ @owner = owner
+ @target = target
+ @options = {}
+ end
+ end
+end
@@ -0,0 +1,42 @@
+require 'active_support/core_ext'
+
+module Trello
+ class AssociationProxy
+ alias :proxy_extend :extend
+
+ instance_methods.each { |m| undef_method m unless m.to_s =~ /^(?:nil\?|send|object_id|to_a)$|^__|^respond_to|proxy_/ }
+
+ delegate :owner, :target, :to => :@association
+ delegate :count, :to => :@association
+
+ def initialize(association)
+ @association = association
+ Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
+ end
+
+ def proxy_assocation
+ @association
+ end
+
+ def method_missing(method, *args, &block)
+ if target.respond_to? method
+ target.send(method, *args, &block)
+ else
+ super
+ end
+ end
+
+ def ===(other)
+ other === target
+ end
+
+ def to_ary
+ proxy_assocation.dup
+ end
+ alias_method :to_a, :to_ary
+
+ def <<(*records)
+ proxy_assocation.concat(records) && self
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module Trello
+ class MultiAssociation < Association
+ delegate :count, :to => :target
+
+ def initialize(owner, target = [])
+ super
+ @proxy = AssociationProxy.new(self)
+ end
+ end
+end

0 comments on commit 0533f3c

Please sign in to comment.