class Megam::SpaceCollection

Attributes

iterator[R]

Public Class Methods

json_create(o) click to toggle source
# File lib/megam/core/space_collection.rb, line 120
def self.json_create(o)
  collection = self.new()
  o["results"].each do |spaces_list|
    spaces_array = spaces_list.kind_of?(Array) ? spaces_list : [ spaces_list ]
    spaces_array.each do |space|
      collection.insert(space)
    end
  end
  collection
end
new() click to toggle source
# File lib/megam/core/space_collection.rb, line 22
def initialize
  @spaces = Array.new
  @spaces_by_name = Hash.new
  @insert_after_idx = nil
end

Public Instance Methods

<<(*args) click to toggle source
# File lib/megam/core/space_collection.rb, line 42
def <<(*args)
  args.flatten.each do |a|
    is_megam_space(a)
    @spaces << a
    @spaces_by_name[a.wiki_name] = @spaces.length - 1
  end
  self
end
Also aliased as: push
[](index) click to toggle source
# File lib/megam/core/space_collection.rb, line 32
def [](index)
  @spaces[index]
end
[]=(index, arg) click to toggle source
# File lib/megam/core/space_collection.rb, line 36
def []=(index, arg)
  is_megam_space(arg)
  @spaces[index] = arg
  @spaces_by_name[arg.wiki_name] = index
end
all_spaces() click to toggle source
# File lib/megam/core/space_collection.rb, line 28
def all_spaces
  @spaces
end
each() { |space| ... } click to toggle source
# File lib/megam/core/space_collection.rb, line 73
def each
  @spaces.each do |space|
    yield space
  end
end
each_index() { |i| ... } click to toggle source
# File lib/megam/core/space_collection.rb, line 79
def each_index
  @spaces.each_index do |i|
    yield i
  end
end
empty?() click to toggle source
# File lib/megam/core/space_collection.rb, line 85
def empty?
  @spaces.empty?
end
insert(space) click to toggle source
# File lib/megam/core/space_collection.rb, line 54
def insert(space)
  is_megam_space(space)
  if @insert_after_idx
    # in the middle of executing a run, so any spaces inserted now should
    # be placed after the most recent addition done by the currently executing
    # space
    @spaces.insert(@insert_after_idx + 1, space)
    # update name -> location mappings and register new space
    @spaces_by_name.each_key do |key|
    @spaces_by_name[key] += 1 if @spaces_by_name[key] > @insert_after_idx
    end
    @spaces_by_name[space.wiki_name] = @insert_after_idx + 1
    @insert_after_idx += 1
  else
  @spaces << space
  @spaces_by_name[space.wiki_name] = @spaces.length - 1 
  end
end
lookup(space) click to toggle source
# File lib/megam/core/space_collection.rb, line 89
def lookup(space)
  lookup_by = nil
  if space.kind_of?(Megam::Space)
  lookup_by = space.wiki_name
  elsif space.kind_of?(String)
  lookup_by = space
  else
    raise ArgumentError, "Must pass a Megam::Space or String to lookup"
  end
  res = @spaces_by_name[lookup_by]
  unless res
    raise ArgumentError, "Cannot find a space matching #{lookup_by} (did you define it first?)"
  end
  @spaces[res]
end
push(*args)

‘push’ is an alias method to <<

Alias for: <<
to_hash() click to toggle source

Transform the ruby obj -> to a Hash

# File lib/megam/core/space_collection.rb, line 106
def to_hash
  index_hash = Hash.new
  self.each do |space|
    index_hash[space.wiki_name] = space.to_s
  end
  index_hash
end
to_json(*a) click to toggle source

Serialize this object as a hash: called from JsonCompat. Verify if this called from JsonCompat during testing.

# File lib/megam/core/space_collection.rb, line 116
def to_json(*a)
  for_json.to_json(*a)
end

Private Instance Methods

is_megam_space(arg) click to toggle source
# File lib/megam/core/space_collection.rb, line 135
def is_megam_space(arg)
  unless arg.kind_of?(Megam::Space)
    raise ArgumentError, "Members must be Megam::Space's"
  end
  true
end
to_s() click to toggle source
# File lib/megam/core/space_collection.rb, line 142
def to_s
  Megam::Stuff.styled_hash(to_hash)
end