Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move modules used only for DiverDown::Web under it #7

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/diver_down.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ class Error < StandardError; end
DELIMITER = ','

require 'diver_down/definition'
require 'diver_down/definition_store'
require 'diver_down/definition_loader'
require 'diver_down/indented_string_io'
require 'diver_down/helper'
end
4 changes: 2 additions & 2 deletions lib/diver_down/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def self.combine(definition_group:, title:, definitions: [])

attr_reader :definition_group, :title

# ID issued when stored in DefinitionStore
# I want to manage ID in DefinitionStore, but for performance reasons, I have to set Definition#id to determine its identity
# ID issued when stored in DiverDown::Web::DefinitionStore
# I want to manage ID in DiverDown::Web::DefinitionStore, but for performance reasons, I have to set Definition#id to determine its identity
# because naive comparing the identity by instance variables of Definitions is slow.
# @attr_accessor [Integer]
attr_accessor :store_id
Expand Down
35 changes: 0 additions & 35 deletions lib/diver_down/definition_loader.rb

This file was deleted.

87 changes: 0 additions & 87 deletions lib/diver_down/definition_store.rb

This file was deleted.

57 changes: 0 additions & 57 deletions lib/diver_down/indented_string_io.rb

This file was deleted.

9 changes: 6 additions & 3 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class Web
require 'diver_down/web/definition_enumerator'
require 'diver_down/web/bit_id'
require 'diver_down/web/module_store'
require 'diver_down/web/indented_string_io'
require 'diver_down/web/definition_store'
require 'diver_down/web/definition_loader'

# For development
autoload :DevServerMiddleware, 'diver_down/web/dev_server_middleware'

# @param definition_dir [String]
# @param module_store [DiverDown::ModuleStore]
# @param store [DiverDown::DefinitionStore]
def initialize(definition_dir:, module_store:, store: DiverDown::DefinitionStore.new)
# @param store [DiverDown::Web::DefinitionStore]
def initialize(definition_dir:, module_store:, store: DiverDown::Web::DefinitionStore.new)
@store = store
@module_store = module_store
@files_server = Rack::Files.new(File.join(WEB_DIR))
Expand Down Expand Up @@ -81,7 +84,7 @@ def call(env)
private

def load_definition_files_on_thread(definition_files)
definition_loader = DiverDown::DefinitionLoader.new
definition_loader = DiverDown::Web::DefinitionLoader.new

Thread.new do
loop do
Expand Down
37 changes: 37 additions & 0 deletions lib/diver_down/web/definition_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module DiverDown
class Web
class DefinitionLoader
# @param path [String]
def load_file(path)
hash = case File.extname(path)
when '.yaml', '.yml'
from_yaml(path)
when '.msgpack'
from_msgpack(path)
when '.json'
from_json(path)
else
raise ArgumentError, "Unsupported file type: #{path}"
end

DiverDown::Definition.from_hash(hash)
end

private

def from_json(path)
JSON.parse(File.read(path))
end

def from_yaml(path)
YAML.load_file(path)
end

def from_msgpack(path)
MessagePack.unpack(File.binread(path))
end
end
end
end
89 changes: 89 additions & 0 deletions lib/diver_down/web/definition_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

module DiverDown
class Web
class DefinitionStore
include Enumerable

attr_reader :bit_id

def initialize
# Hash{ Integer(unique bit flag) => DiverDown::Definition }
@definitions = []
@definition_group_store = Hash.new { |h, k| h[k] = [] }
end

# @param id [Integer]
# @raise [KeyError] if the id is not found
# @return [DiverDown::Definition]
def get(id)
index = id - 1

raise(KeyError, "id not found: #{id}") if id <= 0 || @definitions.size < id

@definitions.fetch(index)
end

# @param definitions [Array<DiverDown::Definition>]
# @return [Array<Integer>] ids of the definitions
def set(*definitions)
definitions.map do
raise(ArgumentError, 'definition already set') if _1.store_id

_1.store_id = @definitions.size + 1

@definitions.push(_1)
@definition_group_store[_1.definition_group] << _1

_1.store_id
end
end

# @return [Array<String, nil>]
def definition_groups
keys = @definition_group_store.keys

# Sort keys with nil at the end
with_nil = keys.include?(nil)
keys.delete(nil) if with_nil
keys.sort!
keys.push(nil) if with_nil

keys
end

# @param definition_group [String, nil]
# @return [Array<DiverDown::Definition>]
def filter_by_definition_group(definition_group)
@definition_group_store.fetch(definition_group, [])
end

# @param id [Integer]
# @return [Boolean]
def key?(id)
id.positive? && id <= @definitions.size
end

# @return [Integer]
def length
@definitions.length
end
alias size length

# @return [Boolean]
def empty?
@definitions.empty?
end

# @yield [DiverDown::Definition]
def each
return enum_for(__method__) unless block_given?

# NOTE: To allow values to be rewritten during #each, duplicate the value through #to_a.
@definitions.each.with_index(1) do |definition, id|
yield(id, definition)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/diver_down/web/definition_to_dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def length
def initialize(definition, module_store, compound: false, concentrate: false, only_module: false)
@definition = definition
@module_store = module_store
@io = DiverDown::IndentedStringIo.new
@io = DiverDown::Web::IndentedStringIo.new
@indent = 0
@compound = compound || only_module # When only-module is enabled, dependencies between modules are displayed as compound.
@compound_map = Hash.new { |h, k| h[k] = {} } # Hash{ ltail => Hash{ lhead => issued id } }
Expand Down
Loading