-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from alpaca-tc/refactor-code
Move modules used only for DiverDown::Web under it
- Loading branch information
Showing
15 changed files
with
205 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.