class Fable::Container

Attributes

bit_flags[RW]
content[RW]
counting_at_start_only[RW]
counting_at_start_only?[RW]
name[RW]
named_content[RW]
path_to_first_leaf_content[RW]
turn_index_should_be_counted[RW]
turn_index_should_be_counted?[RW]
visits_should_be_counted[RW]
visits_should_be_counted?[RW]

Public Class Methods

new(flags) click to toggle source
Calls superclass method
# File lib/fable/container.rb, line 7
def initialize(flags)
  super()
  self.bit_flags = flags
  self.content = []
  self.named_content = {}
  self.process_bit_flags
end

Public Instance Methods

add_content(content_to_add) click to toggle source
# File lib/fable/container.rb, line 15
def add_content(content_to_add)
  if content_to_add.is_a?(Enumerable)
    content_to_add.each{|individual_items| add_content(individual_items) }
    return
  end

  debugger if content_to_add.nil?

  if !content_to_add.parent.nil?
    raise Error, "content is already in #{content_to_add.parent}"
  end

  content_to_add.parent = self
  content << content_to_add

  try_adding_to_named_content(content_to_add)
end
add_contents_of_container(other_container) click to toggle source
# File lib/fable/container.rb, line 54
def add_contents_of_container(other_container)
  content += other_container.content
  other_container.content.each do |content_to_add|
    content_to_add.parent = self
    try_adding_to_named_content(content_to_add)
  end
end
add_to_named_content(content_to_add) click to toggle source
# File lib/fable/container.rb, line 49
def add_to_named_content(content_to_add)
  content_to_add.parent = self
  named_content[content_to_add.name] = content_to_add
end
build_string_of_hierarchy(io, indentation, pointed_object) click to toggle source
# File lib/fable/container.rb, line 117
def build_string_of_hierarchy(io, indentation, pointed_object)
  io << indentation_string(indentation)
  io << "["

  if self.valid_name?
    io << " (#{self.name})"
  end

  if self == pointed_object
    io << " <---"
  end

  io << "\n"

  indentation += 1


  content.each_with_index do |object, index|
    if object.is_a?(Container)
      object.build_string_of_hierarchy(io, indentation, pointed_object)
    else
      io << indentation_string(indentation)
      if object.is_a?(StringValue)
        io << "\"#{object.to_s.gsub("\n", "\\n")}\""
      else
        io << object.to_s
      end
    end

    if index != (content.size - 1)
      io << ","
    end

    if !object.is_a?(Container) && object == pointed_object
      io << " <---"
    end

    io << "\n"
  end

  only_named_content = named_content.reject{|name, item| content.include?(item) }

  if only_named_content.any?
    io << indentation_string(indentation)
    io << "-- named: --\n"

    only_named_content.each do |key, container|
      container.build_string_of_hierarchy(io, indentation, pointed_object)
      io << "\n"
    end
  end

  indentation -= 1

  io << indentation_string(indentation)
  io << "]"
end
content_at_path(path, options= { partial_path_start: 0, partial_path_length: -1 }) click to toggle source
# File lib/fable/container.rb, line 72
def content_at_path(path, options= { partial_path_start: 0, partial_path_length: -1 })
  partial_path_length = options[:partial_path_length]
  partial_path_start = options[:partial_path_start]

  if partial_path_length == -1
    partial_path_length = path.length
  end

  result = SearchResult.new
  result.approximate = false

  current_container = self
  current_object = self

  partial_path_end = (partial_path_length - 1)

  (partial_path_start..partial_path_end).each do |i|
    component = path.components[i]

    # Path component was wrong type
    if current_container.nil?
      result.approximate = true
      break
    end

    found_object = current_container.content_with_path_component(component)

    # Couldn't resolve entire path?
    if found_object.nil?
      result.approximate = true
      break
    end

    current_object = found_object
    if found_object.is_a?(Container)
      current_container = found_object
    else
      current_container = nil
    end
  end

  result.object = current_object
  return result
end
content_with_path_component(component) click to toggle source
# File lib/fable/container.rb, line 62
def content_with_path_component(component)
  if component.is_index?
    return content[component.index]
  elsif component.is_parent?
    return self.parent
  else
    return named_content[component.name]
  end
end
has_bit_flags?() click to toggle source
# File lib/fable/container.rb, line 199
def has_bit_flags?
  !self.bit_flags.nil?
end
insert_content_at(content_to_add, index) click to toggle source
# File lib/fable/container.rb, line 33
def insert_content_at(content_to_add, index)
  if !content_to_add.parent.nil?
    raise Error, "content is already in #{content_to_add.parent}"
  end

  content_to_add.parent = self
  content.insert(index, content_to_add)
  try_adding_to_named_content(content_to_add)
end
process_bit_flags() click to toggle source
# File lib/fable/container.rb, line 187
def process_bit_flags
  if has_bit_flags?
    self.visits_should_be_counted = (bit_flags & 1) > 0
    self.turn_index_should_be_counted = (bit_flags & 2) > 0
    self.counting_at_start_only = (bit_flags & 4) > 0
  else
    self.visits_should_be_counted = false
    self.turn_index_should_be_counted = false
    self.counting_at_start_only = false
  end
end
try_adding_to_named_content(content_to_add) click to toggle source
# File lib/fable/container.rb, line 43
def try_adding_to_named_content(content_to_add)
  if content_to_add.respond_to?(:valid_name?) && content_to_add.valid_name?
    add_to_named_content(content_to_add)
  end
end
valid_name?() click to toggle source
# File lib/fable/container.rb, line 179
def valid_name?
  !name.to_s.empty?
end

Protected Instance Methods

internal_path_to_first_lead_content() click to toggle source
# File lib/fable/container.rb, line 205
def internal_path_to_first_lead_content
  components = []
  container = self
  while !container.nil?
    if container.content.size > 0
      components << Path::Component.new(0)
      container = container.content.first
    end
  end

  return Path.new(components)
end