Sha256: 07e17b7e8d1f8d8a79a4b62c938ccd50bb4d0ed506ce3e8979c7841db984ea58

Contents?: true

Size: 1.58 KB

Versions: 9

Compression:

Stored size: 1.58 KB

Contents

class Guide::Scout
  def initialize(starting_node)
    @starting_node = starting_node

    ensure_starting_node_exists
  end

  def visibility_along_path(node_path)
    current_node = @starting_node
    path_visibility = current_node.options[:visibility]

    node_ids_along_path(node_path).each do |node_id|
      current_node = current_node.child_nodes[node_id]
      ensure_node_exists(current_node, node_id)
      path_visibility = foggiest_visibility(path_visibility,
                                            current_node.options[:visibility])
    end

    path_visibility
  end

  private

  VISIBILITY_PRIORITY = {
    nil => 0,
    :unpublished => 1,
    :restricted => 2,
  }
  private_constant :VISIBILITY_PRIORITY

  def foggiest_visibility(first, second)
    case
    when VISIBILITY_PRIORITY[first] > VISIBILITY_PRIORITY[second]
      first
    when VISIBILITY_PRIORITY[first] < VISIBILITY_PRIORITY[second]
      second
    else
      first
    end
  end

  def node_ids_along_path(node_path)
    @node_ids ||= node_path.split("/").map {|node_id| node_id.to_sym }
  end

  def ensure_node_exists(node, node_id)
    unless node.present?
      raise Guide::Errors::InvalidNode,
        "I can't give you what you're looking for because a node in your path (#{node_id}) doesn't exist."
    end
  end

  def ensure_starting_node_exists
    unless @starting_node.present?
      raise Guide::Errors::InvalidNode,
        "I can't give you what you're looking for because the node that you told me to start from doesn't exist. This means that something is fundamentally wrong with your setup."
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
guide-0.8.0 app/models/guide/scout.rb
guide-0.7.0 app/models/guide/scout.rb
guide-0.6.1 app/models/guide/scout.rb
guide-0.6.0 app/models/guide/scout.rb
guide-0.5.0 app/models/guide/scout.rb
guide-0.4.1 app/models/guide/scout.rb
guide-0.4.0 app/models/guide/scout.rb
guide-0.3.2 app/models/guide/scout.rb
guide-0.3.1 app/models/guide/scout.rb