Skip to content

Commit

Permalink
Merge branch 'relations'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytregunna committed Feb 13, 2012
2 parents 26cd8e4 + c688e87 commit bacceed
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 30 deletions.
35 changes: 19 additions & 16 deletions lib/trello.rb
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions lib/trello/association.rb
@@ -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
42 changes: 42 additions & 0 deletions lib/trello/association_proxy.rb
@@ -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
9 changes: 6 additions & 3 deletions lib/trello/board.rb
Expand Up @@ -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?
Expand All @@ -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.
Expand All @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions lib/trello/card.rb
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/trello/checklist.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/trello/has_actions.rb
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/trello/list.rb
Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions lib/trello/member.rb
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/trello/multi_association.rb
@@ -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
6 changes: 4 additions & 2 deletions lib/trello/organization.rb
Expand Up @@ -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:
Expand Down

0 comments on commit bacceed

Please sign in to comment.