Sha256: 3781b7a6cc29f318c5fd3ad08d602557fbcdebdaaf7e0f75a2986fee9a57532d

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Circuit
  module Storage
    module Nodes
      autoload :Model,        'circuit/storage/nodes/model'
      autoload :MemoryStore,  'circuit/storage/nodes/memory_store'
      autoload :MongoidStore, 'circuit/storage/nodes/mongoid_store'

      extend Circuit::Storage

      # Raised when #get isn't overriden/implemented
      class UnimplementedError < CircuitError; end

      # Raised when a path is not found
      class NotFoundError < CircuitError; end

      # @abstract Subclass and override {#get}
      class BaseStore
        # @param [Sites::Model] site to find path under
        # @param [String] path to find
        # @return [Array<Model>] array of nodes for each path segment
        def get(site, path)
          raise UnimplementedError, "#{self.class.to_s}#get not implemented."
        end

        # @raise NotFoundError if the path cannot be found
        # @param (see #get)
        # @return [Model] Node Model
        # @see #get
        def get!(site, path)
          get(site, path) or raise NotFoundError, "Path not found"
        end

        protected

        # Iterates over the path segments to find the nodes
        # @param [Model] root node
        # @param [String] path to find
        # @return [Array<Model>] array of node Models
        # @see Rack::Request::ClassMethods#path_segments
        def find_nodes_for_path(root, path)
          raise(NotFoundError, "Root path not found") if root.nil?
          [root].tap do |result|
            ::Rack::Request.path_segments(path).each do |segment|
              next if segment.blank?
              if node = result.last.find_child_by_segment(segment)
                result << node
              else
                break
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
circuit-0.2.0 lib/circuit/storage/nodes.rb