Permalink
Browse files

Merge branch 'relations'

  • Loading branch information...
2 parents 26cd8e4 + c688e87 commit bacceed61945b91c92d9e6d7ff69ecf56625f03f @jeremytregunna committed Feb 13, 2012
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
View
@@ -65,7 +65,8 @@ def closed?
# of the following values:
# :filter => [ :none, :open, :closed, :all ] # default :open
def cards(options = { :filter => :open })
- Client.get("/boards/#{id}/cards").json_into(Card)
+ cards = Client.get("/boards/#{id}/cards").json_into(Card)
+ MultiAssociation.new(self, cards).proxy
end
def has_lists?
@@ -78,7 +79,8 @@ def has_lists?
# of the following values:
# :filter => [ :none, :open, :closed, :all ] # default :open
def lists(options = { :filter => :open })
- Client.get("/boards/#{id}/lists", options).json_into(List)
+ lists = Client.get("/boards/#{id}/lists", options).json_into(List)
+ MultiAssociation.new(self, lists).proxy
end
# Returns an array of members who are associated with this board.
@@ -87,7 +89,8 @@ def lists(options = { :filter => :open })
# of the following values:
# :filter => [ :none, :normal, :owners, :all ] # default :all
def members(options = { :filter => :all })
- Client.get("/boards/#{id}/members", options).json_into(Member)
+ members = Client.get("/boards/#{id}/members", options).json_into(Member)
+ MultiAssociation.new(self, members).proxy
end
# Returns a reference to the organization this board belongs to.
View
@@ -52,7 +52,8 @@ def board
# of the following values:
# :filter => [ :none, :all ] # default :all
def checklists(options = { :filter => :all })
- Client.get("/cards/#{id}/checklists", options).json_into(Checklist)
+ checklists = Client.get("/cards/#{id}/checklists", options).json_into(Checklist)
+ MultiAssociation.new(self, checklists).proxy
end
# Returns a reference to the list this card is currently in.
@@ -62,9 +63,10 @@ def list
# Returns a list of members who are assigned to this card.
def members
- member_ids.map do |member_id|
+ members = member_ids.map do |member_id|
Client.get("/members/#{member_id}").json_into(Member)
end
+ MultiAssociation.new(self, members).proxy
end
# Saves a record.
View
@@ -72,9 +72,10 @@ def list
# Return a list of members active in this checklist.
def members
- member_ids.map do |member_id|
+ members = member_ids.map do |member_id|
Member.find(member_id)
end
+ MultiAssociation.new(self, members).proxy
end
# Add an item to the checklist
@@ -2,7 +2,8 @@ module Trello
module HasActions
# Returns a list of the actions associated with this object.
def actions(options = {})
- Client.get("#{request_prefix}/actions", { :filter => :all }.merge(options)).json_into(Action)
+ actions = Client.get("#{request_prefix}/actions", { :filter => :all }.merge(options)).json_into(Action)
+ MultiAssociation.new(self, actions).proxy
end
end
end
View
@@ -64,7 +64,8 @@ def board
# of the following values:
# :filter => [ :none, :open, :closed, :all ] # default :open
def cards(options = { :filter => :open })
- Client.get("/lists/#{id}/cards", options).json_into(Card)
+ cards = Client.get("/lists/#{id}/cards", options).json_into(Card)
+ MultiAssociation.new(self, cards).proxy
end
# :nodoc:
View
@@ -47,7 +47,8 @@ def avatar_url(options = { :size => :large })
# of the following values:
# :filter => [ :none, :members, :organization, :public, :open, :closed, :all ] # default: :all
def boards(options = { :filter => :all })
- Client.get("/members/#{username}/boards", options).json_into(Board)
+ boards = Client.get("/members/#{username}/boards", options).json_into(Board)
+ MultiAssociation.new(self, boards).proxy
end
# Returns a list of cards the member is assigned to.
@@ -56,7 +57,8 @@ def boards(options = { :filter => :all })
# of the following values:
# :filter => [ :none, :open, :closed, :all ] # default :open
def cards(options = { :filter => :open })
- Client.get("/members/#{username}/cards", options).json_into(Card)
+ cards = Client.get("/members/#{username}/cards", options).json_into(Card)
+ MultiAssociation.new(self, cards).proxy
end
# Returns a list of the organizations this member is a part of.
@@ -65,12 +67,14 @@ def cards(options = { :filter => :open })
# of the following values:
# :filter => [ :none, :members, :public, :all ] # default: all
def organizations(options = { :filter => :all })
- Client.get("/members/#{username}/organizations", options).json_into(Organization)
+ orgs = Client.get("/members/#{username}/organizations", options).json_into(Organization)
+ MultiAssociation.new(self, orgs).proxy
end
# Returns a list of notifications for the user
def notifications
- Client.get("/members/#{username}/notifications").json_into(Notification)
+ notifications = Client.get("/members/#{username}/notifications").json_into(Notification)
+ MultiAssociation.new(self, notifications).proxy
end
def save
@@ -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
@@ -29,12 +29,14 @@ def update_fields(fields)
# Returns a list of boards under this organization.
def boards
- Client.get("/organizations/#{id}/boards/all").json_into(Board)
+ boards = Client.get("/organizations/#{id}/boards/all").json_into(Board)
+ MultiAssociation.new(self, boards).proxy
end
# Returns an array of members associated with the organization.
def members
- Client.get("/organizations/#{id}/members/all").json_into(Member)
+ members = Client.get("/organizations/#{id}/members/all").json_into(Member)
+ MultiAssociation.new(self, members).proxy
end
# :nodoc:

0 comments on commit bacceed

Please sign in to comment.