Skip to content

Commit

Permalink
Merge 743518e into ab798b5
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed Feb 9, 2024
2 parents ab798b5 + 743518e commit 3c64937
Show file tree
Hide file tree
Showing 36 changed files with 593 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Expand Up @@ -14,10 +14,10 @@ name: codeql
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main
schedule:
- cron: '45 10 * * 6'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Expand Up @@ -8,7 +8,7 @@ name: dependency-review
on:
pull_request:
branches:
- master
- main

permissions:
contents: read
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ruby.yml
Expand Up @@ -3,10 +3,10 @@ name: build
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

permissions:
contents: read
Expand All @@ -23,6 +23,8 @@ jobs:
bundler-cache: true
- name: Run rubocop
run: bundle exec rubocop
- name: Run rbs + steep
run: bundle exec steep check
spec:
runs-on: ubuntu-latest
strategy:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/semgrep.yml
Expand Up @@ -5,7 +5,6 @@ on:
push:
branches:
- main
- master
paths:
- .github/workflows/semgrep.yml
schedule:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -4,6 +4,8 @@ source 'https://rubygems.org'

gemspec

gem 'rbs'
gem 'steep'
gem 'rubyzip'

group :development do
Expand Down
33 changes: 33 additions & 0 deletions Steepfile
@@ -0,0 +1,33 @@
# D = Steep::Diagnostic
#
target :lib do
signature 'sig'

check 'lib'
# check 'tasks'

# check 'Gemfile'
# check 'Guardfile'
# check 'Rakefile'
# check 'Steepfile'

# library 'rubyzip'
library 'yaml'
library 'securerandom'
#
# # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
# # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
# # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
# # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
# # configure_code_diagnostics do |hash| # You can setup everything yourself
# # hash[D::Ruby::NoMethod] = :information
# # end
end

# target :test do
# signature 'sig', 'sig-private'
#
# check 'test'
#
# # library 'pathname' # Standard libraries
# end
9 changes: 7 additions & 2 deletions lib/rambling/trie/compressor.rb
Expand Up @@ -5,9 +5,11 @@ module Trie
# Responsible for the compression process of a trie data structure.
class Compressor
# Compresses a {Nodes::Node Node} from a trie data structure.
# @param [Nodes::Raw] node the node to compress.
# @param [Nodes::Node] node the node to compress.
# @return [Nodes::Compressed] node the compressed version of the node.
def compress node
return if node.nil?

if node.compressible?
compress_child_and_merge node
else
Expand All @@ -22,8 +24,11 @@ def compress_child_and_merge node
end

def merge node, other
letter = node.letter.to_s << other.letter.to_s
if other.nil?
return new_compressed_node node.letter, node.parent, node.children_tree, node.terminal?
end

letter = node.letter.to_s << other.letter.to_s
new_compressed_node letter.to_sym, node.parent, other.children_tree, other.terminal?
end

Expand Down
7 changes: 5 additions & 2 deletions lib/rambling/trie/configuration/properties.rb
Expand Up @@ -46,8 +46,11 @@ def reset
attr_writer :readers, :serializers

def reset_readers
plain_text_reader = Rambling::Trie::Readers::PlainText.new
@readers = Rambling::Trie::Configuration::ProviderCollection.new :reader, txt: plain_text_reader
@readers = Rambling::Trie::Configuration::ProviderCollection.new :reader, default_reader_providers
end

def default_reader_providers
{txt: Rambling::Trie::Readers::PlainText.new }
end

def reset_serializers
Expand Down
4 changes: 3 additions & 1 deletion lib/rambling/trie/container.rb
Expand Up @@ -87,7 +87,7 @@ def scan word = ''

# Returns all words within a string that match a word contained in the trie.
# @param [String] phrase the string to look for matching words in.
# @return [Enumerator<String>] all the words in the given string that match a word in the trie.
# @return [Array<String>] all the words in the given string that match a word in the trie.
# @yield [String] each word found in phrase.
def words_within phrase
words_within_root(phrase).to_a
Expand Down Expand Up @@ -196,6 +196,8 @@ def words_within_root phrase
yield word
end
end

nil
end

def compress_root
Expand Down
6 changes: 4 additions & 2 deletions lib/rambling/trie/nodes/node.rb
Expand Up @@ -52,6 +52,8 @@ def first_child
# rubocop:disable Lint/UnreachableLoop
children_tree.each_value { |child| return child }
# rubocop:enable Lint/UnreachableLoop

nil
end

# Indicates if the current node is the root node.
Expand Down Expand Up @@ -106,7 +108,7 @@ def scan chars
end

# Returns all words that match a prefix of any length within chars.
# @param [String] chars the chars to base the prefix on.
# @param [Array[String]] chars the chars to base the prefix on.
# @return [Enumerator<String>] all the words that match a prefix by chars.
# @yield [String] each word found.
def match_prefix chars
Expand Down Expand Up @@ -147,7 +149,7 @@ def key? letter
# Delete a given letter and its corresponding {Node Node} from
# this {Node Node}'s children tree.
# @param [Symbol] letter the letter to delete from the node's children tree.
# @return [Node] the node corresponding to the deleted letter.
# @return [Node, nil] the node corresponding to the deleted letter.
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-delete Hash#delete
def delete letter
children_tree.delete letter
Expand Down
2 changes: 1 addition & 1 deletion lib/rambling/trie/nodes/raw.rb
Expand Up @@ -7,7 +7,7 @@ module Nodes
class Raw < Rambling::Trie::Nodes::Node
# Adds a word to the current raw (uncompressed) trie node.
# @param [Array<Symbol>] chars the char array to add to the trie.
# @return [Raw] the added/modified node based on the word added.
# @return [Node] the added/modified node based on the word added.
# @note This method clears the contents of the chars variable.
def add chars
if chars.empty?
Expand Down
4 changes: 4 additions & 0 deletions lib/rambling/trie/serializers/zip.rb
Expand Up @@ -26,6 +26,10 @@ def load filepath

::Zip::File.open filepath do |zip|
entry = zip.entries.first
if entry.nil?
return
end

entry_path = path entry.name
entry.extract entry_path

Expand Down
27 changes: 27 additions & 0 deletions sig/lib/rambling/trie.rbs
@@ -0,0 +1,27 @@
module Rambling
module Trie
VERSION: String

@properties: Configuration::Properties

def self.create: (String?, Readers::Reader?) ?{ (Container) -> void } -> Container

def self.load: (String, Serializers::Serializer[Nodes::Node]?) ?{ (Container) -> void } -> Container

def self.dump: (Container, String, Serializers::Serializer[Nodes::Node]?) -> void

def self.config: ?{ (Configuration::Properties) -> void } -> Configuration::Properties

private

def self.properties: -> Configuration::Properties

def self.readers: -> Configuration::ProviderCollection[Readers::Reader]

def self.serializers: -> Configuration::ProviderCollection[Serializers::Serializer[Nodes::Node]]

def self.compressor: -> Compressor

def self.root_builder: -> ^() -> Nodes::Node
end
end
17 changes: 17 additions & 0 deletions sig/lib/rambling/trie/comparable.rbs
@@ -0,0 +1,17 @@
module Rambling
module Trie
module Comparable
def ==: (Nodes::Node) -> bool

private

# abstract methods

def letter: -> Symbol

def terminal?: -> bool

def children_tree: -> Hash[Symbol, Nodes::Node]
end
end
end
17 changes: 17 additions & 0 deletions sig/lib/rambling/trie/compressible.rbs
@@ -0,0 +1,17 @@
module Rambling
module Trie
module Compressible
def compressible?: -> bool

private

# abstract methods

def root?: -> bool

def terminal?: -> bool

def children_tree: -> Hash[Symbol, Nodes::Node]
end
end
end
19 changes: 19 additions & 0 deletions sig/lib/rambling/trie/compressor.rbs
@@ -0,0 +1,19 @@
module Rambling
module Trie
class Compressor
def compress: (Nodes::Node?) -> Nodes::Compressed?

private

def compress_child_and_merge: (Nodes::Node) -> Nodes::Compressed

def merge: (Nodes::Node, Nodes::Node?) -> Nodes::Compressed

def compress_children_and_copy: (Nodes::Node) -> Nodes::Compressed

def compress_children: (Hash[Symbol, Nodes::Node]) -> Hash[Symbol, Nodes::Node]

def new_compressed_node: (Symbol?, Nodes::Node?, Hash[Symbol, Nodes::Node], bool?) -> Nodes::Compressed
end
end
end
28 changes: 28 additions & 0 deletions sig/lib/rambling/trie/configuration/properties.rbs
@@ -0,0 +1,28 @@
module Rambling
module Trie
module Configuration
class Properties
attr_reader readers: ProviderCollection[Readers::Reader]
attr_reader serializers: ProviderCollection[Serializers::Serializer[Nodes::Node]]
attr_accessor compressor: Compressor
attr_accessor root_builder: ^() -> Nodes::Node
attr_accessor tmp_path: String

def initialize: -> void

def reset: -> void

private

attr_writer readers: ProviderCollection[Readers::Reader]
attr_writer serializers: ProviderCollection[Serializers::Serializer[Nodes::Node]]

def reset_readers: -> void

def default_reader_providers: -> Hash[Symbol, Readers::Reader]

def reset_serializers: -> void
end
end
end
end
47 changes: 47 additions & 0 deletions sig/lib/rambling/trie/configuration/provider_collection.rbs
@@ -0,0 +1,47 @@
module Rambling
module Trie
module Configuration
class ProviderCollection[TProvider < _Nilable]
interface _Nilable
def nil?: -> bool
end

@providers: Hash[Symbol, TProvider]

attr_reader name: Symbol
attr_reader default: TProvider?

def initialize: (Symbol, Hash[Symbol, TProvider], ?TProvider?) -> void

def []: (Symbol) -> TProvider

def add: (Symbol, TProvider) -> TProvider

def default=: (TProvider?) -> TProvider?

def formats: -> Array[Symbol]

def providers: -> Hash[Symbol, TProvider]

def reset: -> void

def resolve: (String) -> TProvider?

private

attr_reader configured_default: TProvider?
attr_reader configured_providers: Hash[Symbol, TProvider]

def []=: (Symbol, TProvider) -> TProvider

def values: -> Array[TProvider]

def file_format: (String) -> Symbol

def contains?: (TProvider?) -> bool

alias provider_instances values
end
end
end
end

0 comments on commit 3c64937

Please sign in to comment.