Skip to content

Commit

Permalink
fix old namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Apr 10, 2024
1 parent fc94f70 commit 7ae87ac
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 142 deletions.
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
6 changes: 3 additions & 3 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Web

# @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 @@ -84,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
50 changes: 26 additions & 24 deletions lib/diver_down/web/definition_loader.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# frozen_string_literal: true

module DiverDown
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
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
DiverDown::Definition.from_hash(hash)
end

private
private

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

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

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

module DiverDown
class DefinitionStore
include Enumerable
class Web
class DefinitionStore
include Enumerable

attr_reader :bit_id
attr_reader :bit_id

def initialize
# Hash{ Integer(unique bit flag) => DiverDown::Definition }
@definitions = []
@definition_group_store = Hash.new { |h, k| h[k] = [] }
end
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
# @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
raise(KeyError, "id not found: #{id}") if id <= 0 || @definitions.size < id

@definitions.fetch(index)
end
@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
# @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
_1.store_id = @definitions.size + 1

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

_1.store_id
_1.store_id
end
end
end

# @return [Array<String, nil>]
def definition_groups
keys = @definition_group_store.keys
# @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
# 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
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 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
# @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 [Integer]
def length
@definitions.length
end
alias size length

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

# @yield [DiverDown::Definition]
def each
return enum_for(__method__) unless block_given?
# @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)
# 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
Expand Down
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
82 changes: 42 additions & 40 deletions lib/diver_down/web/indented_string_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,56 @@
require 'forwardable'

module DiverDown
class IndentedStringIo
extend ::Forwardable
class Web
class IndentedStringIo
extend ::Forwardable

def_delegators :@io, :rewind, :string
def_delegators :@io, :rewind, :string

attr_accessor :indent
attr_accessor :indent

# @param tab [String]
def initialize(tab: ' ')
@io = StringIO.new
@indent = 0
@tab = tab
end
# @param tab [String]
def initialize(tab: ' ')
@io = StringIO.new
@indent = 0
@tab = tab
end

# @param contents [Array<String>]
# @param indent [Boolean] Enable or disable indentation
# @return [void]
def write(*contents, indent: true)
indent_string = if indent
@tab * @indent
else
''
end

string = contents.join
lines = string.lines
lines.each do |line|
if line == "\n"
@io.write "\n"
else
@io.write "#{indent_string}#{line}"
# @param contents [Array<String>]
# @param indent [Boolean] Enable or disable indentation
# @return [void]
def write(*contents, indent: true)
indent_string = if indent
@tab * @indent
else
''
end

string = contents.join
lines = string.lines
lines.each do |line|
if line == "\n"
@io.write "\n"
else
@io.write "#{indent_string}#{line}"
end
end
end
end

# @param content [String]
# @return [void]
def puts(*contents, indent: true)
write("#{contents.join("\n")}\n", indent:)
nil
end
# @param content [String]
# @return [void]
def puts(*contents, indent: true)
write("#{contents.join("\n")}\n", indent:)
nil
end

# increase the indent level for the block
def indented
@indent += 1
yield
ensure
@indent -= 1
# increase the indent level for the block
def indented
@indent += 1
yield
ensure
@indent -= 1
end
end
end
end
6 changes: 3 additions & 3 deletions spec/diver_down/web/definition_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe 'InstanceMethods' do
describe '#each' do
it 'returns definitions sorted by definition_group' do
store = DiverDown::DefinitionStore.new
store = DiverDown::Web::DefinitionStore.new
definition_1 = DiverDown::Definition.new
definition_2 = DiverDown::Definition.new(definition_group: 'b', title: 'definition_2')
definition_3 = DiverDown::Definition.new(definition_group: 'c', title: 'definition_3')
Expand Down Expand Up @@ -37,7 +37,7 @@ def assert_query(store, title, expected)
end

it 'filters by title' do
store = DiverDown::DefinitionStore.new
store = DiverDown::Web::DefinitionStore.new

definition_1 = DiverDown::Definition.new(
title: '01234',
Expand Down Expand Up @@ -83,7 +83,7 @@ def assert_query(store, source, expected)
end

it 'filters by source' do
store = DiverDown::DefinitionStore.new
store = DiverDown::Web::DefinitionStore.new

definition_1 = DiverDown::Definition.new(
title: '01234',
Expand Down
Loading

0 comments on commit 7ae87ac

Please sign in to comment.