Sha256: 3406e89b4e9e44f0f6a7602f434d49bc5d615abb7fc45a9dc2dd5dfa32a0191d
Contents?: true
Size: 1.49 KB
Versions: 18
Compression:
Stored size: 1.49 KB
Contents
module EfoNelfo module HasMany def self.included(base) base.send :extend, ClassMethods end def find_association(post_type) send self.class.association(post_type) if has_association?(post_type) end def has_association?(post_type) !self.class.association(post_type).nil? end def associations self.class.associations end def add(post) find_association(post) << post end def to_a [ super ] + associations_array end protected def associations_array if associations associations.keys.map { |name| find_association(name).to_a }.reject(&:empty?).flatten(1) else [] end end module ClassMethods def has_many(name, options) post_type = options[:post_type] @associations ||= {} @associations[post_type] = name define_method name do instance_variable_get("@#{name}") || instance_variable_set("@#{name}", EfoNelfo::Collection.new(self, post_type)) end define_method "#{name}=" do |values| instance_variable_set "@#{name}", EfoNelfo::Collection.new(self, post_type) values.each { |item| instance_variable_get("@#{name}") << item } if values end end def associations @associations end def association(post_type) type = post_type.is_a?(EfoNelfo::PostType) ? post_type.post_type : post_type.to_s.upcase @associations[type] end end end end
Version data entries
18 entries across 18 versions & 1 rubygems