Skip to content

Commit

Permalink
Start of has many
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed May 6, 2013
1 parent 9539b26 commit 74e54a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/capsule_crm/associations/has_many.rb
@@ -0,0 +1,21 @@
module CapsuleCRM
module Associations
module HasMany
extend ActiveSupport::Concern

module ClassMethods
def has_many(association_name, options = {})

define_method association_name do
instance_variable_get "@#{association_name}"
end

define_method "#{association_name}=" do |associated_objects|
instance_variable_set :"@#{association_name}",
CapsuleCRM::Associations::HasManyProxy.new(associated_objects)
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/capsule_crm/associations/has_many_proxy.rb
@@ -0,0 +1,16 @@
module CapsuleCRM
module Associations
class HasManyProxy < BasicObject

def method_missing(name, *args, &block)
target.send(name, *args, &block)
end

private

def target
@target ||= []
end
end
end
end

0 comments on commit 74e54a9

Please sign in to comment.