class Storexplore::Testing::DummyStore

Read / Write model for a disk html store page. Every modification is directly saved to the disk, nothing remains in memory. All stores are generated in a specific subdirectory of Storexplore::Testing::Configuration dummy_store_generation_dir / Storexplore::Testing::DummyStoreConstants #NAME

Attributes

name[R]

Name of the page

Public Class Methods

new(path, name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 138
def initialize(path, name)
  @path = path
  @name = name
  if !File.exists?(path)
    write(name, [], [], {})
  end
end
open(store_name) click to toggle source

Initializes or opens a dummy store in the 'store_name' subdirectory

# File lib/storexplore/testing/dummy_store.rb, line 36
def self.open(store_name)
  new(root_path(store_name), store_name)
end
uri(store_name) click to toggle source

file:// uri for the place where the 'store_name' store would be created

# File lib/storexplore/testing/dummy_store.rb, line 42
def self.uri(store_name)
  "file://#{root_path(store_name)}"
end
wipe_out() click to toggle source

Deletes from disk all generated dummy stores

# File lib/storexplore/testing/dummy_store.rb, line 47
def self.wipe_out
  FileUtils.rm_rf(root_dir)
end
wipe_out_store(store_name) click to toggle source

Deletes from disk the 'store_name' dummy store

# File lib/storexplore/testing/dummy_store.rb, line 51
def self.wipe_out_store(store_name)
  FileUtils.rm_rf(root_path(store_name))
end

Private Class Methods

root_dir() click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 130
def self.root_dir
  File.join(Testing.config.dummy_store_generation_dir, DummyStoreConstants::NAME)
end
root_path(store_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 134
def self.root_path(store_name)
  "#{root_dir}/#{store_name}/index.html"
end

Public Instance Methods

add_attributes(values) click to toggle source

Adds the specified attributes

# File lib/storexplore/testing/dummy_store.rb, line 115
def add_attributes(values)
  add([], [], HashUtils.stringify_keys(values))
end
attributes(*args) click to toggle source

Hash of the current attributes

# File lib/storexplore/testing/dummy_store.rb, line 108
def attributes(*args)
  return add_attributes(args[0]) if args.size == 1

  _name, _categories, _items, attributes = read
  HashUtils.internalize_keys(attributes)
end
categories() click to toggle source

Child Storexplore::Testing::DummyStore instances for existing sub categories

# File lib/storexplore/testing/dummy_store.rb, line 65
def categories
  _name, categories, _items, _attributes = read
  categories.map do |category_name|
    DummyStore.new("#{absolute_category_dir(category_name)}/index.html", category_name)
  end
end
category(category_name) click to toggle source

Child category Storexplore::Testing::DummyStore instance with the specified name. Creates it if it does not yet exist

# File lib/storexplore/testing/dummy_store.rb, line 73
def category(category_name)
  short_category_name = short_name(category_name)
  add([short_category_name], [], {})
  DummyStore.new("#{absolute_category_dir(short_category_name)}/index.html", category_name)
end
generate(count = 1) click to toggle source

New Storexplore::Testing::DummyStoreGenerator instance for this

# File lib/storexplore/testing/dummy_store.rb, line 124
def generate(count = 1)
  DummyStoreGenerator.new([self], count)
end
item(item_name) click to toggle source

Child item Storexplore::Testing::DummyStore instance with the specified name. Creates it if it does not yet exist

# File lib/storexplore/testing/dummy_store.rb, line 95
def item(item_name)
  short_item_name = short_name(item_name)
  add([], [short_item_name], {})
  DummyStore.new(absolute_item_file(short_item_name), item_name)
end
items() click to toggle source

Child Storexplore::Testing::DummyStore instances for existing sub items

# File lib/storexplore/testing/dummy_store.rb, line 87
def items
  _name, _categories, items, _attributes = read
  items.map do |item_name|
    DummyStore.new(absolute_item_file(item_name), item_name)
  end
end
remove_attributes(*attribute_names) click to toggle source

Removes the specified attriutes

# File lib/storexplore/testing/dummy_store.rb, line 119
def remove_attributes(*attribute_names)
  remove([], [], ArrayUtils.stringify(attribute_names))
end
remove_category(category_name) click to toggle source

Deletes the specified child category.

# File lib/storexplore/testing/dummy_store.rb, line 79
def remove_category(category_name)
  short_category_name = short_name(category_name)
  remove([short_category_name], [], [])
  FileUtils.rm_rf(absolute_category_dir(short_category_name))
end
remove_item(item_name) click to toggle source

Deletes the specified child item.

# File lib/storexplore/testing/dummy_store.rb, line 101
def remove_item(item_name)
  short_item_name = short_name(item_name)
  remove([], [short_item_name], [])
  FileUtils.rm_rf(absolute_item_file(short_item_name))
end
uri() click to toggle source

file:// uri of the page

# File lib/storexplore/testing/dummy_store.rb, line 56
def uri
  "file://#{@path}"
end

Private Instance Methods

absolute_category_dir(category_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 150
def absolute_category_dir(category_name)
  "#{File.dirname(@path)}/#{relative_category_dir(category_name)}"
end
absolute_item_file(item_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 157
def absolute_item_file(item_name)
  "#{File.dirname(@path)}/#{relative_item_file(item_name)}"
end
add(extra_categories, extra_items, extra_attributes) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 164
def add(extra_categories, extra_items, extra_attributes)
  name, categories, items, attributes = read

  if !ArrayUtils.contains?(categories, extra_categories) || !ArrayUtils.contains?(items, extra_items) || !HashUtils.contains?(attributes,extra_attributes)
    write(name, categories + extra_categories, items + extra_items, attributes.merge(extra_attributes))
  end
end
content(name, categories, items, attributes) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 181
def content(name, categories, items, attributes)
  (["<!DOCTYPE html>",
    "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>",
    "<body><h1>#{name}</h1><div id=\"categories\"><h2>Categories</h2><ul>"] +
   categories.map {|cat|  "<li><a class=\"category\" href=\"#{relative_category_dir(cat)}/index.html\">#{cat}</a></li>" } +
   ['</ul></div><div id="items"><h2>Items</h2><ul>']+
   items.map      {|item| "<li><a class=\"item\" href=\"#{relative_item_file(item)}\">#{item}</a></li>" } +
   ['</ul></div><div id="attributes"><h2>Attributes</h2><ul>']+
   attributes.map {|key, value| "<li><span id=\"#{key}\">#{value}</span></li>" } +
   ["</ul></div></body></html>"]).join("\n")
end
parse(lines) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 197
def parse(lines)
  name = ""
  categories = []
  items = []
  attributes = {}
  lines.each do |line|
    name_match = /<h1>([^<]+)<\/h1>/.match(line)
    sub_match = /<li><a class=\"([^\"]+)\" href=\"[^\"]+.html\">([^<]+)<\/a><\/li>/.match(line)
    attr_match = /<li><span id=\"([^\"]+)\">([^<]+)<\/span><\/li>/.match(line)

    if !!name_match
      name = name_match[1]
    elsif !!sub_match
      case sub_match[1]
      when "category" then categories << sub_match[2]
      when "item" then items << sub_match[2]
      end
    elsif !!attr_match
      attributes[attr_match[1]] = attr_match[2]
    end
  end
  [name, categories, items, attributes]
end
read() click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 193
def read
  parse(IO.readlines(@path))
end
relative_category_dir(category_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 153
def relative_category_dir(category_name)
  category_name
end
relative_item_file(item_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 160
def relative_item_file(item_name)
  "#{item_name}.html"
end
remove(wrong_categories, wrong_items, wrong_attributes) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 171
def remove(wrong_categories, wrong_items, wrong_attributes)
  name, categories, items, attributes = read
  write(name, categories - wrong_categories, items - wrong_items, HashUtils.without(attributes,wrong_attributes))
end
short_name(full_name) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 146
def short_name(full_name)
  full_name[0..20]
end
write(name, categories, items, attributes) click to toggle source
# File lib/storexplore/testing/dummy_store.rb, line 176
def write(name, categories, items, attributes)
  FileUtils.mkdir_p(File.dirname(@path))
  IO.write(@path, content(name, categories, items, attributes))
end