Sha256: 06f93b71518a4cd61f30218331c9ee4d534cde4cb4e1114fb391c26b1362a5e6
Contents?: true
Size: 1.76 KB
Versions: 10
Compression:
Stored size: 1.76 KB
Contents
require 'toy/proxies/proxy' module Toy module Proxies class EmbeddedList < Toy::Proxies::Proxy def get(id) target.detect { |record| record.id == id } end def get!(id) get(id) || raise(Toy::NotFound.new(id)) end def include?(record) return false if record.nil? target.include?(record) end def push(instance) assert_type(instance) assign_reference(instance) self.target.push(instance) end alias :<< :push def concat(*instances) instances = instances.flatten instances.each do |instance| assert_type(instance) assign_reference(instance) end self.target.concat instances end def replace(instances) @target = instances.map do |instance| instance = if instance.is_a?(proxy_class) instance else key = instance.delete('id') proxy_class.load(key, instance) end assign_reference(instance) end end def create(attrs={}) proxy_class.new(attrs).tap do |instance| assign_reference(instance) if instance.valid? push(instance) proxy_owner.save end end end def destroy(*args, &block) ids = block_given? ? target.select(&block).map(&:id) : args.flatten target.delete_if { |instance| ids.include?(instance.id) } proxy_owner.save end def destroy_all target.clear proxy_owner.save end private def find_target [] end def assign_reference(instance) instance.parent_reference = proxy_owner instance end end end end
Version data entries
10 entries across 10 versions & 1 rubygems