Sha256: d95ad4ad5747fcd21d8614e20d5e429cff4a3af36ff39ca4845d8e082eac7efb
Contents?: true
Size: 2 KB
Versions: 7
Compression:
Stored size: 2 KB
Contents
# encoding: utf-8 module Dynamoid #:nodoc: # The base association module. module Associations module Association attr_accessor :name, :options, :source def initialize(source, name, options) @name = name @options = options @source = source end def empty? records.empty? end alias :nil? :empty? def size records.count end alias :count :size def include?(object) records.include?(object) end def delete(object) source.update_attribute(source_attribute, source_ids - Array(object).collect(&:id)) Array(object).collect{|o| self.send(:disassociate_target, o)} if target_association object end def <<(object) source.update_attribute(source_attribute, source_ids.merge(Array(object).collect(&:id))) Array(object).collect{|o| self.send(:associate_target, o)} if target_association object end def setter(object) source.update_attribute(source_attribute, Array(object).collect(&:id)) Array(object).collect{|o| self.send(:associate_target, o)} if target_association object end def create(attributes = {}) object = target_class.create(attributes) self << object end private def records results = target_class.find(source_ids.to_a) results.nil? ? [] : Array(results) end def target_class name.to_s.singularize.capitalize.constantize end def target_attribute "#{target_association}_ids".to_sym if target_association end def target_ids target.send(target_attribute) || Set.new end def source_class source.class end def source_attribute "#{name}_ids".to_sym end def source_ids source.send(source_attribute) || Set.new end end end end
Version data entries
7 entries across 7 versions & 1 rubygems