Sha256: aa20d768dd3dc8392eb9e774e344ba5eeb210c12320db358e4bd41948ed375e4

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

require 'toy/proxies/list'

module Toy
  class List
    attr_accessor :model, :name, :options

    def initialize(model, name, *args, &block)
      @model   = model
      @name    = name.to_sym
      @options = args.extract_options!
      @type    = args.shift

      model.send(list_method)[name] = self

      options[:extensions] = modularized_extensions(block, options[:extensions])

      model.attribute(key, options.fetch(:attribute_type) { Array })
      create_accessors
    end

    def type
      @type ||= name.to_s.classify.constantize
    end

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

    def instance_variable
      @instance_variable ||= :"@_#{name}"
    end

    def new_proxy(owner)
      proxy_class.new(self, owner)
    end

    def extensions
      options[:extensions]
    end

    def eql?(other)
      self.class.eql?(other.class) &&
        model == other.model &&
        name  == other.name
    end
    alias :== :eql?

    private

    def proxy_class
      raise('Not Implemented')
    end

    def modularized_extensions(*extensions)
      extensions.flatten.compact.map do |extension|
        Proc === extension ? Module.new(&extension) : extension
      end
    end

    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

8 entries across 8 versions & 1 rubygems

Version Path
toystore-0.13.2 lib/toy/list.rb
toystore-0.13.1 lib/toy/list.rb
toystore-0.13.0 lib/toy/list.rb
toystore-0.12.0 lib/toy/list.rb
toystore-0.11.0 lib/toy/list.rb
toystore-0.10.4 lib/toy/list.rb
toystore-0.10.3 lib/toy/list.rb
toystore-0.10.2 lib/toy/list.rb