Sha256: 21c0b136908eeffe1b3e25fc81b6fab0189a921f944c6d402fd5fb9e38fbab25

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 KB

Contents

class Guide::Node
  class_attribute :child_nodes

  def self.inherited(sub_class)
    sub_class.child_nodes = {}
  end

  def self.contains(id, options = {})
    child_nodes[id] = child_node_class(id).new(options)
  end

  def self.child_node_class(id)
    child_node_class_name = "#{self}::#{id.to_s.camelize}"
    child_node_class_name.constantize
  rescue NameError => name_error
    raise Guide::Errors::InvalidNode,
      "I can't build the tree that backs Guide because I could not load the class #{child_node_class_name}.",
      name_error.backtrace
  end

  attr_reader :id, :options

  def initialize(options = {})
    @id = infer_id_from_class_name
    @options = options
  end

  def name
    @id.to_s.titleize
  end

  def leaf_node?
    self.child_nodes.empty?
  end

  def child_nodes
    self.class.child_nodes
  end

  def ==(other)
    self.class == other.class
  end

  def node_type
    # for direct children, this will be :node
    self.class.superclass.name.demodulize.underscore.to_sym
  end

  def can_be_rendered?
    false
  end

  def view_model
    Guide::ViewModel.new
  end

  def image_path(image_name, extension = "png")
    Guide::Photographer.new(image_name, extension).image_path
  end

  private

  def infer_id_from_class_name
    # for example, Structures::Checkout::Page becomes :page
    self.class.name.demodulize.underscore.to_sym
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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