Class: Fable::Container

Inherits:
RuntimeObject show all
Defined in:
lib/fable/container.rb

Instance Attribute Summary collapse

Attributes inherited from RuntimeObject

#original_object, #own_debug_metadata, #parent, #path

Instance Method Summary collapse

Methods inherited from RuntimeObject

#compact_path_string, #convert_path_to_relative, #copy, #debug_line_number_of_path, #debug_metadata, #indentation_string, #resolve_path, #root_content_container

Constructor Details

#initialize(flags) ⇒ Container

Returns a new instance of Container.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#bit_flagsObject

Returns the value of attribute bit_flags.



3
4
5
# File 'lib/fable/container.rb', line 3

def bit_flags
  @bit_flags
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/fable/container.rb', line 3

def content
  @content
end

#counting_at_start_onlyObject Also known as: counting_at_start_only?

Returns the value of attribute counting_at_start_only.



3
4
5
# File 'lib/fable/container.rb', line 3

def counting_at_start_only
  @counting_at_start_only
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/fable/container.rb', line 3

def name
  @name
end

#named_contentObject

Returns the value of attribute named_content.



3
4
5
# File 'lib/fable/container.rb', line 3

def named_content
  @named_content
end

#path_to_first_leaf_contentObject

Returns the value of attribute path_to_first_leaf_content.



3
4
5
# File 'lib/fable/container.rb', line 3

def path_to_first_leaf_content
  @path_to_first_leaf_content
end

#turn_index_should_be_countedObject Also known as: turn_index_should_be_counted?

Returns the value of attribute turn_index_should_be_counted.



3
4
5
# File 'lib/fable/container.rb', line 3

def turn_index_should_be_counted
  @turn_index_should_be_counted
end

#visits_should_be_countedObject Also known as: visits_should_be_counted?

Returns the value of attribute visits_should_be_counted.



3
4
5
# File 'lib/fable/container.rb', line 3

def visits_should_be_counted
  @visits_should_be_counted
end

Instance Method Details

#add_content(content_to_add) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



54
55
56
57
58
59
60
# 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) ⇒ Object



49
50
51
52
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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 }) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
# 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?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/fable/container.rb', line 199

def has_bit_flags?
  !self.bit_flags.nil?
end

#insert_content_at(content_to_add, index) ⇒ Object



33
34
35
36
37
38
39
40
41
# 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_flagsObject



187
188
189
190
191
192
193
194
195
196
197
# 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) ⇒ Object



43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/fable/container.rb', line 179

def valid_name?
  !name.to_s.empty?
end