Sha256: d9c29874b3ddc48c6c953f2542a0e8c36c2706197f5e95f18d366079d166dae0

Contents?: true

Size: 1.66 KB

Versions: 13

Compression:

Stored size: 1.66 KB

Contents

module Toy
  module EmbeddedLists
    extend ActiveSupport::Concern

    module ClassMethods
      def embedded_lists
        @embedded_lists ||= {}
      end

      def embedded_list?(key)
        embedded_lists.keys.include?(key.to_sym)
      end

      def parent_reference_module
        @parent_reference_module ||= begin
          mod = Module.new
          include mod
          mod
        end
      end

      def parent_references
        @parent_references ||= []
      end

      def parent_reference?(key)
        parent_references.include?(key.to_sym)
      end

      def parent_reference(*names)
        names.flatten.each do |name|
          parent_references << name
          parent_reference_module.module_eval <<-CODE
            def #{name}
              parent_reference
            end

            def #{name}?
              parent_reference.present?
            end
          CODE
        end
      end

      # @examples
      #   list :moves                                   # assumes Move
      #   list :moves, :dependent => true               # assumes Move
      #   list :recent_moves, Move                      # uses Move
      #   list :recent_moves, Move, :dependent => true  # uses Move
      #
      #   embedded_list :moves do
      #     def recent
      #       target.select { |t| t.recent? }
      #     end
      #   end
      #
      #   module RecentExtension
      #     def recent
      #       target.select { |t| t.recent? }
      #     end
      #   end
      #   embedded_list :moves, :extensions => [RecentExtension]
      def embedded_list(name, *args, &block)
        EmbeddedList.new(self, name, *args, &block)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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