Sha256: e638200c0eb07ae3bed19254b5e04d81d154eb68eabfd701233b262f89dac929

Contents?: true

Size: 881 Bytes

Versions: 13

Compression:

Stored size: 881 Bytes

Contents

require 'toy/proxies/list'

module Toy
  class List
    include Toy::Collection

    def after_initialize
      model.attribute(key, Array)
      create_accessors
    end

    def key
      @key ||= :"#{name.to_s.singularize}_ids"
    end

    private
      def proxy_class
        Toy::Proxies::List
      end

      def list_method
        :lists
      end

      def create_accessors
        model.class_eval """
          def #{name}
            #{instance_variable} ||= self.class.#{list_method}[:#{name}].new_proxy(self)
          end

          def #{name}=(records)
            #{name}.replace(records)
          end
        """

        if options[:dependent]
          model.class_eval """
            after_destroy :destroy_#{name}
            def destroy_#{name}
              #{name}.each { |o| o.destroy }
            end
          """
        end
      end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
toystore-0.8.3 lib/toy/list.rb
toystore-0.8.2 lib/toy/list.rb
toystore-0.8.1 lib/toy/list.rb
toystore-0.8.0 lib/toy/list.rb
toystore-0.7.0 lib/toy/list.rb
toystore-0.6.6 lib/toy/list.rb
toystore-0.6.5 lib/toy/list.rb
toystore-0.6.4 lib/toy/list.rb
toystore-0.6.3 lib/toy/list.rb
toystore-0.6.2 lib/toy/list.rb
toystore-0.6.1 lib/toy/list.rb
toystore-0.6 lib/toy/list.rb
toystore-0.5 lib/toy/list.rb