Permalink
Please sign in to comment.
Showing
with
82 additions
and 16 deletions.
- +19 −16 lib/trello.rb
- +11 −0 lib/trello/association.rb
- +42 −0 lib/trello/association_proxy.rb
- +10 −0 lib/trello/multi_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 |
@@ -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